-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathintegration.test.jsx
302 lines (251 loc) · 11.1 KB
/
integration.test.jsx
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import React from 'react';
import { act, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { clearLog, resetInstanceIdCounters } from '../src';
import { Wrapper } from './Wrapper';
import TracedChild from './TracedChild';
import TracedLegacyChild from './TracedLegacyChild';
import TracedLegacyUnsafeChild from './TracedLegacyUnsafeChild';
const nNewLifecyclePanelMethods = 9; // Non-legacy panel has 9 lifecycle methods
const nLegacyLifecyclePanelMethods = 10; // Legacy panel has 10 lifecycle methods
// Return array of length `n` which is 'true' at index `i` and 'false' everywhere else.
const booleanStringListOnlyTrueAt = (n, i) => Array.from({length: n}, (_undefined, ix) => `${ix === i}`);
const formatLogEntries = (instanceName, logMethods) => logMethods.map((e, i) =>
('' + i).padStart(2) + ` ${instanceName}: ` + e // NOTE: padding assumes <=100 entries
);
const setupUser = () => userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
describe('traceLifecycle', () => {
it('preserves static properties', () => {
expect(TracedChild.staticProperty).toBe('a static property');
});
});
describe('LifecyclePanel', () => {
it('shows which methods are implemented', () => {
render(<Wrapper renderChild={() => <TracedChild/>}/>);
const methods = [...screen.getByTestId('lifecycle-panel').querySelectorAll('.lifecycle-method')];
methods.forEach((node) => {
expect(node).toHaveAttribute('data-is-implemented', 'true');
});
});
it('shows new methods for non-legacy component', () => {
render(<Wrapper renderChild={() => <TracedChild/>}/>);
const methods = [...screen.getByTestId('lifecycle-panel').querySelectorAll('.lifecycle-method')];
expect(methods).toHaveLength(nNewLifecyclePanelMethods);
});
it('shows legacy methods for legacy component', () => {
/* eslint-disable no-console */
// Disable console.warn to suppress React warnings about using legacy methods (emitted once per method).
const consoleWarn = console.warn;
console.warn = () => {};
render(<Wrapper renderChild={() => <TracedLegacyChild/>}/>);
console.warn = consoleWarn;
/* eslint-enable no-console */
const methods = [...screen.getByTestId('lifecycle-panel').querySelectorAll('.lifecycle-method')];
expect(methods).toHaveLength(nLegacyLifecyclePanelMethods);
});
});
describe('Log', () => {
it('sequentially highlights log entries', () => {
render(<Wrapper renderChild={() => <TracedChild/>}/>);
act(() => jest.runOnlyPendingTimers()); // log entries are generated asynchronously, so run timers once
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
const nLogEntries = entries.length;
expect(nLogEntries).toBeGreaterThan(0);
for (let i = 0; i < nLogEntries; i++) {
expect(entries.map((node) => node.getAttribute('data-is-highlighted'))).toEqual(
booleanStringListOnlyTrueAt(nLogEntries, i)
);
act(() => jest.runOnlyPendingTimers()); // not necessary for last iteration, but harmless
}
});
it('highlights the corresponding panel method', async () => {
const user = setupUser();
render(<Wrapper renderChild={() => <TracedChild/>}/>);
const logEntries = within(screen.getByTestId('log-entries'));
const panel = within(screen.getByTestId('lifecycle-panel'));
act(() => jest.runOnlyPendingTimers()); // log entries are generated asynchronously, so run timers once
expect(panel.getByText('render')).toHaveAttribute('data-is-highlighted', 'false');
await user.hover(logEntries.getByText('4 Child-1: render'));
expect(panel.getByText('render')).toHaveAttribute('data-is-highlighted', 'true');
expect(panel.getByText('constructor')).toHaveAttribute('data-is-highlighted', 'false');
await user.hover(logEntries.getByText('0 Child-1: constructor'));
expect(panel.getByText('constructor')).toHaveAttribute('data-is-highlighted', 'true');
});
it('logs all new lifecycle methods', async () => {
const user = setupUser();
render(<Wrapper renderChild={({prop}) => <TracedChild prop={prop}/>}/>); // Mount TracedChild
await user.click(screen.getByTestId('prop-value-checkbox')); // Update TracedChild prop
await user.click(screen.getByTestId('state-update-button')); // Update TracedChild state
await user.click(screen.getByTestId('show-child-checkbox')); // Unmount TracedChild
act(() => jest.runOnlyPendingTimers());
const expectedLogEntries = [
// Mount TracedChild
'constructor',
'custom:constructor',
'static getDerivedStateFromProps',
'custom:getDerivedStateFromProps',
'render',
'custom:render',
'componentDidMount',
'custom:componentDidMount',
// Update TracedChild prop
'static getDerivedStateFromProps',
'custom:getDerivedStateFromProps',
'shouldComponentUpdate',
'custom:shouldComponentUpdate',
'render',
'custom:render',
'getSnapshotBeforeUpdate',
'custom:getSnapshotBeforeUpdate',
'componentDidUpdate',
'custom:componentDidUpdate',
// Update TracedChild state
'setState',
'setState:update fn',
'custom:setState update fn',
'static getDerivedStateFromProps',
'custom:getDerivedStateFromProps',
'shouldComponentUpdate',
'custom:shouldComponentUpdate',
'render',
'custom:render',
'getSnapshotBeforeUpdate',
'custom:getSnapshotBeforeUpdate',
'componentDidUpdate',
'custom:componentDidUpdate',
'setState:callback',
'custom:setState callback',
// Unmount TracedChild
'componentWillUnmount',
'custom:componentWillUnmount',
];
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries.map((node) => node.textContent))
.toEqual(formatLogEntries('Child-1', expectedLogEntries)
);
});
it('logs all legacy lifecycle methods', async () => {
const user = setupUser();
/* eslint-disable no-console */
// Disable console.warn to suppress React warnings about using legacy methods.
const consoleWarn = console.warn;
console.warn = () => {};
render(<Wrapper renderChild={({prop}) => <TracedLegacyChild prop={prop}/>}/>); // Mount TracedLegacyChild
console.warn = consoleWarn;
/* eslint-enable no-console */
await user.click(screen.getByTestId('prop-value-checkbox')); // Update TracedLegacyChild prop
await user.click(screen.getByTestId('state-update-button')); // Update TracedLegacyChild state
await user.click(screen.getByTestId('show-child-checkbox')); // Unmount TracedLegacyChild
act(() => jest.runOnlyPendingTimers());
const expectedLogEntries = [
// Mount TracedLegacyChild
'constructor',
'custom:constructor',
'componentWillMount',
'custom:componentWillMount',
'render',
'custom:render',
'componentDidMount',
'custom:componentDidMount',
// Update TracedLegacyChild prop
'componentWillReceiveProps',
'custom:componentWillReceiveProps',
'shouldComponentUpdate',
'custom:shouldComponentUpdate',
'componentWillUpdate',
'custom:componentWillUpdate',
'render',
'custom:render',
'componentDidUpdate',
'custom:componentDidUpdate',
// Update TracedLegacyChild state
'setState',
'setState:update fn',
'custom:setState update fn',
'shouldComponentUpdate',
'custom:shouldComponentUpdate',
'componentWillUpdate',
'custom:componentWillUpdate',
'render',
'custom:render',
'componentDidUpdate',
'custom:componentDidUpdate',
'setState:callback',
'custom:setState callback',
// Unmount TracedLegacyChild
'componentWillUnmount',
'custom:componentWillUnmount',
];
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries.map((node) => node.textContent))
.toEqual(formatLogEntries('LegacyChild-1', expectedLogEntries)
);
});
it('logs all legacy UNSAFE_ lifecycle methods', async () => {
const user = setupUser();
// Mount TracedLegacyUnsafeChild
render(<Wrapper renderChild={({prop}) => <TracedLegacyUnsafeChild prop={prop}/>}/>);
await user.click(screen.getByTestId('prop-value-checkbox')); // Update TracedLegacyUnsafeChild prop
act(() => jest.runOnlyPendingTimers());
const expectedLogEntries = [
// Mount TracedLegacyUnsafeChild
'constructor',
'componentWillMount',
'custom:UNSAFE_componentWillMount',
'render',
'componentDidMount',
// Update TracedLegacyUnsafeChild prop
'componentWillReceiveProps',
'custom:UNSAFE_componentWillReceiveProps',
'shouldComponentUpdate',
'componentWillUpdate',
'custom:UNSAFE_componentWillUpdate',
'render',
'componentDidUpdate',
];
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries.map((node) => node.textContent))
.toEqual(formatLogEntries('LegacyUnsafeChild-1', expectedLogEntries)
);
});
it('is cleared by clearLog()', () => {
render(<Wrapper renderChild={() => <TracedChild/>}/>);
act(() => jest.runOnlyPendingTimers());
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries).not.toHaveLength(0);
act(() => clearLog());
const clearedEntries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(clearedEntries).toHaveLength(0);
});
});
describe('instanceId counter', () => {
it('starts at 1', () => {
render(<Wrapper renderChild={() => <TracedChild/>}/>);
act(() => jest.runOnlyPendingTimers());
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries[0]).toHaveTextContent(/^ ?\d+ Child-1/);
});
it('increments on remount', async () => {
const user = setupUser();
render(<Wrapper renderChild={() => <TracedChild/>}/>); // Mount TracedChild
await user.click(screen.getByTestId('show-child-checkbox')); // Unmount TracedChild
act(() => jest.runOnlyPendingTimers());
act(() => clearLog());
await user.click(screen.getByTestId('show-child-checkbox')); // Mount TracedChild
act(() => jest.runOnlyPendingTimers());
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries[0]).toHaveTextContent(/^ ?\d+ Child-2/);
});
it('is reset by resetInstanceIdCounters', async () => {
const user = setupUser();
render(<Wrapper renderChild={() => <TracedChild/>}/>); // Mount TracedChild
await user.click(screen.getByTestId('show-child-checkbox')); // Unmount TracedChild
act(() => jest.runOnlyPendingTimers());
act(() => clearLog());
resetInstanceIdCounters();
await user.click(screen.getByTestId('show-child-checkbox')); // Mount TracedChild
act(() => jest.runOnlyPendingTimers());
const entries = [...screen.getByTestId('log-entries').querySelectorAll('.entry')];
expect(entries[0]).toHaveTextContent(/^ ?\d+ Child-1/);
});
});