Skip to content

Commit 3320685

Browse files
committed
ButtonContainer tests added
1 parent b317285 commit 3320685

File tree

6 files changed

+215
-9
lines changed

6 files changed

+215
-9
lines changed

.storybook/Storyshots.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import initStoryshots from '@storybook/addon-storyshots';
2-
// import 'jest-styled-components';
1+
import initStoryshots from '@storybook/addon-storyshots';
2+
import 'jest-styled-components';
33

4-
// initStoryshots();
4+
initStoryshots();

__tests__/ButtonContainer.test.js

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
import React from 'react';
2+
import renderer from 'react-test-renderer';
3+
import configureStore from 'redux-mock-store';
4+
import { mount } from 'enzyme';
5+
import 'jest-styled-components';
6+
import { ButtonContainer } from '../src/components/ButtonContainer';
7+
import { submit } from '../src/actions';
8+
9+
const middlewares = [];
10+
const mockStore = configureStore(middlewares);
11+
12+
import { mapStateToProps, mapDispatchToProps } from '../src/components/ButtonContainer';
13+
14+
const patterns = [
15+
{
16+
uuid: 'id_12',
17+
name: 'Singleton',
18+
type: 'creational',
19+
codeES5: `function Person() {
20+
if (typeof Person.instance === 'object') return Person.instance;
21+
22+
Person.instance = this;
23+
24+
return this;
25+
}
26+
27+
module.exports = Person;`,
28+
codeES6: `class Person {
29+
constructor() {
30+
if (typeof Person.instance === 'object') {
31+
return Person.instance;
32+
}
33+
Person.instance = this;
34+
return this;
35+
}
36+
}
37+
38+
export default Person;`
39+
},
40+
41+
{
42+
uuid: 'id_34',
43+
name: 'Singleton',
44+
type: 'creational',
45+
codeES5: `function Person() {
46+
if (typeof Person.instance === 'object') return Person.instance;
47+
48+
Person.instance = this;
49+
50+
return this;
51+
}
52+
53+
module.exports = Person;`,
54+
codeES6: `class Person {
55+
constructor() {
56+
if (typeof Person.instance === 'object') {
57+
return Person.instance;
58+
}
59+
Person.instance = this;
60+
return this;
61+
}
62+
}
63+
64+
export default Person;`
65+
},
66+
67+
{
68+
uuid: 'id_56',
69+
name: 'Singleton',
70+
type: 'creational',
71+
codeES5: `function Person() {
72+
if (typeof Person.instance === 'object') return Person.instance;
73+
74+
Person.instance = this;
75+
76+
return this;
77+
}
78+
79+
module.exports = Person;`,
80+
codeES6: `class Person {
81+
constructor() {
82+
if (typeof Person.instance === 'object') {
83+
return Person.instance;
84+
}
85+
Person.instance = this;
86+
return this;
87+
}
88+
}
89+
90+
export default Person;`
91+
},
92+
93+
{
94+
uuid: 'id_78',
95+
name: 'Singleton',
96+
type: 'creational',
97+
codeES5: `function Person() {
98+
if (typeof Person.instance === 'object') return Person.instance;
99+
100+
Person.instance = this;
101+
102+
return this;
103+
}
104+
105+
module.exports = Person;`,
106+
codeES6: `class Person {
107+
constructor() {
108+
if (typeof Person.instance === 'object') {
109+
return Person.instance;
110+
}
111+
Person.instance = this;
112+
return this;
113+
}
114+
}
115+
116+
export default Person;`
117+
}
118+
];
119+
120+
describe('<ButtonContainer /> dispatch action', () => {
121+
it('should dispatch action', () => {
122+
// Initialize mockstore with empty state
123+
const initialState = {};
124+
const store = mockStore(initialState);
125+
126+
// Dispatch the action
127+
store.dispatch(submit('test'));
128+
129+
// Test if the store dispatched the expected actions
130+
const actions = store.getActions();
131+
132+
const expectedPayload = {
133+
type: 'SUBMIT',
134+
payload: 'test'
135+
};
136+
expect(actions).toEqual([expectedPayload]);
137+
});
138+
});
139+
140+
describe('<ButtonContainer /> component', () => {
141+
const mockClick = jest.fn();
142+
const patternsList = patterns;
143+
const current = patterns[0];
144+
const buttonContainerEl = (
145+
<ButtonContainer patterns={patternsList} current={current} onClick={mockClick} />
146+
);
147+
let buttonContainer;
148+
149+
beforeEach(() => {
150+
buttonContainer = mount(buttonContainerEl);
151+
});
152+
153+
it('should roll the dice again when button is clicked', () => {
154+
const dispatch = jest.fn();
155+
156+
mapDispatchToProps(dispatch).onClick('test');
157+
expect(dispatch.mock.calls[0][0]).toEqual({
158+
type: 'SUBMIT',
159+
payload: 'test'
160+
});
161+
});
162+
163+
it('should show previously rolled value', () => {
164+
const initialState = {
165+
progress: {
166+
current: 0
167+
},
168+
patterns: [0, 1, 2, 3]
169+
};
170+
171+
// Just call the method directly passing in sample data
172+
// to make sure it does what it's supposed to
173+
expect(mapStateToProps(initialState).patterns).toEqual([0, 1, 2, 3]);
174+
expect(mapStateToProps(initialState).current).toEqual(0);
175+
});
176+
177+
it('renders without crashing', () => {
178+
expect(buttonContainer).toBeTruthy();
179+
});
180+
181+
it('has styled-component rendered classes', () => {
182+
const tree = renderer.create(buttonContainerEl).toJSON();
183+
expect(tree.props.className).toBeDefined();
184+
});
185+
186+
it('generates 4 buttons', () => {
187+
expect(buttonContainer.find('button')).toHaveLength(4);
188+
});
189+
190+
it('responds to button click', () => {
191+
buttonContainer
192+
.find('button')
193+
.first()
194+
.simulate('click');
195+
196+
expect(mockClick.mock.calls.length).toEqual(1);
197+
});
198+
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"jest-styled-components": "^6.3.1",
6868
"prettier": "1.16.2",
6969
"react-test-renderer": "^16.7.0",
70+
"redux-mock-store": "^1.5.3",
7071
"storybook-addon-styled-component-theme": "^1.1.1",
7172
"webpack": "^4.29.0",
7273
"webpack-bundle-analyzer": "^3.0.3",

src/components/ButtonContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const StyledButtonContainer = styled.div`
1515
margin: 1rem 0 2rem;
1616
`;
1717

18-
const ButtonContainer = props => {
18+
export const ButtonContainer = props => {
1919
const { current, patterns, onClick } = props;
2020

2121
// get 3 random patterns in addition to correct one
@@ -39,9 +39,9 @@ ButtonContainer.propTypes = {
3939
onClick: PropTypes.func.isRequired
4040
};
4141

42-
const mapStateToProps = ({ progress: { current }, patterns }) => ({ patterns, current });
42+
export const mapStateToProps = ({ progress: { current }, patterns }) => ({ patterns, current });
4343

44-
const mapDispatchToProps = dispatch => ({
44+
export const mapDispatchToProps = dispatch => ({
4545
onClick: id => {
4646
dispatch(submit(id));
4747
}

src/pages/About.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ const About = () => (
3030

3131
<Header>References</Header>
3232
<p>
33-
All the code samples impudently taken from this{' '}
33+
All the code samples are taken from this{' '}
3434
<cite>
3535
<a href="//github.com/fbeline/Design-Patterns-JS" target="_blank">
3636
awesome code compilation
3737
</a>{' '}
3838
by Felipe Beline
39-
</cite>{' '}
40-
- because I have no shame.
39+
</cite>
40+
.
4141
</p>
4242
<p>
4343
If you want a deep dive into the subject I can't recommend enough{' '}

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8974,6 +8974,13 @@ [email protected]:
89748974
dependencies:
89758975
minimatch "3.0.4"
89768976

8977+
redux-mock-store@^1.5.3:
8978+
version "1.5.3"
8979+
resolved "https://registry.yarnpkg.com/redux-mock-store/-/redux-mock-store-1.5.3.tgz#1f10528949b7ce8056c2532624f7cafa98576c6d"
8980+
integrity sha512-ryhkkb/4D4CUGpAV2ln1GOY/uh51aczjcRz9k2L2bPx/Xja3c5pSGJJPyR25GNVRXtKIExScdAgFdiXp68GmJA==
8981+
dependencies:
8982+
lodash.isplainobject "^4.0.6"
8983+
89778984
redux@^4.0.1:
89788985
version "4.0.1"
89798986
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.1.tgz#436cae6cc40fbe4727689d7c8fae44808f1bfef5"

0 commit comments

Comments
 (0)