-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathchild-observation.integration.spec.ts
750 lines (660 loc) · 30.2 KB
/
child-observation.integration.spec.ts
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
import { Aurelia, TaskQueue, useShadowDOM } from 'aurelia-framework';
import 'aurelia-loader-webpack';
import { Controller, inlineView } from '../src/aurelia-templating';
import { child, children } from '../src/child-observation';
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IChildObserver {}
interface IBindableMutationObserver extends MutationObserver {
binders: IChildObserver[];
}
interface IMutationObserverHost extends Element {
__childObserver__: IBindableMutationObserver;
}
describe('child-observation.integration.spec.ts', () => {
beforeAll(() => waitForTicks(10));
for (const shouldUseShadowDom of [true, false]) {
describe(`[useShadowDOM(): ${shouldUseShadowDom}]`, () => {
it('gets the child correctly in basic scenario', async () => {
const Template_App =
`<template>
<parent-el view-model.ref=parentVm>
<div class="item"></div>
</parent-el>
</template>`;
const Template_Parent = '<template>This is parent<div><slot></slot></div></template>';
@inlineView(Template_App)
class App {
itemCount = 10;
parentVm: ParentEl;
}
@inlineView(Template_Parent)
class ParentEl {
@child('.item') item: HTMLDivElement;
@children('.item') items: HTMLDivElement[];
item_changed_call_count = 0;
items_changed_call_count = 0;
itemChanged() {
this.item_changed_call_count++;
}
itemsChanged() {
this.items_changed_call_count++;
}
}
if (shouldUseShadowDom) {
useShadowDOM({ mode: 'open' })(ParentEl);
}
const {
host,
rootVm,
dispose,
} = await createFixture(App, [ParentEl]);
// right after app has started successfully,
// it is guaranteed that all child observation is setup & done
// so there's no difference between native shadowDOM & emulation
const parentVm = rootVm.parentVm;
expect(parentVm.item instanceof HTMLDivElement).toBe(true);
expect(parentVm.items.length).toBe(1, '1 .item');
expect(parentVm.items[0]).toBe(rootVm.parentVm.item);
expect(parentVm.item_changed_call_count).toBe(1);
expect(parentVm.items_changed_call_count).toBe(1);
const parentEl = host.querySelector('parent-el') as IMutationObserverHost;
assertIsMutationObserverHost(parentEl);
const observer = parentEl.__childObserver__;
expect(observer.binders.length).toBe(/* 1 for @child + 1 for @children */2);
dispose();
expect(observer.binders.length).toBe(0);
expect(parentVm.item_changed_call_count).toBe(1);
expect(parentVm.items_changed_call_count).toBe(1);
if (shouldUseShadowDom) {
// when using shadow DOM, the binder will clear the value on unbind
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
}
});
describe('With [if]', () => {
// In this test, the assertions are:
// 1. initially render without any content element
// 2. showChild = true
// 3. showChild = false
// 4. showChild = true
// 5. dispose
it('works with [if] on content CHILD elements', async () => {
const Template_App =
`<template>
<parent-el view-model.ref=parentVm>
<div class="item" if.bind="showChild"></div>
</parent-el>
</template>`;
const Template_Parent = '<template>This is parent<p><slot></slot></p></template>';
@inlineView(Template_App)
class App {
showChild = false;
parentVm: ParentEl;
}
@inlineView(Template_Parent)
class ParentEl {
@child('.item') item: HTMLDivElement;
@children('.item') items: HTMLDivElement[];
item_changed_call_count = 0;
items_changed_call_count = 0;
itemChanged() {
this.item_changed_call_count++;
}
itemsChanged() {
this.items_changed_call_count++;
}
}
if (shouldUseShadowDom) {
useShadowDOM({ mode: 'open' })(ParentEl);
}
const {
host,
rootVm,
dispose,
} = await createFixture(App, [ParentEl]);
// Assertion 1 ===========================
const parentVm = rootVm.parentVm;
expect(parentVm.item).toBe(undefined);
if (shouldUseShadowDom) {
// when using shadowDOM, it's safe to initialize immediately
// so if theres no items, initialize it to an empty array
expect(parentVm.items).toEqual([], '0 .item');
} else {
expect(parentVm.items).toEqual(undefined, '0 .item');
}
const parentEl = host.querySelector('parent-el') as IMutationObserverHost;
const observer = parentEl.__childObserver__;
assertIsMutationObserverHost(parentEl);
expect(observer.binders.length).toBe(/* 1 for @child + 1 for @children */2);
// IMPORTANT: ==========
// In native shadow DOM + there's no children
// items Changed call count is always 1 count ahead of itemChanged call count
// as it is always call at the start, with empty values
expect(parentVm.item_changed_call_count).toBe(0, 'item changed() -- 1');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 1 : 0, 'items changed() -- 1');
// Assertion 2 ===========================
rootVm.showChild = true;
// for mutation observer to fire
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(1, '0 .item');
expect(parentVm.items[0]).toBe(parentVm.item);
expect(parentVm.item_changed_call_count).toBe(1, 'item changed() -- 2');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'items changed() -- 2');
// Assertion 3 ===========================
// Turning show child property back to false
// =======================================
rootVm.showChild = false;
// for mutation observer to fire
await waitForTicks(2);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toEqual([], '0 .item');
} else {
expect(parentVm.item).toBe(null, 'should have no .item after showChild = false');
expect(parentVm.items.length).toBe(0, '1 .item < after showChild = false');
}
expect(parentVm.item_changed_call_count).toBe(2, 'item changed() -- 3');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 3 : 2, 'items changed() -- 3');
// Assertion 4 ===========================
// assert disposing logic with value already populated
rootVm.showChild = true;
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(1, '0 .item');
expect(parentVm.items[0]).toBe(parentVm.item);
expect(parentVm.item_changed_call_count).toBe(3, 'item changed() -- 4');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 4');
dispose();
// Assertion 5 ===========================
expect(observer.binders.length).toBe(0, 'should have no binders after dispose()');
expect(parentVm.item_changed_call_count).toBe(3, 'item changed() -- 5');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 5');
if (shouldUseShadowDom) {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
}
});
// In this test, the assertions are:
// 1. initially render WITH parent element
// 2. assign false to if binding value, and remove parent element
// 3. assign true to if binding value, and show parent element
// 4. dispose the app
it('works with [if] on content PARENT elements', async () => {
const Template_App =
`<template>
<parent-el view-model.ref=parentVm if.bind="showParent">
<div class="item"></div>
</parent-el>
</template>`;
const Template_Parent = '<template>This is parent<p><slot></slot></p></template>';
@inlineView(Template_App)
class App {
showParent = true;
parentVm: ParentEl;
}
@inlineView(Template_Parent)
class ParentEl {
@child('.item') item: HTMLDivElement;
@children('.item') items: HTMLDivElement[];
item_changed_call_count = 0;
items_changed_call_count = 0;
itemChanged() {
this.item_changed_call_count++;
}
itemsChanged() {
this.items_changed_call_count++;
}
}
if (shouldUseShadowDom) {
useShadowDOM({ mode: 'open' })(ParentEl);
}
const {
host,
rootVm,
dispose,
} = await createFixture(App, [ParentEl]);
// Assertion 1 =========================================
// 1. initially render WITH parent element
const parentVm = rootVm.parentVm;
expect(parentVm.item instanceof HTMLElement).toBe(true);
expect(parentVm.items.length).toBe(1, '1 .item');
const parentEl = host.querySelector('parent-el') as IMutationObserverHost;
let observer = parentEl.__childObserver__;
assertIsMutationObserverHost(parentEl);
expect(observer.binders.length).toBe(/* 1 for @child + 1 for @children */2);
expect(parentVm.item_changed_call_count).toBe(1, 'item changed() -- 1');
expect(parentVm.items_changed_call_count).toBe(1, 'items changed() -- 1');
// Assertion 2 =========================================
// 2. assign false to if binding value, and remove parent element
rootVm.showParent = false;
// flush all aurelia bindings, including repeat/if
await waitForTicks(1);
// mutation observer is cleaned upon cleaning all binders
expect(parentEl.__childObserver__).toBe(null);
expect(observer.binders.length).toBe(0);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
expect(parentVm.item instanceof HTMLElement).toBe(true);
expect(parentVm.items.length).toBe(1, '1 .item');
expect(parentVm.items[0]).toBe(parentVm.item);
}
// for mutation observer to fire
await waitForTicks(1);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(1, '0 .item');
expect(parentVm.items[0]).toBe(parentVm.item);
}
// nothing changed, DOM mutation wise
expect(parentVm.item_changed_call_count).toBe(1, 'item changed() -- 2');
expect(parentVm.items_changed_call_count).toBe(1, 'items changed() -- 2');
// Assertion 3 =========================================
// 3. assign true to if binding value, and show parent element
rootVm.showParent = true;
await waitForTicks(1);
// observer is clean so need to get a new reference
expect(observer).not.toBe(parentEl.__childObserver__);
observer = parentEl.__childObserver__;
expect(observer.binders.length).toBe(2, 'Should have 2 binders after showing parent');
// when using native shadow DOM
// whenever the mutation observer binder is bound
// it will calls the change handler, so it's 1 count ahead of the emulation
expect(parentVm.item_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'item changed() -- 3');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'items changed() -- 3');
// Assertion 4 =========================================
// 4. dispose the app
dispose();
expect(observer.binders.length).toBe(0);
expect(parentEl.__childObserver__).toBe(null);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(1, '0 .item');
expect(parentVm.items[0]).toBe(parentVm.item);
}
expect(parentVm.item_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'item changed() -- 4');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'items changed() -- 4');
});
});
describe('With [repeat]', () => {
// In this test, the assertions are:
// 1. initially render without any content element (itemCount = 0)
// 2. itemCount = 2, and render child content elements
// 3. itemCount = 0, and remove child content elements
// 4. itemCount = 2, and render child content elements
// 5. dispose the app
it('works with [repeat] on content CHILD elements', async () => {
const Template_App =
`<template>
<parent-el view-model.ref=parentVm>
<div id="item-\${i}" class="item" repeat.for="i of itemCount"></div>
</parent-el>
</template>`;
const Template_Parent = '<template>This is parent<p><slot></slot></p></template>';
@inlineView(Template_App)
class App {
itemCount = 0;
parentVm: ParentEl;
}
@inlineView(Template_Parent)
class ParentEl {
@child('.item') item: HTMLDivElement;
@children('.item') items: HTMLDivElement[];
item_changed_call_count = 0;
items_changed_call_count = 0;
itemChanged() {
this.item_changed_call_count++;
}
itemsChanged() {
this.items_changed_call_count++;
}
}
if (shouldUseShadowDom) {
useShadowDOM({ mode: 'open' })(ParentEl);
}
const {
host,
rootVm,
dispose,
} = await createFixture(App, [ParentEl]);
// Assertion 1 ===========================
// 1. initially render without any content element (itemCount = 0)
const parentVm = rootVm.parentVm;
expect(parentVm.item).toBe(undefined);
if (shouldUseShadowDom) {
// when using shadowDOM, it's safe to initialize immediately
// so if theres no items, initialize it to an empty array
expect(parentVm.items).toEqual([], '0 .item');
} else {
expect(parentVm.items).toEqual(undefined, '0 .item');
}
const parentEl = host.querySelector('parent-el') as IMutationObserverHost;
const observer = parentEl.__childObserver__;
assertIsMutationObserverHost(parentEl);
expect(observer.binders.length).toBe(/* 1 for @child + 1 for @children */2);
expect(parentVm.item_changed_call_count).toBe(0, 'item changed() -- 1');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 1 : 0, 'items changed() -- 1');
// Assertion 2 ===========================
// 2. itemCount = 2, and render child content elements
rootVm.itemCount = 2;
// for mutation observer to fire
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(2, '0 .item');
expect(parentVm.items[0]).not.toBe(parentVm.items[1]);
expect(parentVm.items[1]).toBe(parentVm.item);
// note: change handler is call for every item when it's @child
// this could be a surprise, or perf issue
expect(parentVm.item_changed_call_count).toBe(2, 'item changed() -- 2');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'items changed() -- 2');
// Assertion 3 ===========================
// 3. itemCount = 0, and remove child content elements
rootVm.itemCount = 0;
// for mutation observer to fire
await waitForTicks(2);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toEqual([], '0 .item');
} else {
expect(parentVm.item).toBe(null, 'should have no .item after showChild = false');
expect(parentVm.items.length).toBe(0, '1 .item < after showChild = false');
}
expect(parentVm.item_changed_call_count).toBe(3, 'item changed() -- 3');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 3 : 2, 'items changed() -- 3');
// Assertion 4 ===========================
// 4. itemCount = 2, and render child content elements
rootVm.itemCount = 2;
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items.length).toBe(2, '0 .item');
expect(parentVm.items[0]).not.toBe(parentVm.items[1]);
expect(parentVm.items[1]).toBe(parentVm.item);
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 4');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 4');
dispose();
// Assertion 5 ===========================
expect(observer.binders.length).toBe(0, 'should have no binders after dispose()');
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 5');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 5');
if (shouldUseShadowDom) {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
// when not using shadow DOM, the binder won't clear the value on unbind
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.items[0]).not.toBe(parentVm.items[1]);
expect(parentVm.items[1]).toBe(parentVm.item);
}
});
});
// In this test, the assertions are:
// 1. showParent = true + itemCount = 0
// ---- assert [add] while [attached] mutation ----
// 2. itemCount = 2
// 2.1 itemCount = 0
// 2.2 itemCount = 2
// ---- assert [removal] while [detached] mutation
// 3. showParent = false
// 4. itemCount = 0
// ---- assert [add] while [detached] ----
// 5. itemCount = 2
// ---- assert [removal] while [attached] mutation -----
// 6. showParent = true
// 7. itemCount = 0
// ---- final ----
// 8. dispose
it('\n\tworks with [if] on content PARENT elements\n\tAnd [repeat] on CHILD elements\n', async () => {
const Template_App =
`<template>
<parent-el view-model.ref=parentVm if.bind="showParent">
<div id="item-\${generateId(i)}" class="item" repeat.for="i of itemCount"></div>
</parent-el>
</template>`;
const Template_Parent = '<template>This is parent<p><slot></slot></p></template>';
@inlineView(Template_App)
class App {
showParent = true;
itemCount = 0;
parentVm: ParentEl;
id = 0;
generateId(i: number) {
return `item-${i}-${this.id++}`;
}
}
@inlineView(Template_Parent)
class ParentEl {
@child('.item') item: HTMLDivElement;
@children('.item') items: HTMLDivElement[];
item_changed_call_count = 0;
items_changed_call_count = 0;
itemChanged() {
this.item_changed_call_count++;
}
itemsChanged() {
this.items_changed_call_count++;
}
}
if (shouldUseShadowDom) {
useShadowDOM({ mode: 'open' })(ParentEl);
}
const {
host,
rootVm,
dispose,
} = await createFixture(App, [ParentEl]);
// Assertion 1 =========================================
// 1. initially render WITH parent element
const parentVm = rootVm.parentVm;
expect(parentVm.item).toBe(undefined, '0 .item');
if (shouldUseShadowDom) {
expect(parentVm.items).toEqual([], '[] .item[]');
} else {
expect(parentVm.items).toBe(undefined, '0 .item[]');
}
const parentEl = host.querySelector('parent-el') as IMutationObserverHost;
let observer = parentEl.__childObserver__;
assertIsMutationObserverHost(parentEl);
expect(observer.binders.length).toBe(/* 1 for @child + 1 for @children */2);
expect(parentVm.item_changed_call_count).toBe(0, 'item changed() -- 1');
// during .bind()
// change handler is always called when using native shadowDOM
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 1 : 0, 'items changed() -- 1');
// ---- assert [add] while [attached] mutation ----
// Assertion 2 =========================================
// 2. itemCount = 2
rootVm.itemCount = 2;
// for mutation observer to fire
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.item instanceof HTMLElement).toBe(true);
expect(parentVm.items.length).toBe(2, '2 .item');
expect(parentVm.items[1]).toBe(parentVm.item);
expect(parentVm.item_changed_call_count).toBe(2, 'item changed() -- 2');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 2 : 1, 'items changed() -- 2');
// 2.1 itemCount = 0
rootVm.itemCount = 0;
await waitForTicks(2);
expect(parentVm.item).toBe(null);
expect(parentVm.items).toEqual([]);
expect(parentVm.item_changed_call_count).toBe(3, 'item changed() -- 2.1');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 3 : 2, 'items changed() -- 2.1');
// 2.2 itemCount = 2
rootVm.itemCount = 2;
await waitForTicks(2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toEqual([]);
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 2.2');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 2.2');
// ---- assert [removal] while [detached] mutation
// Assertion 3 =========================================
// 3. showParent = false
rootVm.showParent = false;
await waitForTicks(1);
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 3');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 3');
// ensure one mutation observer not affecting another
await waitForTicks(1);
// Assertion 4 =========================================
// 4. itemCount = 0
// This steps corrupts the repeat, and will leave 2 orphaned <div/> inside the
rootVm.itemCount = 0;
await waitForTicks(2);
if (shouldUseShadowDom) {
assertSelectorCount(parentEl, '.item', 0);
expect(parentVm.item).toBe(null, '4. item === null');
expect(parentVm.items).toBe(null, '4. items === null');
} else {
// shadow dom emulation doesn't response to mutation while detached
assertSelectorCount(parentEl, 'p > .item', 0);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.items.length).toBe(/* 2 orphaned */2);
}
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 4');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 4');
// ---- assert [add] while [detached] ----
// Assertion 5 =========================================
// 5. itemCount = 2
const old_item = parentVm.item;
// const old_items_arr = parentVm.items;
// const old_items = old_items_arr?.slice(0) ?? [];
rootVm.itemCount = 2;
await waitForTicks(2);
if (shouldUseShadowDom) {
assertSelectorCount(parentEl, '.item', 0);
expect(parentVm.item).toBe(null, '5. item === null');
expect(parentVm.item).toBe(old_item, '5. item === old_item');
expect(parentVm.items).toBe(null, '5. items === null');
} else {
// "slotted" content wont be removed when the view is detached
assertSelectorCount(parentEl, 'p > .item', 0);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.items.length).toBe(/* 2 orphaned */2);
}
expect(parentVm.item_changed_call_count).toBe(5, 'item changed() -- 5');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 4 : 3, 'items changed() -- 5');
// ---- assert [removal] while [attached] mutation -----
// 6. showParent = true
rootVm.showParent = true;
await waitForTicks(2);
if (shouldUseShadowDom) {
assertSelectorCount(parentEl, '.item', 2);
expect(parentVm.item).not.toBe(null, '6. item !== null');
expect(parentVm.item).not.toBe(old_item, '6. item !== old_item');
expect(parentVm.items).not.toBe(null, '6. items !== old_items');
} else {
assertSelectorCount(parentEl, 'p > .item', 2);
expect(parentVm.item).not.toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.items.length).toBe(/* 2 orphaned + 2 from repeat */4);
}
expect(parentVm.item_changed_call_count).toBe(7, 'item changed() -- 6');
// when using shadowDOM
// 1 call for bind()
// 1 call for actual mutation change from repeat
// so +2 more calls in total
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 6 : 4, 'items changed() -- 6');
// 7. itemCount = 0
rootVm.itemCount = 0;
await waitForTicks(2);
assertSelectorCount(parentEl, '.item', 0);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null, '7. item === null');
expect(parentVm.items).toEqual([], '7. items === null');
} else {
// shadow dom emulation doesn't response to mutation while detached
expect(parentVm.item).toBe(null);
expect(parentVm.items).not.toBe(null);
expect(parentVm.items.length).toBe(/* 2 orphaned */2);
}
expect(parentVm.item_changed_call_count).toBe(8, 'item changed() -- 7');
expect(parentVm.items_changed_call_count).toBe(shouldUseShadowDom ? 7 : 5, 'items changed() -- 7');
// ---- final ----
// 9. dispose
dispose();
expect(observer.binders.length).toBe(0);
expect(parentEl.__childObserver__).toBe(null);
if (shouldUseShadowDom) {
expect(parentVm.item).toBe(null);
expect(parentVm.items).toBe(null);
} else {
expect(parentVm.item).toBe(null);
expect(parentVm.items.length).toBe(/* 2 orphaned */2);
}
});
});
}
async function createFixture<T>(root: Constructable<T>, resources: any[] = []) {
const aurelia = new Aurelia();
let $$taskQueue: TaskQueue = createFixture.taskQueue;
if ($$taskQueue) {
aurelia.container.registerInstance(TaskQueue, $$taskQueue);
} else {
$$taskQueue = createFixture.taskQueue = aurelia.container.get(TaskQueue);
}
aurelia
.use
.defaultBindingLanguage()
.defaultResources()
.globalResources(resources)
const host = document.createElement('div');
await aurelia.start();
await aurelia.setRoot(root, host);
const rootController = aurelia['root'] as Controller;
return {
aurelia,
host,
root: rootController,
rootVm: rootController.viewModel as T,
taskQueue: $$taskQueue,
dispose: () => {
$$taskQueue.flushMicroTaskQueue();
$$taskQueue.flushTaskQueue();
rootController.detached();
rootController.unbind();
$$taskQueue.flushMicroTaskQueue();
}
}
}
createFixture.taskQueue = null;
function assertIsMutationObserverHost(element: Element): element is IMutationObserverHost {
const observer = element['__childObserver__'];
expect(observer instanceof MutationObserver).toBe(true, 'there should be mutation observer on element');
return true;
}
function assertSelectorCount(element: Element, selector: string, count: number): void {
let childCount = element.querySelectorAll(selector).length;
expect(childCount).toBe(count, `Expected selecotr: "${selector}" count for <${element.tagName.toLowerCase()}/> to be ${count}, found ${childCount}`);
}
async function waitForTicks(count = 0) {
while (count > 0) {
await Promise.resolve();
count--;
}
}
interface Constructable<T> {
new(...args: any[]): T;
}
});