Skip to content

Commit c061e79

Browse files
committed
Added a test.
1 parent 7bc158d commit c061e79

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Call the setter of useState() to update the different part. 1`] = `true`;
4+
5+
exports[`Call the setter of useState() to update the different part. 2`] = `undefined`;
6+
7+
exports[`Call the setter of useState() to update the different part. 3`] = `
8+
Object {
9+
"child": false,
10+
}
11+
`;
12+
13+
exports[`Call the setter of useState() to update the different part. 4`] = `
14+
Object {
15+
"test": Object {
16+
"child": true,
17+
},
18+
"testX": Object {
19+
"child": false,
20+
},
21+
}
22+
`;
23+
324
exports[`Call useState() to get the state and the setter. 1`] = `
425
Object {
526
"test": true,

__tests__/lib/shared-state.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,27 @@ test('Call useState() twice to make sure it did not assign setter again.', () =>
122122
renderer.create(React.createElement(component, null));
123123
expect(shareState.useState).toBeCalled();
124124
});
125+
126+
test('Call the setter of useState() to update the different part.', () => {
127+
const shareState = new SharedState();
128+
jest.spyOn(shareState, 'useState');
129+
const childComponent = () => {
130+
const [state] = shareState.useState('test.child', true);
131+
expect(state).toMatchSnapshot();
132+
return React.createElement('div', null);
133+
};
134+
135+
const parentComponent = () => {
136+
const [state, setState] = shareState.useState('testX');
137+
expect(state).toMatchSnapshot();
138+
if (!state) {
139+
setState({child: false});
140+
}
141+
142+
return React.createElement('div', null);
143+
};
144+
145+
renderer.create(React.createElement(childComponent, null));
146+
renderer.create(React.createElement(parentComponent, null));
147+
expect(shareState.state).toMatchSnapshot();
148+
});

0 commit comments

Comments
 (0)