Skip to content

Commit efd8ac3

Browse files
committed
Added tests.
1 parent ae477b3 commit efd8ac3

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
- checkout
1515
- run: npm install
1616
- run: npm test
17+
- run: npm coveralls
1718

1819
node12:
1920
working_directory: ~/react-hooks-shared-state

__tests__/lib/__snapshots__/shared-state.js.snap

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Setter it was generated execute without path. 1`] = `
3+
exports[`Call useState() to get the state and the setter. 1`] = `
4+
Object {
5+
"test": true,
6+
}
7+
`;
8+
9+
exports[`Call useState() to get the state and the setter. 2`] = `
10+
Object {
11+
"test": false,
12+
}
13+
`;
14+
15+
exports[`Call useState() twice to make sure it did not assign setter again. 1`] = `
16+
Object {
17+
"test": true,
18+
}
19+
`;
20+
21+
exports[`Call useState() twice to make sure it did not assign setter again. 2`] = `
22+
Object {
23+
"test": true,
24+
}
25+
`;
26+
27+
exports[`Call useState() with a initial state to get the state. 1`] = `
28+
Object {
29+
"test": true,
30+
}
31+
`;
32+
33+
exports[`Call useState() with a path and a initial partial state to get the state. 1`] = `true`;
34+
35+
exports[`Setter assign a partial state and call setters with path. 1`] = `
36+
Object {
37+
"test": true,
38+
}
39+
`;
40+
41+
exports[`Setter call setState() without path. 1`] = `
442
Object {
543
"test": true,
644
}

__tests__/lib/shared-state.js

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const React = require('react');
2+
const renderer = require('react-test-renderer');
13
const SharedState = require('../../lib/shared-state');
24

35
test('setState() is assign state and call setters.', () => {
@@ -17,9 +19,88 @@ test('setState() is assign state and call setters.', () => {
1719
expect(shareState.setters[1].set).toBeCalledWith({test: true});
1820
});
1921

20-
test('Setter it was generated execute without path.', () => {
22+
test('Setter call setState() without path.', () => {
2123
const shareState = new SharedState();
2224
const setState = shareState.generateSetState();
25+
jest.spyOn(shareState, 'setState');
2326
setState({test: true});
2427
expect(shareState.state).toMatchSnapshot();
28+
expect(shareState.setState).toBeCalledWith({test: true});
29+
});
30+
31+
test('Setter assign a partial state and call setters with path.', () => {
32+
const shareState = new SharedState();
33+
const setState = shareState.generateSetState('test');
34+
shareState.setters = [
35+
{
36+
path: 'test',
37+
set: jest.fn()
38+
},
39+
{
40+
set: jest.fn()
41+
}
42+
];
43+
setState(true);
44+
expect(shareState.state).toMatchSnapshot();
45+
expect(shareState.setters[0].set).toBeCalledWith(true);
46+
expect(shareState.setters[1].set).toBeCalledWith({test: true});
47+
});
48+
49+
test('Call useState() to get the state and the setter.', () => {
50+
const shareState = new SharedState();
51+
jest.spyOn(shareState, 'useState');
52+
shareState.setState({test: true});
53+
const component = () => {
54+
const [state, setState] = shareState.useState();
55+
expect(state).toMatchSnapshot();
56+
if (state.test) {
57+
setState({test: false});
58+
}
59+
60+
return React.createElement('div', null);
61+
};
62+
63+
renderer.create(React.createElement(component, null));
64+
expect(shareState.useState).toBeCalledTimes(2);
65+
});
66+
67+
test('Call useState() with a initial state to get the state.', () => {
68+
const shareState = new SharedState();
69+
jest.spyOn(shareState, 'useState');
70+
const component = () => {
71+
const [state] = shareState.useState(null, {test: true});
72+
expect(state).toMatchSnapshot();
73+
return React.createElement('div', null);
74+
};
75+
76+
renderer.create(React.createElement(component, null));
77+
expect(shareState.useState).toBeCalled();
78+
});
79+
80+
test('Call useState() with a path and a initial partial state to get the state.', () => {
81+
const shareState = new SharedState();
82+
jest.spyOn(shareState, 'useState');
83+
const component = () => {
84+
const [state] = shareState.useState('test', true);
85+
expect(state).toMatchSnapshot();
86+
return React.createElement('div', null);
87+
};
88+
89+
renderer.create(React.createElement(component, null));
90+
expect(shareState.useState).toBeCalled();
91+
});
92+
93+
test('Call useState() twice to make sure it did not assign setter again.', () => {
94+
const shareState = new SharedState();
95+
jest.spyOn(shareState, 'useState');
96+
shareState.setState({test: true});
97+
const component = () => {
98+
const [state] = shareState.useState();
99+
expect(state).toMatchSnapshot();
100+
return React.createElement('div', null);
101+
};
102+
103+
renderer.create(React.createElement(component, null));
104+
renderer.create(React.createElement(component, null));
105+
expect(shareState.useState).toBeCalled();
25106
});

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"start": "./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
88
"build": "./node_modules/webpack/bin/webpack.js --env.mode=production",
99
"test": "./node_modules/jest/bin/jest.js --coverage",
10+
"coveralls": "./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info",
1011
"ncu": "./node_modules/npm-check-updates/bin/ncu"
1112
},
1213
"repository": {
@@ -34,6 +35,7 @@
3435
"@babel/preset-env": "7.4.3",
3536
"@babel/preset-react": "7.0.0",
3637
"babel-loader": "8.0.5",
38+
"coveralls": "3.0.3",
3739
"eslint": "5.16.0",
3840
"eslint-config-xo": "0.26.0",
3941
"eslint-config-xo-react": "0.19.0",
@@ -43,6 +45,7 @@
4345
"jest": "24.7.1",
4446
"npm-check-updates": "3.1.8",
4547
"react-dom": "16.8.6",
48+
"react-test-renderer": "16.8.6",
4649
"webpack": "4.30.0",
4750
"webpack-cli": "3.3.0",
4851
"webpack-dev-server": "3.3.1"

0 commit comments

Comments
 (0)