/
animationHooks.spec.jsx
1320 lines (1202 loc) · 44.8 KB
/
animationHooks.spec.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
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Component, render } from 'inferno';
import { createElement } from 'inferno-create-element';
describe('animation hooks', () => {
let container;
beforeEach(function () {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(function () {
render(null, container);
container.innerHTML = '';
document.body.removeChild(container);
});
/**
* This is how component animation hooks work:
*
* On node creation, if there is a callback, a reference to the DOM-node is passed to that callback which allows for CSS-
* animations to be applied.
*
* On node removal, if there is a callback, the node isn't actually removed until the callback has finished.
*
* On node move, if there is a callback, the original node is cloned and both a reference to the cloned node and the new
* node are passed to the callback allowing CSS-animations to be performed.
*
* ** Entrypoints **
* - mounting.ts
* - unmounnting.ts
*
* ** Scope **
* We will start by implementing this for class components. Animations are expensive so it is probably a reasonable
* tradeoff to force the use of class componnents.
*
* QUESTION: How do we handle nested animations?
* Normally we only want to animated the outermost animation, but there are situation when the dev might want to
* do this differenty. Should we:
* - always block animations in children?
* - allow the dev to specify?
*
* Ex. you have a page animation and also animations on items in that page.
* ANSWER: I will block all animations down stream for starters. Giving an option requires A LOT of thought on
* edge cases.
*
* QUESTION: What if we add a set of siblings in a list, then all of them should animate, no?
* DONE: Investigate how to solve this.
* ANSWER: Animations should be a tree and only the highest node animates, but all siblings will animate
*
* DONE: Fix code path when removing last item in a list using clearDOM(dom) optmisation
* DONE: The callback is lost when transition is completed by the timeout
* QUESTION: Should I require the component to have a key?
* ANSWER: No, they should only be required in lists.
* DONE: What happens if order of DOM is changed during animation?
* DONE: remove intermediate elements during animation in list
* DONE: randomly reassign keys in a list (before/during animation)
* DONE: randomly sort list during animation
* DONE: shuffle keys
* DONE: The keyed patching appears to add children despite existing nodes with same key
* DONE: When rerendering a list 1,3,4,5,6 to 1,2,3,4,5 there are issues in the patching algorithm where 6 is left in DOM
* STARTED: Compare with tests in patchKeyedChildren.spec.js
* TODO: Investigate adding animations in https://github.com/infernojs/inferno/tree/master/docs/uibench-reactlike
* TODO: Add an animation blocking parent component to example
*
*/
it('should NOT call "componentWillMove" when component is inserted into DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillMove(parentVNode, parent, dom, props) {
spyer('willMove');
expect(parentVNode.type).toEqual(App);
expect(parent instanceof HTMLDivElement).toEqual(true);
expect(dom instanceof HTMLDivElement).toEqual(true);
expect(next instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
expect(spyer).toHaveBeenCalledTimes(1);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
});
it('should NOT call "componentWillMove" when component is removed from DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillMove(parentVNode, parent, dom, props) {
spyer('willMove');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
render(null, container);
expect(spyer).toHaveBeenCalledTimes(1);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
});
it('should call "componentWillMove" when component is about to be moved in DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillMove(parentVNode, parent, dom, props) {
spyer('willMove');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div>{this.props.children}</div>;
}
}
render(
<div>
<App key="1">1</App>
<App key="2">2</App>
</div>,
container
);
expect(container.textContent).toEqual('12');
render(
<div>
<App key="2">2</App>
<App key="1">1</App>
</div>,
container
);
expect(container.textContent).toEqual('21');
// Only the first element needs to perform a DOM move
// The second element is animated by parent checking positions before and after move
expect(spyer).toHaveBeenCalledTimes(3);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['willMove']);
});
it('should call "componentWillMove" when component is about to be moved in DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillMove(parentVNode, parent, dom, props) {
spyer('willMove');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div>{this.props.children}</div>;
}
}
render(
<div>
<App key="1">1</App>
<App key="2">2</App>
<App key="3">3</App>
</div>,
container
);
expect(container.textContent).toEqual('123');
render(
<div>
<App key="2">2</App>
<App key="3">3</App>
<App key="1">1</App>
</div>,
container
);
expect(container.textContent).toEqual('231');
// Only the first element needs to perform a DOM move
// The second element is animated by parent checking positions before and after move
expect(spyer).toHaveBeenCalledTimes(4);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['didMount']);
expect(spyer.calls.argsFor(3)).toEqual(['willMove']);
});
it('should call "componentWillMove" when component is about to be moved in DOM', () => {
const spyer = jasmine.createSpy();
let parentDom;
class App extends Component {
componentWillMove(parentVNode, parent, dom, props) {
spyer('willMove');
expect(dom instanceof HTMLDivElement).toEqual(true);
parentDom = parentDom || parent;
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div>{this.props.children}</div>;
}
}
render(
<div>
<App key="1">1</App>
<App key="2">2</App>
<App key="3">3</App>
<App key="4">4</App>
</div>,
container
);
expect(container.textContent).toEqual('1234');
render(
<div>
<App key="4">4</App>
<App key="3">3</App>
<App key="2">2</App>
<App key="1">1</App>
</div>,
container
);
expect(spyer).toHaveBeenCalledTimes(7);
// Three elements need to perform a DOM move
// The fourth element is animated by parent checking positions before and after move
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['didMount']);
expect(spyer.calls.argsFor(3)).toEqual(['didMount']);
expect(spyer.calls.argsFor(4)).toEqual(['willMove']);
expect(spyer.calls.argsFor(5)).toEqual(['willMove']);
expect(spyer.calls.argsFor(6)).toEqual(['willMove']);
expect(container.textContent).toEqual('4321');
});
it('should call "componentDidAppear" when component has been inserted into DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentDidAppear(dom) {
spyer('didAppear');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
expect(spyer).toHaveBeenCalledTimes(2);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didAppear']);
});
it('should only call parent "componentDidAppear" when component tree has been inserted into DOM', () => {
const spyer = jasmine.createSpy();
class Child extends Component {
componentDidAppear() {
spyer('no-op');
}
componentDidMount() {
spyer('childDidMount');
}
render() {
return <div />;
}
}
class App extends Component {
componentDidAppear(dom) {
spyer('didAppear');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentDidMount() {
spyer('didMount');
}
render() {
return (
<div>
<Child />
</div>
);
}
}
render(<App />, container);
expect(spyer).toHaveBeenCalledTimes(3);
expect(spyer.calls.argsFor(0)).toEqual(['childDidMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['didAppear']);
});
it('should only call "componentDidAppear" when child component has been inserted into DOM', (done) => {
const spyer = jasmine.createSpy();
class Child extends Component {
componentDidAppear() {
spyer('childDidAppear');
}
componentDidMount() {
spyer('childDidMount');
}
render() {
return <div>{this.props.children}</div>;
}
}
class App extends Component {
constructor() {
super(...arguments);
this.state = {
items: [1]
};
}
componentDidMount() {
spyer('didMount');
setTimeout(() => {
this.setState({
items: [1, 2]
});
// Make sure inferno is done and then check the results
setTimeout(finished, 5);
}, 5);
}
render() {
return (
<div>
{this.state.items.map((i) => (
<Child key={i}>{i}</Child>
))}
</div>
);
}
}
render(<App />, container);
const finished = () => {
expect(spyer).toHaveBeenCalledTimes(5);
expect(spyer.calls.argsFor(0)).toEqual(['childDidMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['childDidAppear']);
expect(spyer.calls.argsFor(3)).toEqual(['childDidMount']);
expect(spyer.calls.argsFor(4)).toEqual(['childDidAppear']);
expect(container.innerHTML).toEqual('<div><div>1</div><div>2</div></div>');
done();
};
});
it('should call all "componentDidAppear" when multiple siblings have been inserted into DOM', () => {
const spyer = jasmine.createSpy();
class Child extends Component {
componentDidAppear(dom) {
spyer('childDidAppear');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
render() {
return <div />;
}
}
class App extends Component {
componentDidMount() {
spyer('didMount');
}
render() {
return (
<div>
<Child />
<Child />
<Child />
<Child />
</div>
);
}
}
render(<App />, container);
expect(spyer).toHaveBeenCalledTimes(5);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['childDidAppear']);
expect(spyer.calls.argsFor(2)).toEqual(['childDidAppear']);
expect(spyer.calls.argsFor(3)).toEqual(['childDidAppear']);
expect(spyer.calls.argsFor(4)).toEqual(['childDidAppear']);
});
it('should call "componentWillDisappear" when component is about to be removed from DOM', () => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillDisappear(dom, done) {
spyer('willDisappear');
expect(dom instanceof HTMLDivElement).toEqual(true);
expect(done instanceof Function).toEqual(true);
done();
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
render(null, container);
expect(spyer).toHaveBeenCalledTimes(2);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['willDisappear']);
});
it('should handle async callbacks from "componentWillDisappear"', (done) => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillDisappear(dom, callback) {
spyer('willDisappear');
expect(dom instanceof HTMLDivElement).toEqual(true);
expect(callback instanceof Function).toEqual(true);
setTimeout(() => {
callback();
setTimeout(() => didFinish(), 10);
}, 10);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
render(null, container);
function didFinish() {
expect(spyer).toHaveBeenCalledTimes(2);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['willDisappear']);
done();
}
});
it('should handle async callbacks "componentWillDisappear" when removing the two last elements in list', (done) => {
/**
* This test is hard to get to consistently fail. It should trigger
* clearDOM from last animation callback prior to deferComponentClassRemoval
* of at least one item. But it does work as expected now
* Change
* clearVNodeDOM(vNode, parentDOM, true);
* to
* clearVNodeDOM(vNode, parentDOM, false);
* in
* function deferComponentClassRemoval
* to force failure
*/
const spyer = jasmine.createSpy();
class App extends Component {
state = {
items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
};
render() {
if (this.state.items.length > 0) {
setTimeout(() => {
const items = this.state.items;
items.pop();
this.setState({ items });
});
}
if (this.state.items.length === 0) {
return <div />;
}
return (
<div>
{this.state.items.map((i) => (
<Item key={i} index={i} />
))}
</div>
);
}
}
class Item extends Component {
componentWillDisappear(dom, callback) {
spyer('willDisappear ' + this.props.index);
let timeout = 10;
if (this.props.index === 0) {
timeout = 0;
}
setTimeout(() => {
callback();
}, timeout);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
var checkRenderComplete_ONE = () => {
if (container.innerHTML !== '<div></div>') {
return setTimeout(checkRenderComplete_ONE, 10);
}
expect(spyer).toHaveBeenCalledTimes(40);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(19)).toEqual(['didMount']);
expect(spyer.calls.argsFor(20)).toEqual(['willDisappear 19']);
expect(spyer.calls.argsFor(39)).toEqual(['willDisappear 0']);
done();
};
checkRenderComplete_ONE();
});
it('should handle async callbacks from "componentWillDisappear" and mounting components with "componentDidAppear"', (done) => {
const spyer = jasmine.createSpy();
// Always call the componentWillDisappear callback after last render
let lastRenderDone = false;
let callMeAfterLastRender;
class App extends Component {
componentDidAppear(dom) {
spyer('didAppear');
expect(dom instanceof HTMLDivElement).toEqual(true);
}
componentWillDisappear(dom, callback) {
spyer('willDisappear');
expect(dom instanceof HTMLDivElement).toEqual(true);
expect(callback instanceof Function).toEqual(true);
if (this.props.forceDone) {
callback();
} else {
setTimeout(() => {
callMeAfterLastRender = () => {
callback();
setTimeout(() => didFinish(), 10);
};
lastRenderDone && callMeAfterLastRender();
}, 10);
}
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(<App />, container);
render(null, container);
// forceDone completes the willDissapear hook immediately
render(<App forceDone />, container);
expect(container.innerHTML).toBe('<div></div><div></div>');
lastRenderDone = true;
callMeAfterLastRender && callMeAfterLastRender();
function didFinish() {
expect(spyer).toHaveBeenCalledTimes(5);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didAppear']);
expect(spyer.calls.argsFor(2)).toEqual(['willDisappear']);
expect(spyer.calls.argsFor(3)).toEqual(['didMount']);
expect(spyer.calls.argsFor(4)).toEqual(['didAppear']);
expect(container.innerHTML).toBe('<div></div>');
render(null, container);
expect(container.innerHTML).toBe('');
done();
}
});
it('should handle async callbacks even when parent is removed during animation', (done) => {
const spyer = jasmine.createSpy();
class App extends Component {
componentWillDisappear(dom, callback) {
spyer('willDisappear');
expect(dom instanceof HTMLDivElement).toEqual(true);
expect(callback instanceof Function).toEqual(true);
setTimeout(() => {
callback();
}, 10);
}
componentDidMount() {
spyer('didMount');
}
render() {
return <div />;
}
}
render(
<div>
<App />
<App />
</div>,
container
);
render(
<div>
<App />
</div>,
container
);
render(null, container);
expect(container.innerHTML).not.toEqual('');
// Wait for all async operations to finish
var checkRenderComplete_ONE = () => {
if (container.innerHTML !== '') {
return setTimeout(checkRenderComplete_ONE, 10);
}
expect(spyer).toHaveBeenCalledTimes(6);
expect(spyer.calls.argsFor(0)).toEqual(['didMount']);
expect(spyer.calls.argsFor(1)).toEqual(['didMount']);
expect(spyer.calls.argsFor(2)).toEqual(['willDisappear']);
expect(spyer.calls.argsFor(3)).toEqual(['willDisappear']);
expect(spyer.calls.argsFor(4)).toEqual(['didMount']);
expect(spyer.calls.argsFor(5)).toEqual(['willDisappear']);
done();
};
checkRenderComplete_ONE();
});
const template = function (child) {
return createElement('div', null, child);
};
it('should add all nodes', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes([], spyer)), container);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
expect(container.textContent).toBe('#0#1#2#3');
expect(container.firstChild.childNodes.length).toBe(4);
//expect(spyer).toHaveBeenCalledTimes(4);
render(null, container);
expect(container.innerHTML).toBe('');
//expect(spyer).toHaveBeenCalledTimes(8);
});
it('should size up', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', '#1'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
expect(container.textContent).toBe('#0#1#2#3');
expect(container.firstChild.childNodes.length).toBe(4);
//expect(spyer).toHaveBeenCalledTimes(4);
render(null, container);
expect(container.innerHTML).toBe('');
// expect(spyer).toHaveBeenCalledTimes(8);
});
it('should do smiple size down', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', '#1', '#2'], spyer)), container);
render(template(generateKeyNodes(['#0', '#2'], spyer)), container);
expect(container.textContent).toBe('#0#2');
expect(container.firstChild.childNodes.length).toBe(2);
//expect(spyer).toHaveBeenCalledTimes(4);
render(null, container);
expect(container.innerHTML).toBe('');
// expect(spyer).toHaveBeenCalledTimes(8);
});
it('should size down', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1'], spyer)), container);
// expect(spyer).toHaveBeenCalledTimes(2);
expect(container.textContent).toBe('#0#1');
expect(container.firstChild.childNodes.length).toBe(2);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1'], spyer)), container);
//expect(spyer).toHaveBeenCalledTimes(10);
expect(container.textContent).toBe('#0#1');
expect(container.firstChild.childNodes.length).toBe(2);
render(null, container);
expect(container.innerHTML).toBe('');
//expect(spyer).toHaveBeenCalledTimes(12);
});
it('should handle multiple removes of siblings combined with adds', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', '#1', '#2', '#3', '#4', '#5'], spyer)), container);
render(template(generateKeyNodes(['#0', '#2', '#5', '#6'], spyer)), container);
// expect(spyer).toHaveBeenCalledTimes(2);
expect(container.textContent).toBe('#0#2#5#6');
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes(['#10', '#11'], spyer)), container);
//expect(spyer).toHaveBeenCalledTimes(10);
expect(container.textContent).toBe('#10#11');
expect(container.firstChild.childNodes.length).toBe(2);
render(null, container);
expect(container.innerHTML).toBe('');
//expect(spyer).toHaveBeenCalledTimes(12);
});
it('should clear all nodes', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes([], spyer)), container);
expect(container.textContent).toBe('');
expect(container.firstChild.childNodes.length).toBe(0);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes([], spyer)), container);
});
it('should work with mixed nodes', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['1', '#0', '#1', '#2'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3'], spyer)), container);
expect(container.textContent).toBe('#0#1#2#3');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move a key for start to end', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['a', '#0', '#1', '#2'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1', '#2', 'a'], spyer)), container);
expect(container.textContent).toBe('#0#1#2a');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move a key', () => {
const spyer = jasmine.createSpy();
render(template(generateKeyNodes(['#0', 'a', '#2', '#3'], spyer)), container);
render(template(generateKeyNodes(['#0', '#1', 'a', '#3'], spyer)), container);
expect(container.textContent).toBe('#0#1a#3');
expect(container.firstChild.childNodes.length).toBe(4);
});
/* Skipping spyer from here on */
it('should move a key', () => {
render(template(generateKeyNodes(['#0', 'a', '#2', '#3'])), container);
render(template(generateKeyNodes(['#0', '#1', 'a', '#3'])), container);
expect(container.textContent).toBe('#0#1a#3');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move a key with a size up', () => {
render(template(generateKeyNodes(['a', '#1', '#2', '#3'])), container);
render(template(generateKeyNodes(['#0', '#1', '#2', '#3', 'a', '#5'])), container);
expect(container.textContent).toBe('#0#1#2#3a#5');
expect(container.firstChild.childNodes.length).toBe(6);
});
it('should move a key with a size down', () => {
render(template(generateKeyNodes(['a', '#1', '#2', '#3'])), container);
render(template(generateKeyNodes(['#0', 'a', '#2'])), container);
expect(container.textContent).toBe('#0a#2');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should avoid unnecessary reordering', () => {
render(template(generateKeyNodes(['#0', 'a', '#2'])), container);
render(template(generateKeyNodes(['#0', 'a', '#2'])), container);
expect(container.textContent).toBe('#0a#2');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should work with keyed nodes', () => {
render(template(generateKeyNodes([0, 1, 2, 3, 4])), container);
render(template(generateKeyNodes([1, 2, 3, 4, 0])), container);
expect(container.textContent).toBe('12340');
expect(container.firstChild.childNodes.length).toBe(5);
render(template(generateKeyNodes([0, 1, 2, 3, 4])), container);
render(template(generateKeyNodes(['7', '4', '3', '2', '6', 'abc', 'def', '1'])), container);
expect(container.textContent).toBe('74326abcdef1');
expect(container.firstChild.childNodes.length).toBe(8);
render(template(generateKeyNodes(['#0', 'a', '#2'])), container);
expect(container.textContent).toBe('#0a#2');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should reorder keys', () => {
render(template(generateKeyNodes(['1', '2', '3', '4', 'abc', '6', 'def', '7'])), container);
render(template(generateKeyNodes(['7', '4', '3', '2', '6', 'abc', 'def', '1'])), container);
expect(container.textContent).toBe('74326abcdef1');
expect(container.firstChild.childNodes.length).toBe(8);
});
it('should remove one key at the start', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
render(template(generateKeyNodes(['b', 'c'])), container);
expect(container.textContent).toBe('bc');
expect(container.firstChild.childNodes.length).toBe(2);
});
it('should do a complex reverse', () => {
render(template(generateKeyNodes(['a', 'b', 'c', 'd'])), container);
render(template(generateKeyNodes(['d', 'c', 'b', 'a'])), container);
expect(container.textContent).toBe('dcba');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should remove two keys at the start', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
render(template(generateKeyNodes(['c'])), container);
expect(container.textContent).toBe('c');
expect(container.firstChild.childNodes.length).toBe(1);
});
it('should add one key to start', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
render(template(generateKeyNodes(['a', 'b'])), container);
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should add two key to start', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
render(template(generateKeyNodes(['c'])), container);
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should remove one key at the end', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
render(template(generateKeyNodes(['a', 'b'])), container);
expect(container.textContent).toBe('ab');
expect(container.firstChild.childNodes.length).toBe(2);
});
it('should remove two keys at the end', () => {
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
render(template(generateKeyNodes(['a'])), container);
expect(container.textContent).toBe('a');
expect(container.firstChild.childNodes.length).toBe(1);
});
it('should add one key at the end', () => {
render(template(generateKeyNodes(['a', 'b'])), container);
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should add two key at the end', () => {
render(template(generateKeyNodes(['a'])), container);
render(template(generateKeyNodes(['a', 'b', 'c'])), container);
expect(container.textContent).toBe('abc');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should add to end, delete from center & reverse', () => {
render(template(generateKeyNodes(['a', 'b', 'c', 'd'])), container);
render(template(generateKeyNodes(['e', 'd', 'c', 'a'])), container);
expect(container.textContent).toBe('edca');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should add to the beginning and remove', () => {
render(template(generateKeyNodes(['c', 'd'])), container);
render(template(generateKeyNodes(['a', 'b', 'c', 'e'])), container);
expect(container.textContent).toBe('abce');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should keep a central pivot', () => {
render(template(generateKeyNodes(['1', '2', '3'])), container);
render(template(generateKeyNodes(['4', '2', '5'])), container);
expect(container.textContent).toBe('425');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should insert to the middle', () => {
render(template(generateKeyNodes(['c', 'd', 'e'])), container);
render(template(generateKeyNodes(['a', 'b', 'e'])), container);
expect(container.textContent).toBe('abe');
expect(container.firstChild.childNodes.length).toBe(3);
render(template(generateKeyNodes(['c', 'd', 'e'])), container);
render(template(generateKeyNodes(['c', 'd', 'e'])), container);
render(template(generateKeyNodes(['a', 'p', 'e'])), container);
expect(container.textContent).toBe('ape');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should shuffle, insert and remove', () => {
render(template(generateKeyNodes(['a', 'b', 'c', 'd', 'e', 'f', 'g'])), container);
render(template(generateKeyNodes(['b', 'c', 'a'])), container);
expect(container.textContent).toBe('bca');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should remove a element from the middle', () => {
render(template(generateKeyNodes([1, 2, 3, 4, 5])), container);
expect(container.firstChild.childNodes.length).toBe(5);
render(template(generateKeyNodes([1, 2, 4, 5])), container);
expect(container.textContent).toBe('1245');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move a element forward', () => {
render(template(generateKeyNodes([1, 2, 3, 4])), container);
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([2, 3, 1, 4])), container);
expect(container.textContent).toBe('2314');
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([3, 2, 1, 4])), container);
expect(container.textContent).toBe('3214');
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([3, 2, 4, 1])), container);
expect(container.textContent).toBe('3241');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move a element to the end', () => {
render(template(generateKeyNodes([1, 2, 3])), container);
expect(container.firstChild.childNodes.length).toBe(3);
render(template(generateKeyNodes([2, 3, 1])), container);
expect(container.textContent).toBe('231');
expect(container.firstChild.childNodes.length).toBe(3);
});
it('should move a element backwards', () => {
render(template(generateKeyNodes([1, 2, 3, 4])), container);
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([1, 4, 2, 3])), container);
expect(container.textContent).toBe('1423');
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should swap first and last', () => {
render(template(generateKeyNodes([1, 2, 3, 4])), container);
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([4, 2, 3, 1])), container);
expect(container.textContent).toBe('4231');
expect(container.firstChild.childNodes.length).toBe(4);
render(template(generateKeyNodes([1, 2, 3, 4])), container);
expect(container.firstChild.childNodes.length).toBe(4);
});
it('should move to left and replace', () => {
render(template(generateKeyNodes([1, 2, 3, 4, 5])), container);
expect(container.firstChild.childNodes.length).toBe(5);
render(template(generateKeyNodes([4, 1, 2, 3, 6])), container);
expect(container.textContent).toBe('41236');
expect(container.firstChild.childNodes.length).toBe(5);
render(template(generateKeyNodes([4, 5, 2, 3, 0])), container);
expect(container.textContent).toBe('45230');
render(template(generateKeyNodes([1, 2, 3, 4, 5])), container);
expect(container.firstChild.childNodes.length).toBe(5);
});
it('should move to left and leave a hole', () => {
render(template(generateKeyNodes([1, 4, 5])), container);
expect(container.firstChild.childNodes.length).toBe(3);
render(template(generateKeyNodes([4, 6])), container);
expect(container.textContent).toBe('46');
expect(container.firstChild.childNodes.length).toBe(2);
});
it('should do something', () => {
render(template(generateKeyNodes([0, 1, 2, 3, 4, 5])), container);
expect(container.firstChild.childNodes.length).toBe(6);
render(template(generateKeyNodes([4, 3, 2, 1, 5, 0])), container);
expect(container.textContent).toBe('432150');
expect(container.firstChild.childNodes.length).toBe(6);
});
it('should cycle order correctly', () => {
render(template(generateKeyNodes([1, 2, 3, 4])), container);
expect(container.firstChild.childNodes.length).toBe(4);
expect(container.textContent).toBe('1234');
render(template(generateKeyNodes([2, 3, 4, 1])), container);
expect(container.firstChild.childNodes.length).toBe(4);
expect(container.textContent).toBe('2341');
render(template(generateKeyNodes([3, 4, 1, 2])), container);
expect(container.firstChild.childNodes.length).toBe(4);
expect(container.textContent).toBe('3412');
render(template(generateKeyNodes([4, 1, 2, 3])), container);
expect(container.firstChild.childNodes.length).toBe(4);
expect(container.textContent).toBe('4123');
render(template(generateKeyNodes([1, 2, 3, 4])), container);