-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
keys.test.js
495 lines (391 loc) · 13.8 KB
/
keys.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import { createElement as h, Component, render } from '../../src/index';
import { setupScratch, teardown } from '../_util/helpers';
import { logCall, clearLog, getLog } from '../_util/logCall';
/** @jsx h */
describe('keys', () => {
/** @type {HTMLDivElement} */
let scratch;
/** @type {string[]} */
let ops;
function createStateful(name) {
return class Stateful extends Component {
componentDidUpdate() {
ops.push(`Update ${name}`);
}
componentDidMount() {
ops.push(`Mount ${name}`);
}
componentWillUnmount() {
ops.push(`Unmount ${name}`);
}
render() {
return <div>{name}</div>;
}
};
}
/** @type {(props: {values: any[]}) => any} */
const List = props => (
<ol>
{props.values.map(value => <li key={value}>{value}</li>)}
</ol>
);
/**
* Move an element in an array from one index to another
* @param {any[]} values The array of values
* @param {number} from The index to move from
* @param {number} to The index to move to
*/
function move(values, from, to) {
const value = values[from];
values.splice(from, 1);
values.splice(to, 0, value);
}
before(() => {
logCall(Element.prototype, 'appendChild');
logCall(Element.prototype, 'insertBefore');
logCall(Element.prototype, 'removeChild');
logCall(Element.prototype, 'remove');
});
beforeEach(() => {
scratch = setupScratch();
ops = [];
});
afterEach(() => {
teardown(scratch);
clearLog();
});
// https://fb.me/react-special-props
it('should not pass key in props', () => {
const Foo = sinon.spy(() => null);
render(<Foo key="foo" />, scratch);
expect(Foo.args[0][0]).to.deep.equal({});
});
// See developit/preact-compat#21
it('should remove orphaned keyed nodes', () => {
render((
<div>
<div>1</div>
<li key="a">a</li>
<li key="b">b</li>
</div>
), scratch);
render((
<div>
<div>2</div>
<li key="b">b</li>
<li key="c">c</li>
</div>
), scratch);
expect(scratch.innerHTML).to.equal('<div><div>2</div><li>b</li><li>c</li></div>');
});
it('should remove keyed nodes (#232)', () => {
class App extends Component {
componentDidMount() {
setTimeout(() => this.setState({ opened: true,loading: true }), 10);
setTimeout(() => this.setState({ opened: true,loading: false }), 20);
}
render({ opened, loading }) {
return (
<BusyIndicator id="app" busy={loading}>
<div>This div needs to be here for this to break</div>
{ opened && !loading && <div>{[]}</div> }
</BusyIndicator>
);
}
}
class BusyIndicator extends Component {
render({ children, busy }) {
return (<div class={busy ? 'busy' : ''}>
{ children && children.length ? children : <div class="busy-placeholder" /> }
<div class="indicator">
<div>indicator</div>
<div>indicator</div>
<div>indicator</div>
</div>
</div>);
}
}
render(<App />, scratch);
render(<App opened loading />, scratch);
render(<App opened />, scratch);
const html = String(scratch.firstChild.innerHTML).replace(/ class=""/g, '');
expect(html).to.equal('<div>This div needs to be here for this to break</div><div></div><div class="indicator"><div>indicator</div><div>indicator</div><div>indicator</div></div>');
});
it('should append new keyed elements', () => {
const values = ['a', 'b'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('ab');
values.push('c');
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abc');
expect(getLog()).to.deep.equal([ '<li>.appendChild(#text)', '<ol>ab.appendChild(<li>c)' ]);
});
it('should remove keyed elements from the end', () => {
const values = ['a', 'b', 'c', 'd'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd');
values.pop();
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abc');
expect(getLog()).to.deep.equal([ '<li>d.remove()' ]);
});
it('should prepend keyed elements to the beginning', () => {
const values = ['b', 'c'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('bc');
values.unshift('a');
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abc');
expect(getLog()).to.deep.equal([ '<li>.appendChild(#text)', '<ol>bc.insertBefore(<li>a, <li>b)' ]);
});
it('should remove keyed elements from the beginning', () => {
const values = ['z', 'a', 'b', 'c'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('zabc');
values.shift();
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abc');
expect(getLog()).to.deep.equal([ '<li>z.remove()' ]);
});
it('should insert new keyed children in the middle', () => {
const values = ['a', 'c'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('ac');
values.splice(1, 0, 'b');
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abc');
expect(getLog()).to.deep.equal([ '<li>.appendChild(#text)', '<ol>ac.insertBefore(<li>b, <li>c)' ]);
});
it('should remove keyed children from the middle', () => {
const values = ['a', 'b', 'x', 'y', 'z', 'c', 'd'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abxyzcd');
values.splice(2, 3);
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd');
expect(getLog()).to.deep.equal([ '<li>z.remove()', '<li>y.remove()', '<li>x.remove()' ]);
});
it('should swap children efficiently', () => {
render(<List values={['a', 'b']} />, scratch);
expect(scratch.textContent).to.equal('ab');
clearLog();
render(<List values={['b', 'a']} />, scratch);
expect(scratch.textContent).to.equal('ba');
expect(getLog()).to.deep.equal([ '<ol>ab.appendChild(<li>a)' ]);
});
it('should swap existing keyed children in the middle of a list efficiently', () => {
const values = ['a', 'b', 'c', 'd'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd');
// swap
move(values, 1, 2);
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('acbd', 'initial swap');
expect(getLog()).to.deep.equal([ '<ol>abcd.insertBefore(<li>b, <li>d)' ], 'initial swap');
// swap back
move(values, 2, 1);
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd', 'swap back');
expect(getLog()).to.deep.equal([ '<ol>acbd.insertBefore(<li>c, <li>d)' ], 'swap back');
});
it('should move keyed children to the end of the list', () => {
const values = ['a', 'b', 'c', 'd'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd');
// move to end
move(values, 0, values.length - 1);
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('bcda', 'move to end');
expect(getLog()).to.deep.equal([ '<ol>abcd.appendChild(<li>a)' ], 'move to end');
// move to beginning
move(values, values.length - 1, 0);
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal('abcd', 'move to beginning');
expect(getLog()).to.deep.equal([ '<ol>bcda.insertBefore(<li>a, <li>b)' ], 'move to beginning');
});
it('should reverse children effectively', () => {
const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal(values.join(''));
// reverse list
values.reverse();
clearLog();
render(<List values={values} />, scratch);
expect(scratch.textContent).to.equal(values.join(''));
expect(getLog()).to.deep.equal([
'<ol>abcdefghij.insertBefore(<li>j, <li>a)',
'<ol>jabcdefghi.insertBefore(<li>i, <li>a)',
'<ol>jiabcdefgh.insertBefore(<li>h, <li>a)',
'<ol>jihabcdefg.insertBefore(<li>g, <li>a)',
'<ol>jihgabcdef.appendChild(<li>e)',
'<ol>jihgabcdfe.appendChild(<li>d)',
'<ol>jihgabcfed.appendChild(<li>c)',
'<ol>jihgabfedc.appendChild(<li>b)',
'<ol>jihgafedcb.appendChild(<li>a)'
]);
});
it('should not preserve state when a component\'s keys are different', () => {
const Stateful = createStateful('Stateful');
function Foo({ condition }) {
return condition ? <Stateful key="a" /> : <Stateful key="b" />;
}
ops = [];
render(<Foo condition />, scratch);
expect(ops).to.deep.equal(['Mount Stateful'], 'initial mount');
ops = [];
render(<Foo condition={false} />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful', 'Mount Stateful'], 'switching keys 1');
ops = [];
render(<Foo condition />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful', 'Mount Stateful'], 'switching keys 2');
});
it('should not preserve state between an unkeyed and keyed component', () => {
// React and Preact v8 behavior: https://codesandbox.io/s/57prmy5mx
const Stateful = createStateful('Stateful');
function Foo({ keyed }) {
return keyed ? <Stateful key="a" /> : <Stateful />;
}
ops = [];
render(<Foo keyed />, scratch);
expect(ops).to.deep.equal(['Mount Stateful'], 'initial mount with key');
ops = [];
render(<Foo keyed={false} />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful', 'Mount Stateful'], 'switching from keyed to unkeyed');
ops = [];
render(<Foo keyed />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful', 'Mount Stateful'], 'switching from unkeyed to keyed');
});
it('should not preserve state when keys change with multiple children', () => {
// React & Preact v8 behavior: https://codesandbox.io/s/8l3p6lz9kj
const Stateful1 = createStateful('Stateful1');
const Stateful2 = createStateful('Stateful2');
let Stateful1Ref;
let Stateful2Ref;
let Stateful1MovedRef;
let Stateful2MovedRef;
function Foo({ moved }) {
return moved ? (
<div>
<div>1</div>
<Stateful1 key="c" ref={c => Stateful1MovedRef = c} />
<div>2</div>
<Stateful2 key="d" ref={c => Stateful2MovedRef = c} />
</div>
) : (
<div>
<div>1</div>
<Stateful1 key="a" ref={c => Stateful1Ref = c} />
<div>2</div>
<Stateful2 key="b" ref={c => Stateful2Ref = c} />
</div>
);
}
ops = [];
render(<Foo moved={false} />, scratch);
expect(ops).to.deep.equal(['Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1Ref).to.exist;
expect(Stateful2Ref).to.exist;
ops = [];
render(<Foo moved />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful2', 'Unmount Stateful1', 'Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1MovedRef).to.not.equal(Stateful1Ref);
expect(Stateful2MovedRef).to.not.equal(Stateful2Ref);
ops = [];
render(<Foo moved={false} />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful2', 'Unmount Stateful1', 'Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1Ref).to.not.equal(Stateful1MovedRef);
expect(Stateful2Ref).to.not.equal(Stateful2MovedRef);
});
it('should preserve state when moving keyed children components', () => {
// React & Preact v8 behavior: https://codesandbox.io/s/8l3p6lz9kj
const Stateful1 = createStateful('Stateful1');
const Stateful2 = createStateful('Stateful2');
let Stateful1Ref;
let Stateful2Ref;
let Stateful1MovedRef;
let Stateful2MovedRef;
function Foo({ moved }) {
return moved ? (
<div>
<div>1</div>
<Stateful2 key="b" ref={c => Stateful2MovedRef = c} />
<div>2</div>
<Stateful1 key="a" ref={c => Stateful1MovedRef = c} />
</div>
) : (
<div>
<div>1</div>
<Stateful1 key="a" ref={c => Stateful1Ref = c} />
<div>2</div>
<Stateful2 key="b" ref={c => Stateful2Ref = c} />
</div>
);
}
ops = [];
render(<Foo moved={false} />, scratch);
expect(ops).to.deep.equal(['Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1Ref).to.exist;
expect(Stateful2Ref).to.exist;
ops = [];
render(<Foo moved />, scratch);
expect(ops).to.deep.equal(['Update Stateful2', 'Update Stateful1']);
expect(Stateful1MovedRef).to.equal(Stateful1Ref);
expect(Stateful2MovedRef).to.equal(Stateful2Ref);
ops = [];
render(<Foo moved={false} />, scratch);
expect(ops).to.deep.equal(['Update Stateful1', 'Update Stateful2']);
expect(Stateful1Ref).to.equal(Stateful1MovedRef);
expect(Stateful2Ref).to.equal(Stateful2MovedRef);
});
it('should not preserve state when switching between keyed and unkeyed components as children', () => {
// React & Preact v8 behavior: https://codesandbox.io/s/8l3p6lz9kj
const Stateful1 = createStateful('Stateful1');
const Stateful2 = createStateful('Stateful2');
let Stateful1Ref;
let Stateful2Ref;
let Stateful1MovedRef;
let Stateful2MovedRef;
function Foo({ unkeyed }) {
return unkeyed ? (
<div>
<div>1</div>
<Stateful1 ref={c => Stateful2MovedRef = c} />
<div>2</div>
<Stateful2 ref={c => Stateful1MovedRef = c} />
</div>
) : (
<div>
<div>1</div>
<Stateful1 key="a" ref={c => Stateful1Ref = c} />
<div>2</div>
<Stateful2 key="b" ref={c => Stateful2Ref = c} />
</div>
);
}
ops = [];
render(<Foo unkeyed={false} />, scratch);
expect(ops).to.deep.equal(['Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1Ref).to.exist;
expect(Stateful2Ref).to.exist;
ops = [];
render(<Foo unkeyed />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful2', 'Unmount Stateful1', 'Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1MovedRef).to.not.equal(Stateful1Ref);
expect(Stateful2MovedRef).to.not.equal(Stateful2Ref);
ops = [];
render(<Foo unkeyed={false} />, scratch);
expect(ops).to.deep.equal(['Unmount Stateful2', 'Unmount Stateful1', 'Mount Stateful2', 'Mount Stateful1']);
expect(Stateful1Ref).to.not.equal(Stateful1MovedRef);
expect(Stateful2Ref).to.not.equal(Stateful2MovedRef);
});
});