-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
forwardRef.test.js
347 lines (261 loc) · 8.7 KB
/
forwardRef.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
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
import { createElement as h, render, createRef, forwardRef, hydrate, memo, useState, useImperativeHandle } from '../../src';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { setupRerender } from 'preact/test-utils';
/* eslint-disable react/jsx-boolean-value, react/display-name, prefer-arrow-callback */
/** @jsx h */
describe('forwardRef', () => {
/** @type {HTMLDivElement} */
let scratch, rerender;
beforeEach(() => {
scratch = setupScratch();
rerender = setupRerender();
});
afterEach(() => {
teardown(scratch);
});
it('should have isReactComponent flag', () => {
let App = forwardRef((_, ref) => <div ref={ref}>foo</div>);
expect(App.prototype.isReactComponent).to.equal(true);
});
it('should pass ref with createRef', () => {
let App = forwardRef((_, ref) => <div ref={ref}>foo</div>);
let ref = createRef();
render(<App ref={ref} />, scratch);
expect(ref.current).to.equalNode(scratch.firstChild);
});
it('should share the same ref reference', () => {
let passedRef;
let App = forwardRef((_, ref) => {
passedRef = ref;
return <div ref={ref}>foo</div>;
});
let ref = createRef();
render(<App ref={ref} />, scratch);
expect(ref).to.equal(passedRef);
});
it('should pass ref with a callback', () => {
let App = forwardRef((_, ref) => <div><span ref={ref}>foo</span></div>);
let ref;
render(<App ref={x => (ref = x)} />, scratch);
expect(ref).to.equalNode(scratch.firstChild.firstChild);
});
it('should forward props', () => {
let spy = sinon.spy();
let App = forwardRef(spy);
render(<App foo="bar" />, scratch);
expect(spy).to.be.calledWithMatch({ foo: 'bar' });
});
it('should support nesting', () => {
let passedRef;
let Inner = forwardRef((_, ref) => {
passedRef = ref;
return <div ref={ref}>inner</div>;
});
let App = forwardRef((_, ref) => <Inner ref={ref} />);
let ref = createRef();
render(<App ref={ref} />, scratch);
expect(ref).to.equal(passedRef);
});
it('should forward null on unmount', () => {
let passedRef;
let App = forwardRef((_, ref) => {
passedRef = ref;
return <div ref={ref}>foo</div>;
});
let ref = createRef();
render(<App ref={ref} />, scratch);
render(null, scratch);
expect(passedRef.current).to.equal(null);
});
it('should be able to render and hydrate forwardRef components', () => {
const Foo = ({ label, forwardedRef }) => (
<div ref={forwardedRef}>{label}</div>
);
const App = forwardRef((props, ref) => (
<Foo {...props} forwardedRef={ref} />
));
const ref = createRef();
const markup = <App ref={ref} label="Hi" />;
const element = document.createElement('div');
element.innerHTML = '<div>Hi</div>';
expect(element.textContent).to.equal('Hi');
expect(ref.current==null).to.equal(true);
hydrate(markup, element);
expect(element.textContent).to.equal('Hi');
expect(ref.current.tagName).to.equal('DIV');
});
it('should update refs when switching between children', () => {
function Foo({ forwardedRef, setRefOnDiv }) {
return (
<section>
<div ref={setRefOnDiv ? forwardedRef : null}>First</div>
<span ref={setRefOnDiv ? null : forwardedRef}>Second</span>
</section>
);
}
const App = forwardRef((props, ref) => (
<Foo {...props} forwardedRef={ref} />
));
const ref = createRef();
render(<App ref={ref} setRefOnDiv={true} />, scratch);
expect(ref.current.nodeName).to.equal('DIV');
render(<App ref={ref} setRefOnDiv={false} />, scratch);
expect(ref.current.nodeName).to.equal('SPAN');
});
it('should support rendering null', () => {
const App = forwardRef(() => null);
const ref = createRef();
render(<App ref={ref} />, scratch);
expect(ref.current==null).to.equal(true);
});
it('should support rendering null for multiple children', () => {
const Foo = forwardRef(() => null);
const ref = createRef();
render(
<div>
<div />
<Foo ref={ref} />
<div />
</div>,
scratch
);
expect(ref.current==null).to.equal(true);
});
it('should support useImperativeHandle', () => {
let setValue;
const Foo = forwardRef((props, ref) => {
const result = useState('');
setValue = result[1];
useImperativeHandle(ref, () => ({
getValue: () => result[0]
}), [result[0]]);
return (
<input ref={ref} value={result[0]} />
);
});
const ref = createRef();
render(<Foo ref={ref} />, scratch);
expect(typeof ref.current.getValue).to.equal('function');
expect(ref.current.getValue()).to.equal('');
setValue('x');
rerender();
expect(typeof ref.current.getValue).to.equal('function');
expect(ref.current.getValue()).to.equal('x');
});
it('should not bailout if forwardRef is not wrapped in memo', () => {
const Component = props => <div {...props} />;
let renderCount = 0;
const App = forwardRef((props, ref) => {
renderCount++;
return <Component {...props} forwardedRef={ref} />;
});
const ref = createRef();
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(1);
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(2);
});
it('should bailout if forwardRef is wrapped in memo', () => {
const Component = props => <div ref={props.forwardedRef} />;
let renderCount = 0;
const App = memo(
forwardRef((props, ref) => {
renderCount++;
return <Component {...props} forwardedRef={ref} />;
}),
);
const ref = createRef();
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(1);
expect(ref.current.nodeName).to.equal('DIV');
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(1);
const differentRef = createRef();
render(
<App ref={differentRef} optional="foo" />,
scratch
);
expect(renderCount).to.equal(2);
expect(ref.current==null).to.equal(true);
expect(differentRef.current.nodeName).to.equal('DIV');
render(<App ref={ref} optional="bar" />, scratch);
expect(renderCount).to.equal(3);
});
it('should bailout if forwardRef is wrapped in memo using function refs', () => {
const Component = props => <div ref={props.forwardedRef} />;
let renderCount = 0;
const App = memo(
forwardRef((props, ref) => {
renderCount++;
return <Component {...props} forwardedRef={ref} />;
}),
);
const ref = sinon.spy();
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(1);
expect(ref).to.have.been.called;
ref.resetHistory();
render(<App ref={ref} optional="foo" />, scratch);
expect(renderCount).to.equal(1);
const differentRef = sinon.spy();
render(<App ref={differentRef} optional="foo" />, scratch);
expect(renderCount).to.equal(2);
expect(ref).to.have.been.calledWith(null);
expect(differentRef).to.have.been.called;
differentRef.resetHistory();
render(<App ref={ref} optional="bar" />, scratch);
expect(renderCount).to.equal(3);
});
it('should pass ref through memo() with custom comparer function', () => {
const Foo = props => <div ref={props.forwardedRef} />;
let renderCount = 0;
const App = memo(
forwardRef((props, ref) => {
renderCount++;
return <Foo {...props} forwardedRef={ref} />;
}),
(o, p) => o.a === p.a && o.b === p.b,
);
const ref = createRef();
render(<App ref={ref} a="0" b="0" c="1" />, scratch);
expect(renderCount).to.equal(1);
expect(ref.current.nodeName).to.equal('DIV');
// Changing either a or b rerenders
render(<App ref={ref} a="0" b="1" c="1" />, scratch);
expect(renderCount).to.equal(2);
// Changing c doesn't rerender
render(<App ref={ref} a="0" b="1" c="2" />, scratch);
expect(renderCount).to.equal(2);
const App2 = memo(
App,
(o, p) => o.a === p.a && o.c === p.c,
);
render(<App2 ref={ref} a="0" b="0" c="0" />, scratch);
expect(renderCount).to.equal(3);
// Changing just b no longer updates
render(<App2 ref={ref} a="0" b="1" c="0" />, scratch);
expect(renderCount).to.equal(3);
// Changing just a and c updates
render(<App2 ref={ref} a="2" b="2" c="2" />, scratch);
expect(renderCount).to.equal(4);
// Changing just c does not update
render(<App2 ref={ref} a="2" b="2" c="3" />, scratch);
expect(renderCount).to.equal(4);
// Changing ref still rerenders
const differentRef = createRef();
render(<App2 ref={differentRef} a="2" b="2" c="3" />, scratch);
expect(renderCount).to.equal(5);
expect(ref.current==null).to.equal(true);
expect(differentRef.current.nodeName).to.equal('DIV');
});
it('calls ref when this is a function.', () => {
const spy = sinon.spy();
const Bar = forwardRef((props, ref) => {
useImperativeHandle(ref, () => ({ foo: 100 }));
return null;
});
render(<Bar ref={spy} />, scratch);
expect(spy).to.be.calledOnce;
expect(spy).to.be.calledWithExactly({ foo: 100 });
});
});