-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
134 lines (111 loc) · 3.69 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import React from 'react';
import useSetState from './index';
import Enzyme, { shallow, render, mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
const ENGLISH_GREETING = 'Hello World';
const SPANISH_GREETING = 'Hola Mundo';
describe('useSetState', () => {
const App = () => {
const initState = {
count: 0,
greeting: ENGLISH_GREETING,
};
const [state, setState] = useSetState(initState);
return (
<div>
<div id="count">{state.count}</div>
<div id="greeting">{state.greeting}</div>
<button
id="language"
onClick={() => setState(prev => ({
greeting: prev.greeting === ENGLISH_GREETING
? SPANISH_GREETING
: ENGLISH_GREETING
}))}
/>
<button
id="set"
onClick={() => setState({ count: 1 })}
/>
<button
id="reset"
onClick={() => setState({ count: 0 })}
/>
<button
id="increment"
onClick={() => setState(prev => ({ ...prev, count: prev.count + 1 }))}
/>
</div>
);
};
test('initial state', () => {
const wrapper = shallow(<App/>);
expect(wrapper.find('#count').text()).toBe('0');
});
test('update state', () => {
const wrapper = shallow(<App/>);
const setButton = wrapper.find('#set');
const resetButton = wrapper.find('#reset');
setButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('1');
resetButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('0');
});
test('update based on prev state', () => {
const wrapper = shallow(<App/>);
const incButton = wrapper.find('#increment');
const langButton = wrapper.find('#language');
incButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('1');
incButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('2');
langButton.simulate('click');
expect(wrapper.find('#greeting').text()).toBe(SPANISH_GREETING);
});
test('should not alter other state values', () => {
const wrapper = shallow(<App/>);
const incButton = wrapper.find('#increment');
const langButton = wrapper.find('#language');
incButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('1');
expect(wrapper.find('#greeting').text()).toBe(ENGLISH_GREETING);
incButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('2');
expect(wrapper.find('#greeting').text()).toBe(ENGLISH_GREETING);
langButton.simulate('click');
expect(wrapper.find('#count').text()).toBe('2');
expect(wrapper.find('#greeting').text()).toBe(SPANISH_GREETING);
});
const InvalidApp = () => {
const [state, setState] = useSetState(3);
return null;
};
const ValidApp = () => {
const [state, setState] = useSetState(null);
return null;
};
test('state should be plain object or null', () => {
expect(() => shallow(<InvalidApp />)).toThrow();
expect(() => shallow(<App />)).not.toThrow();
expect(() => shallow(<ValidApp />)).not.toThrow();
});
const App2 = ({ callback }) => {
const [state, setState] = useSetState({ error: false });
return (
<button
id="btn"
onClick={() => setState({ error: true }, callback)}
/>
);
};
test('should invoke callback', () => {
const mock = jest.fn();
// full DOM rendering to enable effects
const wrapper = mount(<App2 callback={mock} />);
expect(mock).not.toHaveBeenCalled();
wrapper.find('#btn').simulate('click');
expect(mock).toHaveBeenCalled();
expect(mock).toHaveBeenCalledTimes(1);
});
});