-
Notifications
You must be signed in to change notification settings - Fork 879
/
intersection-controller_test.ts
561 lines (478 loc) · 15.8 KB
/
intersection-controller_test.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
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {
ReactiveElement,
PropertyValues,
ReactiveControllerHost,
} from '@lit/reactive-element';
import {
IntersectionController,
IntersectionControllerConfig,
IntersectionValueCallback,
} from '@lit-labs/observers/intersection-controller.js';
import {generateElementName, nextFrame} from './test-helpers.js';
import {assert} from '@esm-bundle/chai';
// Note, since tests are not built with production support, detect DEV_MODE
// by checking if warning API is available.
const DEV_MODE = !!ReactiveElement.enableWarning;
if (DEV_MODE) {
ReactiveElement.disableWarning?.('change-in-update');
}
const canTest = () => {
// TODO: disable tests on Sauce Safari until can figure out issues.
// The tests pass on latest Safari locally.
const isSafari =
navigator.userAgent.includes('Safari/') &&
navigator.userAgent.includes('Version/');
return !isSafari && window.IntersectionObserver;
};
(canTest() ? suite : suite.skip)('IntersectionController', () => {
let container: HTMLElement;
interface TestElement extends ReactiveElement {
observer: IntersectionController;
observerValue: unknown;
resetObserverValue: () => void;
changeDuringUpdate?: () => void;
}
const defineTestElement = (
getControllerConfig: (
host: ReactiveControllerHost
) => IntersectionControllerConfig
) => {
class A extends ReactiveElement {
observer: IntersectionController;
observerValue: unknown;
changeDuringUpdate?: () => void;
constructor() {
super();
const config = getControllerConfig(this);
this.observer = new IntersectionController(this, {
callback: () => true,
...config,
});
}
override update(props: PropertyValues) {
super.update(props);
if (this.changeDuringUpdate) {
this.changeDuringUpdate();
}
}
override updated() {
this.observerValue = this.observer.value;
}
resetObserverValue() {
this.observer.value = this.observerValue = undefined;
}
}
customElements.define(generateElementName(), A);
return A;
};
const renderTestElement = async (Ctor: typeof HTMLElement) => {
const el = new Ctor() as TestElement;
container.appendChild(el);
await intersectionComplete();
return el;
};
const getTestElement = async (
getControllerConfig: (
host: ReactiveControllerHost
) => IntersectionControllerConfig = () => ({})
) => {
const ctor = defineTestElement(getControllerConfig);
const el = await renderTestElement(ctor);
return el;
};
const intersectionComplete = async () => {
await nextFrame();
await nextFrame();
// For Firefox to pass tests we need a setTimeout.
await new Promise((resolve) => setTimeout(resolve, 0));
};
const intersectOut = (el: HTMLElement) => {
el.style.position = 'absolute';
el.style.left = el.style.top = '-10000px';
};
const intersectIn = (el: HTMLElement) => {
el.style.position = 'absolute';
el.style.left = el.style.top = '0px';
};
setup(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
teardown(() => {
if (container && container.parentNode) {
container.parentNode.removeChild(container);
}
});
// Note, only the first test fails on chrome when using playwright.
// Work around this by inserting 1 dummy test.
test('dummy test: workaround for first test fails on chrome when using playwright', async () => {
const el = await getTestElement();
assert.ok(el);
});
test('can observe changes', async () => {
const el = await getTestElement();
// Reports initial change by default
assert.isTrue(el.observerValue);
// Reports change when not intersecting
el.resetObserverValue();
intersectOut(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change when intersecting
el.resetObserverValue();
intersectIn(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can observe changes during update', async () => {
const el = await getTestElement();
el.resetObserverValue();
el.changeDuringUpdate = () => intersectOut(el);
el.requestUpdate();
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('skips initial changes when `skipInitial` is `true`', async () => {
const el = await getTestElement(() => ({
skipInitial: true,
}));
// Does not reports initial change when `skipInitial` is set
assert.isUndefined(el.observerValue);
// Reports subsequent attribute change when `skipInitial` is set
el.resetObserverValue();
intersectOut(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports another attribute change
el.resetObserverValue();
el.requestUpdate();
await intersectionComplete();
assert.isUndefined(el.observerValue);
intersectIn(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('observation managed via connection', async () => {
const el = await getTestElement(() => ({
skipInitial: true,
}));
assert.isUndefined(el.observerValue);
intersectOut(el);
// Does not report change after element removed.
el.remove();
await intersectionComplete();
assert.isUndefined(el.observerValue);
// Does not report change after element re-connected
container.appendChild(el);
await intersectionComplete();
assert.isUndefined(el.observerValue);
// Reports change on mutation when element is connected
el.resetObserverValue();
intersectIn(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can observe external element', async () => {
const d = document.createElement('div');
container.appendChild(d);
const el = await getTestElement(() => ({
target: d,
skipInitial: true,
}));
assert.equal(el.observerValue, undefined);
// Observe intersect out
intersectOut(d);
await intersectionComplete();
assert.isTrue(el.observerValue);
el.resetObserverValue();
// Observe intersect in
intersectIn(d);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can manage value via `callback`', async () => {
const el = await getTestElement(() => ({
callback: (entries: IntersectionObserverEntry[]) =>
entries[0]?.isIntersecting,
}));
assert.isTrue(el.observerValue);
el.resetObserverValue();
// Intersect out
intersectOut(el);
await intersectionComplete();
assert.isFalse(el.observerValue);
// Intersect in
el.resetObserverValue();
await intersectionComplete();
intersectIn(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can call `observe` to observe element', async () => {
const el = await getTestElement();
el.resetObserverValue();
const d1 = document.createElement('div');
// Reports initial changes when observe called.
el.observer.observe(d1);
el.renderRoot.appendChild(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to observed target.
el.resetObserverValue();
intersectOut(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to configured target.
el.resetObserverValue();
intersectOut(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reset host
intersectIn(el);
await intersectionComplete();
// Can observe another target
el.resetObserverValue();
const d2 = document.createElement('div');
el.observer.observe(d2);
el.renderRoot.appendChild(d2);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to new observed target.
el.resetObserverValue();
intersectOut(d2);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to configured target.
el.resetObserverValue();
intersectOut(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reset host
intersectIn(el);
await intersectionComplete();
// Reports change to first observed target.
el.resetObserverValue();
intersectIn(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can specifying target as `null` and call `observe` to observe element', async () => {
const el = await getTestElement(() => ({
target: null,
}));
el.resetObserverValue();
const d1 = document.createElement('div');
// Reports initial changes when observe called.
el.observer.observe(d1);
el.renderRoot.appendChild(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to observed target.
el.resetObserverValue();
intersectOut(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Can observe another target
el.resetObserverValue();
const d2 = document.createElement('div');
el.observer.observe(d2);
el.renderRoot.appendChild(d2);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to new observed target.
el.resetObserverValue();
intersectOut(d2);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to first observed target.
el.resetObserverValue();
intersectIn(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('observed targets are re-observed on host connected', async () => {
const el = await getTestElement(() => ({target: null}));
const d1 = document.createElement('div');
const d2 = document.createElement('div');
el.renderRoot.appendChild(d1);
el.renderRoot.appendChild(d2);
el.observer.observe(d1);
el.observer.observe(d2);
await intersectionComplete();
el.resetObserverValue();
el.remove();
container.appendChild(el);
// Reports change to first observed target.
el.resetObserverValue();
intersectOut(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change to second observed target.
el.resetObserverValue();
intersectOut(d2);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('observed target respects `skipInitial`', async () => {
const el = await getTestElement(() => ({
target: null,
skipInitial: true,
}));
const d1 = document.createElement('div');
el.renderRoot.appendChild(d1);
await intersectionComplete();
// Does not reports initial changes when observe called.
el.observer.observe(d1);
el.requestUpdate();
await intersectionComplete();
assert.isUndefined(el.observerValue);
// Reports change to observed target.
intersectOut(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('observed target re-observed on connection', async () => {
const el = await getTestElement(() => ({
target: null,
skipInitial: true,
}));
const d1 = document.createElement('div');
el.renderRoot.appendChild(d1);
el.observer.observe(d1);
await intersectionComplete();
assert.isUndefined(el.observerValue);
// Does not report change when disconnected.
el.requestUpdate();
el.remove();
await intersectionComplete();
assert.isUndefined(el.observerValue);
// Reports changes when re-connected
container.appendChild(el);
await intersectionComplete();
assert.isUndefined(el.observerValue);
intersectOut(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can observe changes when initialized after host connected', async () => {
class TestFirstUpdated extends ReactiveElement {
observer!: IntersectionController<true>;
observerValue: true | undefined = undefined;
override firstUpdated() {
this.observer = new IntersectionController(this, {
callback: () => true,
});
}
override updated() {
this.observerValue = this.observer.value;
}
resetObserverValue() {
this.observer.value = this.observerValue = undefined;
}
}
customElements.define(generateElementName(), TestFirstUpdated);
const el = (await renderTestElement(TestFirstUpdated)) as TestFirstUpdated;
// Reports initial change by default
assert.isTrue(el.observerValue);
// Reports change when not intersecting
el.resetObserverValue();
intersectOut(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
// Reports change when intersecting
el.resetObserverValue();
assert.isUndefined(el.observerValue);
intersectIn(el);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('can observe external element after host connected', async () => {
const d = document.createElement('div');
container.appendChild(d);
class A extends ReactiveElement {
observer!: IntersectionController<true>;
observerValue: true | undefined = undefined;
override firstUpdated() {
this.observer = new IntersectionController(this, {
target: d,
skipInitial: true,
callback: () => true,
});
}
override updated() {
this.observerValue = this.observer.value;
}
resetObserverValue() {
this.observer.value = this.observerValue = undefined;
}
}
customElements.define(generateElementName(), A);
const el = (await renderTestElement(A)) as A;
assert.equal(el.observerValue, undefined);
// Observe intersect out
intersectOut(d);
await intersectionComplete();
assert.isTrue(el.observerValue);
el.resetObserverValue();
// Observe intersect in
intersectIn(d);
await intersectionComplete();
assert.isTrue(el.observerValue);
});
test('IntersectionController<T> type-checks', async () => {
// This test only checks compile-type behavior. There are no runtime checks.
const el = await getTestElement();
const A = new IntersectionController<number>(el, {
// @ts-expect-error Type 'string' is not assignable to type 'number'
callback: () => '',
});
if (A) {
// Suppress no-unused-vars warnings
}
const B = new IntersectionController(el, {
callback: () => '',
});
// @ts-expect-error Type 'number' is not assignable to type 'string'.
B.value = 2;
const C = new IntersectionController(
el,
{} as IntersectionController<string>
);
// @ts-expect-error Type 'number' is not assignable to type 'string'.
C.value = 3;
const narrowTypeCb: IntersectionValueCallback<string | null> = () => '';
const D = new IntersectionController(el, {callback: narrowTypeCb});
D.value = null;
D.value = undefined;
D.value = '';
// @ts-expect-error Type 'number' is not assignable to type 'string'
D.value = 3;
});
test('observed target can be unobserved', async () => {
const el = await getTestElement(() => ({target: null}));
const d1 = document.createElement('div');
// Reports initial changes when observe called.
el.observer.observe(d1);
el.renderRoot.appendChild(d1);
await intersectionComplete();
assert.isTrue(el.observerValue);
el.resetObserverValue();
await intersectionComplete();
// Does not report change when unobserved
el.observer.unobserve(d1);
intersectOut(d1);
await intersectionComplete();
assert.isUndefined(el.observerValue);
el.remove();
container.appendChild(el);
// Does not report changes when re-connected
intersectIn(d1);
await intersectionComplete();
assert.isUndefined(el.observerValue);
});
});