-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
347 lines (279 loc) · 9.48 KB
/
index.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* External dependencies
*/
import { render } from '@testing-library/react';
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import useMergeRefs from '../';
describe( 'useMergeRefs', () => {
// Setup
// =====
//
// A component with two merged ref callbacks. The second has a dependency,
// the first does not. We expect the one with the dependency to be called
// with null in the old function and the node in the new function. We don't
// expect the first ref callback to be called **unless** the node changes.
// There's a prop controlling the tag name, which can be used to trigger a
// node change. In that case we expect both ref callbacks to be called with
// null in the old function and the **new** node in the new function.
//
// The history of all functions is recorded. Note that a new function is
// created on every render, which will all be tracked. Some functions are
// never expected to be called on subsequent renders if no callback
// dependency updates!
function renderCallback( args ) {
renderCallback.history.push( args );
}
renderCallback.history = [];
function MergedRefs( {
count,
tagName: TagName = 'div',
disable1,
disable2,
unused,
} ) {
function refCallback1( value ) {
refCallback1.history.push( value );
}
refCallback1.history = [];
function refCallback2( value ) {
refCallback2.history.push( value );
}
refCallback2.history = [];
renderCallback( [ refCallback1.history, refCallback2.history ] );
const ref1 = useCallback( refCallback1, [] );
const ref2 = useCallback( refCallback2, [ count ] );
const mergedRefs = useMergeRefs( [
! disable1 && ref1,
! disable2 && ref2,
] );
if ( unused ) {
return <TagName ref={ ref1 } />;
}
return <TagName ref={ mergedRefs } />;
}
afterEach( () => {
// Reset all history.
renderCallback.history = [];
} );
it( 'should work', () => {
const { container, rerender } = render( <MergedRefs /> );
const originalElement = container.firstElementChild;
// Render 1: both initial callback functions should be called with node.
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement ] ],
] );
rerender( <MergedRefs /> );
// Render 2: the new callback functions should not be called! There has
// been no dependency change.
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement ] ],
[ [], [] ],
] );
rerender( null );
// Unmount: the initial callback functions should receive null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null ],
[ originalElement, null ],
],
[ [], [] ],
] );
} );
it( 'should work for node change', () => {
const { container, rerender } = render( <MergedRefs /> );
const originalElement = container.firstElementChild;
rerender( <MergedRefs tagName="button" /> );
const newElement = container.firstElementChild;
// After a render with the original element and a second render with the
// new element, expect the initial callback functions to be called with
// the original element, then null, then the new element.
// Again, the new callback functions should not be called! There has
// been no dependency change.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement ],
[ originalElement, null, newElement ],
],
[ [], [] ],
] );
rerender( null );
// Unmount: the initial callback functions should receive null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement, null ],
[ originalElement, null, newElement, null ],
],
[ [], [] ],
] );
} );
it( 'should work with dependencies', () => {
const { container, rerender } = render( <MergedRefs count={ 1 } /> );
const originalElement = container.firstElementChild;
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement ] ],
] );
rerender( <MergedRefs count={ 2 } /> );
// After a second render with a dependency change, expect the inital
// callback function to be called with null and the new callback
// function to be called with the original node. Note that for callback
// one no dependencies have changed.
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement, null ] ],
[ [], [ originalElement ] ],
] );
rerender( null );
// Unmount: current callback functions should be called with null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null ],
[ originalElement, null ],
],
[ [], [ originalElement, null ] ],
] );
} );
it( 'should simultaneously update node and dependencies', () => {
const { container, rerender } = render( <MergedRefs count={ 1 } /> );
const originalElement = container.firstElementChild;
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement ] ],
] );
rerender( <MergedRefs count={ 2 } tagName="button" /> );
const newElement = container.firstElementChild;
// Both the node changes and the dependencies update for the second
// callback, so expect the old callback function to be called with null
// and the new callback function to be called with the **new** node.
// For the first callback, we expect the initial function to be called
// with null and then the new node since no dependencies have changed.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement ],
[ originalElement, null ],
],
[ [], [ newElement ] ],
] );
rerender( null );
// Unmount: current callback functions should be called with null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement, null ],
[ originalElement, null ],
],
[ [], [ newElement, null ] ],
] );
} );
it( 'should work for dependency change after node change', () => {
const { container, rerender } = render( <MergedRefs /> );
const originalElement = container.firstElementChild;
rerender( <MergedRefs tagName="button" /> );
const newElement = container.firstElementChild;
// After a render with the original element and a second render with the
// new element, expect the initial callback functions to be called with
// the original element, then null, then the new element.
// Again, the new callback functions should not be called! There has
// been no dependency change.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement ],
[ originalElement, null, newElement ],
],
[ [], [] ],
] );
rerender( <MergedRefs tagName="button" count={ 1 } /> );
// After a third render with a dependency change, expect the inital
// callback function to be called with null and the new callback
// function to be called with the new element. Note that for callback
// one no dependencies have changed.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement ],
[ originalElement, null, newElement, null ],
],
[ [], [] ],
[ [], [ newElement ] ],
] );
rerender( null );
// Unmount: current callback functions should be called with null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null, newElement, null ],
[ originalElement, null, newElement, null ],
],
[ [], [] ],
[ [], [ newElement, null ] ],
] );
} );
it( 'should allow disabling a ref', () => {
const { container, rerender } = render( <MergedRefs disable1 /> );
const originalElement = container.firstElementChild;
// Render 1: ref 1 should be disabled.
expect( renderCallback.history ).toEqual( [
[ [], [ originalElement ] ],
] );
rerender( <MergedRefs disable2 /> );
// Render 2: ref 1 should be enabled and receive the ref. Note that the
// callback hasn't changed, so the original callback function will be
// called. Ref 2 should be disabled, so called with null.
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [ originalElement, null ] ],
[ [], [] ],
] );
rerender( <MergedRefs disable1 count={ 1 } /> );
// Render 3: ref 1 should again be disabled. Ref 2 to should receive a
// ref with the new callback function because the count has been
// changed.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null ],
[ originalElement, null ],
],
[ [], [] ],
[ [], [ originalElement ] ],
] );
rerender( null );
// Unmount: current callback functions should receive null.
expect( renderCallback.history ).toEqual( [
[
[ originalElement, null ],
[ originalElement, null ],
],
[ [], [] ],
[ [], [ originalElement, null ] ],
] );
} );
it( 'should allow the hook being unused', () => {
const { container, rerender } = render( <MergedRefs unused /> );
const originalElement = container.firstElementChild;
// Render 1: ref 1 should updated, ref 2 should not.
expect( renderCallback.history ).toEqual( [
[ [ originalElement ], [] ],
] );
rerender( <MergedRefs /> );
// Render 2: ref 2 should be updated as well.
expect( renderCallback.history ).toEqual( [
[ [ originalElement, null, originalElement ], [ originalElement ] ],
[ [], [] ],
] );
rerender( <MergedRefs unused /> );
// Render 3: ref 2 should be updated with null
expect( renderCallback.history ).toEqual( [
[
[
originalElement,
null,
originalElement,
null,
originalElement,
],
[ originalElement, null ],
],
[ [], [] ],
[ [], [] ],
] );
} );
} );