forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useEvent.test.ts
184 lines (168 loc) Β· 4.66 KB
/
useEvent.test.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { renderHook } from '@testing-library/react-hooks';
import useEvent, { ListenerType1, ListenerType2 } from '../src/useEvent';
interface Props {
name: string;
handler: (...args: any[]) => void;
target: ListenerType1 | ListenerType2;
options: any;
}
const propsList1 = [
{
name: 'name1',
handler: () => {},
target: {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
},
options: { a: 'opt1' },
},
{
name: 'name2',
handler: () => {},
target: {
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
},
options: { a: 'opt2' },
},
];
const propsList2 = [
{
...propsList1[0],
target: {
on: jest.fn(),
off: jest.fn(),
},
},
{
...propsList1[1],
target: {
on: jest.fn(),
off: jest.fn(),
},
},
];
it('should call addEventListener/removeEventListener on mount/unmount', () => {
checkOnMountAndUnmount(propsList1[0], 'addEventListener', 'removeEventListener');
});
it('should call on/off on mount/unmount', () => {
checkOnMountAndUnmount(propsList2[0], 'on', 'off');
});
it('should call addEventListener/removeEventListener on deps changes', () => {
checkOnDepsChanges(propsList1[0], propsList1[1], 'addEventListener', 'removeEventListener');
});
it('should call on/off on deps changes', () => {
checkOnDepsChanges(propsList2[0], propsList2[1], 'on', 'off');
});
const checkOnMountAndUnmount = (
props: Props,
addEventListenerName: string,
removeEventListenerName: string
) => {
const { unmount } = renderHook((p: Props) => useEvent(p.name, p.handler, p.target, p.options), {
initialProps: props,
});
expect(props.target[addEventListenerName]).toHaveBeenCalledTimes(1);
expect(props.target[addEventListenerName]).toHaveBeenLastCalledWith(
props.name,
props.handler,
props.options
);
unmount();
expect(props.target[removeEventListenerName]).toHaveBeenCalledTimes(1);
expect(props.target[removeEventListenerName]).toHaveBeenLastCalledWith(
props.name,
props.handler,
props.options
);
};
const checkOnDepsChanges = (
props1: Props,
props2: Props,
addEventListenerName: string,
removeEventListenerName: string
) => {
const { rerender } = renderHook((p: Props) => useEvent(p.name, p.handler, p.target, p.options), {
initialProps: props1,
});
expect(props1.target[addEventListenerName]).toHaveBeenCalledTimes(1);
expect(props1.target[addEventListenerName]).toHaveBeenLastCalledWith(
props1.name,
props1.handler,
props1.options
);
// deps are same as previous
rerender({
name: props1.name,
handler: props1.handler,
target: props1.target,
options: props1.options,
});
expect(props1.target[removeEventListenerName]).not.toHaveBeenCalled();
// name is different from previous
rerender({
name: props2.name,
handler: props1.handler,
target: props1.target,
options: props1.options,
});
expect(props1.target[removeEventListenerName]).toHaveBeenCalledTimes(1);
expect(props1.target[removeEventListenerName]).toHaveBeenLastCalledWith(
props1.name,
props1.handler,
props1.options
);
// handler is different from previous
rerender({
name: props2.name,
handler: props2.handler,
target: props1.target,
options: props1.options,
});
expect(props1.target[removeEventListenerName]).toHaveBeenCalledTimes(2);
expect(props1.target[removeEventListenerName]).toHaveBeenLastCalledWith(
props2.name,
props1.handler,
props1.options
);
// options contents is same as previous
rerender({
name: props2.name,
handler: props2.handler,
target: props1.target,
options: { a: 'opt1' },
});
expect(props1.target[removeEventListenerName]).toHaveBeenCalledTimes(2);
// options is different from previous
rerender({
name: props2.name,
handler: props2.handler,
target: props1.target,
options: props2.options,
});
expect(props1.target[removeEventListenerName]).toHaveBeenCalledTimes(3);
expect(props1.target[removeEventListenerName]).toHaveBeenLastCalledWith(
props2.name,
props2.handler,
props1.options
);
// target is different from previous
rerender({
name: props2.name,
handler: props2.handler,
target: props2.target,
options: props2.options,
});
expect(props1.target[removeEventListenerName]).toHaveBeenCalledTimes(4);
expect(props1.target[removeEventListenerName]).toHaveBeenLastCalledWith(
props2.name,
props2.handler,
props2.options
);
expect(props2.target[addEventListenerName]).toHaveBeenCalledTimes(1);
expect(props2.target[addEventListenerName]).toHaveBeenLastCalledWith(
props2.name,
props2.handler,
props2.options
);
};