-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
VList.spec.ts
1590 lines (1287 loc) · 51.5 KB
/
VList.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
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 { test, expect, ElementHandle } from "@playwright/test";
import {
storyUrl,
getFirstItem,
getLastItem,
scrollToBottom,
scrollToRight,
clearInput,
approxymate,
scrollBy,
getScrollTop,
getScrollLeft,
getScrollBottom,
getScrollRight,
expectInRange,
scrollWithTouch,
getFirstItemRtl,
scrollToLeft,
getVirtualizer,
getScrollable,
clearTimer,
scrollTo,
} from "./utils";
const listenScrollCount = (
component: ElementHandle<SVGElement | HTMLElement>,
timeout = 2000
): Promise<number> => {
return component.evaluate((c, t) => {
let timer: null | ReturnType<typeof setTimeout> = null;
let called = 0;
return new Promise<number>((resolve) => {
const cb = () => {
called++;
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(() => {
c.removeEventListener("scroll", cb);
resolve(called);
}, t);
};
c.addEventListener("scroll", cb);
});
}, timeout);
};
test.describe("smoke", () => {
test("vertically scrollable", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
const first = await getFirstItem(component);
await expect(first.text).toEqual("0");
await expect(first.top).toEqual(0);
// scroll to the end
await scrollToBottom(component);
// check if the end is displayed
await expect(await component.innerText()).toContain("999");
});
test("horizontally scrollable", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--horizontal"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
const first = await getFirstItem(component);
await expect(first.text).toEqual("Column 0");
await expect(first.left).toEqual(0);
// scroll to the end
await scrollToRight(component);
// check if the end is displayed
await expect(await component.innerText()).toContain("999");
});
test("reverse", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--reverse"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if last is displayed
const last = await getLastItem(component);
await expect(last.text).toEqual("999");
await expect(last.bottom).toEqual(0);
});
test("display: none", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
const initialTotalHeight = await component.evaluate(
(s) => getComputedStyle(s.childNodes[0] as HTMLElement).height
);
await component.evaluate((s) => (s.style.display = "none"));
await component.waitForElementState("stable");
const changedTotalHeight = await component.evaluate(
(s) => getComputedStyle(s.childNodes[0] as HTMLElement).height
);
expect(initialTotalHeight).toBeTruthy();
expect(initialTotalHeight).toEqual(changedTotalHeight);
});
test("new window", async ({ page, context }) => {
await page.goto(storyUrl("advanced-newwindow--default"));
// open new window
const newPagePromise = context.waitForEvent("page");
await page.getByRole("button", { name: "open window" }).click();
const newPage = await newPagePromise;
const component = await getScrollable(newPage);
await component.waitForElementState("stable");
// check if start is displayed
const first = await getFirstItem(component);
await expect(first.text).toEqual("0");
await expect(first.top).toEqual(0);
// scroll to the end
await scrollToBottom(component);
// check if the end is displayed
await expect(await component.innerText()).toContain("999");
});
test("scroll restoration", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--scroll-restoration"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
const initialItem = await getFirstItem(component);
expect((await initialItem).text).toEqual("0");
// scroll to mid
await scrollTo(component, 5000);
await page.waitForTimeout(250);
const mountedItem = await getFirstItem(component);
expect(mountedItem.text).not.toEqual(initialItem.text);
// check if items are unmounted
await page.getByRole("button", { name: "hide" }).click();
expect((await getFirstItem(component)).text).not.toEqual(mountedItem.text);
// check if scroll position is restored
await page.getByRole("button", { name: "show" }).click();
await page.waitForTimeout(250);
const remountedComponent = await getScrollable(page);
const remountedItem = await getFirstItem(remountedComponent);
expect(remountedItem.text).toEqual(mountedItem.text);
expect(remountedItem.top).toEqual(mountedItem.top);
});
});
test.describe("check if it works when children change", () => {
test("recovering from 0", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--increasing-items"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
const updateButton = page.getByRole("button", { name: "update" });
// delete all
await page.getByRole("radio", { name: "decrease" }).click();
for (let i = 0; i < 10; i++) {
await updateButton.click();
}
const topItem = await getFirstItem(component);
expect(topItem.text).not.toEqual("0");
// add
await page.getByRole("radio", { name: "increase" }).click();
await updateButton.click();
{
// check if an error didn't occur
expect(
(await page.innerText("body")).toLowerCase().includes("localhost")
).toBeFalsy();
}
});
test("recovering when changed a lot after scrolling", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--increasing-items"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
const input = page.getByRole("spinbutton");
const updateButton = page.getByRole("button", { name: "update" });
// add many
input.fill("1000");
await updateButton.click();
await component.waitForElementState("stable");
// scroll a lot
await scrollToBottom(component);
await component.waitForElementState("stable");
const topItem = await getFirstItem(component);
expect(topItem.text).not.toEqual("0");
// delete many
await page.getByRole("radio", { name: "decrease" }).click();
await updateButton.click();
await component.waitForElementState("stable");
// add many
await page.getByRole("radio", { name: "increase" }).click();
await updateButton.click();
await component.waitForElementState("stable");
{
// check if an error didn't occur
expect(
(await page.innerText("body")).toLowerCase().includes("localhost")
).toBeFalsy();
}
});
});
test.describe("check if scroll jump compensation works", () => {
test("vertical start -> end", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
// check if offset from start is always keeped
await component.click();
const min = 200;
const initial = await getScrollTop(component);
let prev = initial;
for (let i = 0; i < 500; i++) {
await page.keyboard.press("ArrowDown", { delay: 10 });
const offset = await getScrollTop(component);
await expect(offset).toBeGreaterThanOrEqual(prev);
prev = offset;
}
await expect(prev).toBeGreaterThan(initial + min);
});
test("vertical end -> start", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
// scroll to the end
await scrollToBottom(component);
// check if offset from end is always keeped
await component.click();
const min = 200;
const initial = await getScrollBottom(component);
let prev = initial;
for (let i = 0; i < 500; i++) {
await page.keyboard.press("ArrowUp", { delay: 10 });
const offset = await getScrollBottom(component);
await expect(offset).toBeGreaterThanOrEqual(prev);
prev = offset;
}
await expect(prev).toBeGreaterThan(initial + min);
});
test("horizontal start -> end", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--horizontal"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("Column 0");
// check if offset from start is always keeped
await component.click();
const min = 200;
const initial = await getScrollLeft(component);
let prev = initial;
for (let i = 0; i < 500; i++) {
await page.keyboard.press("ArrowRight", { delay: 10 });
const offset = await getScrollLeft(component);
await expect(offset).toBeGreaterThanOrEqual(prev);
prev = offset;
}
await expect(prev).toBeGreaterThan(initial + min);
});
test("horizontal end -> start", async ({ page }) => {
await page.goto(storyUrl("basics-vlist--horizontal"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("Column 0");
// scroll to the end
await scrollToRight(component);
// check if offset from end is always keeped
await component.click();
const min = 200;
const initial = await getScrollRight(component);
let prev = initial;
for (let i = 0; i < 500; i++) {
await page.keyboard.press("ArrowLeft", { delay: 10 });
const offset = await getScrollRight(component);
await expect(offset).toBeGreaterThanOrEqual(prev);
prev = offset;
}
await expect(prev).toBeGreaterThan(initial + min);
});
test("resize when its top is out of viewport", async ({ page }) => {
await page.goto(storyUrl("advanced-collapse--collapse-and-scroll"));
const component = await getScrollable(page);
const container = await getVirtualizer(page);
await component.waitForElementState("stable");
expect(
await container.evaluate((e) => e.children.length)
).toBeGreaterThanOrEqual(3);
const targetIndex = 1;
const marginTop = 100;
const getTargetItem = () => {
return container.evaluateHandle((e, i) => {
return Array.from(e.children).find((c) =>
c.textContent!.startsWith(String(i) + "Resize")
)!;
}, targetIndex);
};
const getResizeButton = async () => {
const target = await getTargetItem();
const button = await target.evaluateHandle((e) => {
const buttons = e.querySelectorAll("button");
return buttons[0];
});
expect(await button.textContent()).toBe("Resize");
return button;
};
const getTargetTop = async () => {
const target = await getTargetItem();
return target.evaluate((e) => {
return (e as HTMLElement).offsetTop;
});
};
// resize and check if jump compensation doesn't work
// collapse -> expand
for (let i = 0; i <= 1; i++) {
await scrollTo(component, (await getTargetTop()) + marginTop);
const initialItem = await getFirstItem(component);
expect(initialItem.top).toBeLessThan(0);
expect(initialItem.text).toContain(String(targetIndex));
await page.waitForTimeout(200);
await (await getResizeButton()).click();
await (await getTargetItem()).waitForElementState("stable");
const updatedItem = await getFirstItem(component);
expect(updatedItem.top).toEqual(initialItem.top);
expect(updatedItem.text).toContain(String(targetIndex));
}
});
test("resize at bottom", async ({ page, browserName }) => {
await page.goto(storyUrl("advanced-collapse--two-stage-render"));
const component = await getScrollable(page);
const container = await getVirtualizer(page);
await component.waitForElementState("stable");
await page.waitForTimeout(500);
// should reach to the bottom within the specified number of tries
for (let i = 0; i <= 1; i++) {
// scroll to bottom
await scrollToBottom(component);
const prevBottomItem = getLastItem(component);
// wait for resize completed
await page.waitForTimeout(500);
await container.waitForElementState("stable");
const bottomItem = getLastItem(component);
// check if distance from the bottom isn't changed by resizes
const prevBottom = (await prevBottomItem).bottom;
const bottom = (await bottomItem).bottom;
if (
browserName === "firefox"
? Math.abs(bottom - prevBottom) <= 2
: bottom === prevBottom
) {
// succeeded
return;
}
}
throw new Error(`couldn't reach the bottom`);
});
test("resize with smooth scroll", async ({ page }) => {
await page.goto(storyUrl("advanced-collapse--collapse-and-scroll"));
const component = await getScrollable(page);
const container = await getVirtualizer(page);
await component.waitForElementState("stable");
expect(
await container.evaluate((e) => e.children.length)
).toBeGreaterThanOrEqual(3);
const targetIndex = 1;
const getTargetItem = () => {
return container.evaluateHandle((e, i) => {
return Array.from(e.children).find((c) =>
c.textContent!.startsWith(String(i) + "Resize")
)!;
}, targetIndex);
};
const getResizeAndScrollButton = async () => {
const target = await getTargetItem();
const button = await target.evaluateHandle((e) => {
const buttons = e.querySelectorAll("button");
return buttons[buttons.length - 1];
});
expect(await button.textContent()).toBe("Resize + Smooth Scroll");
return button;
};
const getTargetBottom = async () => {
const target = await getTargetItem();
return target.evaluate((e) => {
return (e as HTMLElement).offsetTop + e.getBoundingClientRect().height;
});
};
// scroll from the upper side
// collapse -> expand
for (let i = 0; i <= 1; i++) {
await scrollTo(component, 0);
const initialItem = await getFirstItem(component);
expect(initialItem.top).toEqual(0);
expect(initialItem.text).not.toContain(String(targetIndex));
await page.waitForTimeout(200);
const scrollListener = listenScrollCount(component, 1000);
await (await getResizeAndScrollButton()).click();
const called = await scrollListener;
await expect(called).toBeGreaterThanOrEqual(2);
const updatedItem = await getFirstItem(component);
expect(updatedItem.top).toEqual(0);
expect(updatedItem.text).toContain(String(targetIndex));
}
// scroll from the lower side
// collapse -> expand
for (let i = 0; i <= 1; i++) {
// collapse
await scrollTo(component, await getTargetBottom());
const initialItem = await getFirstItem(component);
expect(initialItem.top).toEqual(0);
expect(initialItem.text).not.toContain(String(targetIndex));
await page.waitForTimeout(200);
const scrollListener = listenScrollCount(component, 1000);
await (await getResizeAndScrollButton()).click();
const called = await scrollListener;
await expect(called).toBeGreaterThanOrEqual(2);
const updatedItem = await getFirstItem(component);
expect(updatedItem.top).toEqual(0);
expect(updatedItem.text).toContain(String(targetIndex));
}
});
test("stick to bottom", async ({ page }) => {
await page.goto(storyUrl("advanced-chat--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if end is displayed
const initialItem = await getLastItem(component);
expectInRange(initialItem.bottom, { min: 0, max: 1 });
await clearTimer(page);
const button = page.getByRole("button", { name: "submit" });
const textarea = (await page.getByRole("textbox"))!;
// append small item
await button.click();
await component.waitForElementState("stable");
const smallItem = await getLastItem(component);
await expect(smallItem.text).not.toEqual(initialItem.text);
expectInRange(smallItem.bottom, { min: 0, max: 1 });
// append large item
await textarea.clear();
await textarea.fill(
"Hello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\nHello\n"
);
await button.click();
await component.waitForElementState("stable");
const largeItem = await getLastItem(component);
await expect(largeItem.text).not.toEqual(smallItem.text);
expectInRange(largeItem.bottom, { min: 0, max: 1 });
await expect(largeItem.height).toBeGreaterThan(smallItem.height * 10);
});
test("dynamic image", async ({ page, browserName }) => {
await page.goto(storyUrl("advanced-feed--default"));
const component = await getScrollable(page);
await component.waitForElementState("stable");
// TODO firefox is bit unstable
const nearlyZeroMax = browserName === "firefox" ? 2 : 1;
// check if start is displayed
expectInRange((await getFirstItem(component)).top, {
min: 0,
max: nearlyZeroMax,
});
// check if stable after image load
await page.waitForTimeout(3000);
expectInRange((await getFirstItem(component)).top, {
min: 0,
max: nearlyZeroMax,
});
// scroll to top
await scrollTo(component, 0);
// wait for prepending
await component.evaluate((e) => {
let timer: null | ReturnType<typeof setTimeout> = null;
return new Promise<void>((resolve, reject) => {
let prepended = false;
if (e.scrollTop !== 0) {
reject();
return;
}
const cb = () => {
if (e.scrollTop > 1000) {
prepended = true;
} else {
if (!prepended) {
return;
}
}
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(() => {
e.removeEventListener("scroll", cb);
resolve();
}, 2000);
};
e.addEventListener("scroll", cb);
});
});
// check if stable after prepending
expectInRange((await getFirstItem(component)).top, {
min: 0,
max: nearlyZeroMax,
});
});
});
test.describe("check if scrollToIndex works", () => {
test.beforeEach(async ({ page }) => {
await page.goto(storyUrl("basics-vlist--scroll-to"));
});
test.describe("align start", () => {
test("mid", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("700");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const firstItem = await getFirstItem(component);
await expect(firstItem.text).toEqual("700");
await expect(firstItem.top).toEqual(0);
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("650");
await expect(await component.innerText()).not.toContain("750");
});
test("start", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("500");
await button.click();
await component.waitForElementState("stable");
await expect(await component.innerText()).toContain("500");
await clearInput(input);
await input.fill("0");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const firstItem = await getFirstItem(component);
await expect(firstItem.text).toEqual("0");
await expect(firstItem.top).toEqual(0);
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("50\n");
});
test("end", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("999");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const lastItem = await getLastItem(component);
await expect(lastItem.text).toEqual("999");
expectInRange(lastItem.bottom, { min: -0.9, max: 1 });
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("949");
});
test("mid smooth", async ({ page, browserName }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
await page.getByRole("checkbox", { name: "smooth" }).click();
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
const scrollListener = listenScrollCount(component);
await clearInput(input);
await input.fill("700");
await button.click();
await page.waitForTimeout(500);
const called = await scrollListener;
// Check if this is smooth scrolling
await expect(called).toBeGreaterThanOrEqual(
// TODO find better way to check in webkit
browserName === "webkit" ? 2 : 10
);
// Check if scrolled precisely
const firstItem = await getFirstItem(component);
await expect(firstItem.text).toEqual("700");
expectInRange(firstItem.top, { min: 0, max: 1 });
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("650");
await expect(await component.innerText()).not.toContain("750");
});
});
test.describe("align end", () => {
test.beforeEach(async ({ page }) => {
await page.getByRole("radio", { name: "end" }).click();
});
test("mid", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("700");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const lastItem = await getLastItem(component);
await expect(lastItem.text).toEqual("700");
expectInRange(lastItem.bottom, { min: 0, max: 1 });
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("650");
await expect(await component.innerText()).not.toContain("750");
});
test("start", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("500");
await button.click();
await component.waitForElementState("stable");
await expect(await component.innerText()).toContain("500");
await clearInput(input);
await input.fill("0");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const firstItem = await getFirstItem(component);
await expect(firstItem.text).toEqual("0");
await expect(firstItem.top).toEqual(0);
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("50\n");
});
test("end", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
await clearInput(input);
await input.fill("999");
await button.click();
await component.waitForElementState("stable");
// Check if scrolled precisely
const lastItem = await getLastItem(component);
await expect(lastItem.text).toEqual("999");
expectInRange(lastItem.bottom, { min: 0, max: 1 });
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("949");
});
test("mid smooth", async ({ page, browserName }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
await page.getByRole("checkbox", { name: "smooth" }).click();
const button = page.getByRole("button", { name: "scroll to index" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
const scrollListener = listenScrollCount(component);
await clearInput(input);
await input.fill("700");
await button.click();
await page.waitForTimeout(500);
const called = await scrollListener;
// Check if this is smooth scrolling
await expect(called).toBeGreaterThanOrEqual(
// TODO find better way to check in webkit
browserName === "webkit" ? 2 : 10
);
// Check if scrolled precisely
const lastItem = await getLastItem(component);
await expect(lastItem.text).toEqual("700");
expectInRange(lastItem.bottom, { min: 0, max: 1 });
// Check if unnecessary items are not rendered
await expect(await component.innerText()).not.toContain("650");
await expect(await component.innerText()).not.toContain("750");
});
});
});
test.describe("check if scrollTo works", () => {
test.beforeEach(async ({ page }) => {
await page.goto(storyUrl("basics-vlist--scroll-to"));
});
test("down and up", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", { name: "scroll to offset" });
const input = await button.evaluateHandle(
(el) => el.previousSibling as HTMLInputElement
);
// scroll down
await clearInput(input);
await input.fill("5000");
await button.click();
await component.waitForElementState("stable");
await expect(
// scrollTo may not scroll to exact position with dynamic sized items
approxymate(await getScrollTop(component))
).toEqual(5000);
// scroll up
await clearInput(input);
await input.fill("1000");
await button.click();
await component.waitForElementState("stable");
await expect(
// scrollTo may not scroll to exact position with dynamic sized items
approxymate(await getScrollTop(component))
).toEqual(1000);
});
});
test.describe("check if scrollBy works", () => {
test.beforeEach(async ({ page }) => {
await page.goto(storyUrl("basics-vlist--scroll-to"));
});
test("down and up", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
// check if start is displayed
await expect((await getFirstItem(component)).text).toEqual("0");
const button = page.getByRole("button", {
name: "scroll by offset",
});
const input = await button.evaluateHandle(
(el) => el.previousSibling!.previousSibling as HTMLInputElement
);
// scroll down
await clearInput(input);
await input.fill("1234");
await button.click();
await component.waitForElementState("stable");
await expect(await getScrollTop(component)).toEqual(1234);
// scroll up
await clearInput(input);
await input.fill("-234");
await button.click();
await component.waitForElementState("stable");
await expect(await getScrollTop(component)).toEqual(1000);
});
});
test.describe("check if item shift compensation works", () => {
test.beforeEach(async ({ page }) => {
await page.goto(storyUrl("basics-vlist--increasing-items"));
});
test("keep end at mid when add to/remove from end", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
const updateButton = page.getByRole("button", { name: "update" });
// fill list and move to mid
for (let i = 0; i < 20; i++) {
await updateButton.click();
}
await scrollBy(component, 400);
await page.waitForTimeout(500);
const topItem = await getFirstItem(component);
expect(topItem.text).not.toEqual("0");
expect(topItem.text.length).toBeLessThanOrEqual(2);
// add
await page.getByRole("radio", { name: "increase" }).click();
await updateButton.click();
await page.waitForTimeout(100);
// check if visible item is keeped
expect(topItem).toEqual(await getFirstItem(component));
// remove
await page.getByRole("radio", { name: "decrease" }).click();
await updateButton.click();
await page.waitForTimeout(100);
// check if visible item is keeped
expect(topItem).toEqual(await getFirstItem(component));
});
test("keep start at mid when add to/remove from start", async ({ page }) => {
const component = await getScrollable(page);
await component.waitForElementState("stable");
const updateButton = page.getByRole("button", { name: "update" });
// fill list and move to mid
for (let i = 0; i < 20; i++) {
await updateButton.click();
}
await scrollBy(component, 800);
await page.waitForTimeout(500);