-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest.browser.js
More file actions
817 lines (688 loc) · 27.2 KB
/
test.browser.js
File metadata and controls
817 lines (688 loc) · 27.2 KB
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
import { render, fireEvent, waitFor, cleanup } from "@marko/testing-library";
import {
afterEach,
beforeEach,
afterAll,
beforeAll,
describe,
it,
expect,
vi,
} from "vitest";
import { fastAnimations } from "../../../common/test-utils/browser";
import template from "../index.marko";
import * as mock from "./mock";
import * as scrollTransitionModule from "../utils/scroll-transition";
beforeAll(() => fastAnimations.start());
afterAll(() => fastAnimations.stop());
afterEach(cleanup);
beforeEach(() => {
vi.useFakeTimers({
toFake: [
"setTimeout",
"clearTimeout",
"setImmediate",
"clearImmediate",
"setInterval",
"clearInterval",
"requestAnimationFrame",
"cancelAnimationFrame",
],
shouldAdvanceTime: true,
});
vi.mock("../utils/scroll-transition", { spy: true });
vi.mocked(scrollTransitionModule.scrollTransition).mockImplementation(
(el, to, fn) => {
el.scrollLeft = to;
fn();
return () => {};
},
);
});
afterEach(() => {
vi.clearAllTimers();
vi.resetAllMocks();
});
/** @type import("@marko/testing-library").RenderResult */
let component;
// TODO: should be testing the tab-index of children, would need to change test setup a bit.
// The above test should also be tested when resizing the carousel.
describe("given a continuous carousel", () => {
describe("without any items", () => {
const input = mock.continuous0Items;
beforeEach(async () => {
component = await render(template, input);
});
it("then prev and next controls are disabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).toHaveAttribute("aria-disabled", "true");
expect(
component.getByLabelText(input.a11yNextText),
).toHaveAttribute("aria-disabled", "true");
});
});
describe("with 1 item (single slide)", () => {
const input = mock.continuous1Item;
beforeEach(async () => {
component = await render(template, input);
});
it("then prev and next controls are disabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).toHaveAttribute("aria-disabled", "true");
expect(
component.getByLabelText(input.a11yNextText),
).toHaveAttribute("aria-disabled", "true");
});
});
describe("with 6 items at the beginning", () => {
const input = mock.continuous6Items;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
describe("when it is rerendered to show the second item", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: 1 }),
);
await waitForCarouselUpdate();
});
it("then it moved to the second item", async () => {
await waitFor(() => {
const secondItem = component.getByText(
input.item[1].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
});
});
describe("when it is rerendered with an index below zero", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: -1 }),
);
await doesNotEventuallyScroll();
});
it("then shows the first item", () => {
const firstItem = component.getByText(
input.item[0].renderBody.text,
);
assertAtStartOfSlide(firstItem);
});
});
describe("when it is rerendered with an index higher than the number of items", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: 6 }),
);
await doesNotEventuallyScroll();
});
it("then shows the first item", () => {
const firstItem = component.getByText(
input.item[0].renderBody.text,
);
assertAtStartOfSlide(firstItem);
});
});
describe("when the previous button is clicked while disabled", () => {
beforeEach(async () => {
await fireEvent.click(
component.getByLabelText(input.a11yPreviousText),
);
});
it("then it did not emit the prev event", () => {
expect(component.emitted("previous")).has.length(0);
});
});
describe("when next button is clicked", () => {
let nextHiddenItem;
beforeEach(async () => {
nextHiddenItem = input.item
.map((item) => component.getByText(item.renderBody.text))
.find((el) => el.hasAttribute("aria-hidden"));
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
});
it("then it emitted the next event", () => {
expect(component.emitted("next")).has.length(1);
});
it("then it moved to the next hidden item", () => {
assertAtStartOfSlide(nextHiddenItem);
});
});
});
describe("with 6 items at the end", () => {
const input = Object.assign({}, mock.continuous6Items, { index: 5 });
beforeEach(async () => {
component = await render(template, input);
await waitForCarouselUpdate();
});
describe("when the next button is clicked while disabled", () => {
beforeEach(async () => {
await fireEvent.click(
component.getByLabelText(input.a11yNextText),
);
});
it("then it did not emit the next event", () => {
expect(component.emitted("next")).has.length(0);
});
});
describe("when previous button is clicked", () => {
let previousHiddenItem;
beforeEach(async () => {
previousHiddenItem = input.item
.map((item) => component.getByText(item.renderBody.text))
.reverse()
.find((el) => el.hasAttribute("aria-hidden"));
fireEvent.click(
component.getByLabelText(input.a11yPreviousText),
);
await waitForCarouselUpdate();
});
it("then it emitted the previous event", () => {
expect(component.emitted("previous")).has.length(1);
});
it("then it moved to the previous hidden item", () => {
assertAtEndOfSlide(previousHiddenItem);
});
});
});
describe("with 12 items", () => {
const input = mock.continuous12Items;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
describe("when next button is clicked three times", () => {
beforeEach(async () => {
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
});
it("then emitted 3 next events", () => {
expect(component.emitted("next")).has.length(3);
});
it("then the last item is visible", () => {
const lastItem = component.getByText(
input.item[input.item.length - 1].renderBody.text,
);
assertAtEndOfSlide(lastItem);
});
});
describe("when next button is clicked three times, and previous button is clicked once", () => {
beforeEach(async () => {
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
fireEvent.click(
component.getByLabelText(input.a11yPreviousText),
);
await waitForCarouselUpdate();
});
it("then emitted 3 next events", () => {
expect(component.emitted("next")).has.length(3);
});
it("then emitted a prev event", () => {
expect(component.emitted("previous")).has.length(1);
});
it("then the last item is not visible", () => {
const lastItem = component.getByText(
input.item[input.item.length - 1].renderBody.text,
);
assertIsNotVisible(lastItem);
});
});
});
});
describe("given a discrete carousel", () => {
describe("without any items", () => {
const input = mock.discrete1PerSlide0Items;
beforeEach(async () => {
component = await render(template, input);
});
it("then prev and next controls are disabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).toHaveAttribute("aria-disabled", "true");
expect(
component.getByLabelText(input.a11yNextText),
).toHaveAttribute("aria-disabled", "true");
});
});
describe("with 1 item per slide and 1 item", () => {
const input = mock.discrete1PerSlide1Items;
beforeEach(async () => {
component = await render(template, input);
});
it("then prev and next controls are disabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).toHaveAttribute("aria-disabled", "true");
expect(
component.getByLabelText(input.a11yNextText),
).toHaveAttribute("aria-disabled", "true");
});
});
describe("with 1 item per slide and 3 items at the beginning", () => {
const input = mock.discrete1PerSlide3Items;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
it("then prev control is disabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).toHaveAttribute("aria-disabled", "true");
});
describe("when it is rerendered to show the second item", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: 1 }),
);
await waitForCarouselUpdate();
});
it("then it moved to the second item", () => {
const secondItem = component.getByText(
input.item[1].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
});
describe("when it is rerendered with an index below zero", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: -1 }),
);
await doesNotEventuallyScroll();
});
it("then shows the first item", () => {
const firstItem = component.getByText(
input.item[0].renderBody.text,
);
assertAtStartOfSlide(firstItem);
});
});
describe("when it is rerendered with an index higher than the number of items", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { index: 6 }),
);
await doesNotEventuallyScroll();
});
it("then shows the first item", () => {
const firstItem = component.getByText(
input.item[0].renderBody.text,
);
assertAtStartOfSlide(firstItem);
});
});
describe("when the previous button is clicked while disabled", () => {
beforeEach(async () => {
await fireEvent.click(
component.getByLabelText(input.a11yPreviousText),
);
});
it("then it did not emit the prev event", () => {
expect(component.emitted("previous")).has.length(0);
});
});
describe("when next button is clicked", () => {
beforeEach(async () => {
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
});
it("then it emitted the next event", () => {
expect(component.emitted("next")).has.length(1);
});
it("then it emitted the slide event", () => {
expect(component.emitted("slide")).has.nested.property(
"[0][0].slide",
2,
);
});
thenItMovedToTheSecondSlide();
});
//
describe("when it is scrolled to the second slide", () => {
beforeEach(async () => {
const thirdItem = component.getByText(
input.item[1].renderBody.text,
);
const list = thirdItem.parentElement;
list.scrollLeft = thirdItem.offsetLeft;
fireEvent.scroll(list);
await waitForCarouselUpdate();
});
it("then it emitted the scroll event", () => {
expect(component.emitted("scroll")).has.length(1);
});
thenItMovedToTheSecondSlide();
});
function thenItMovedToTheSecondSlide() {
it("then it moved to the second item", () => {
const secondItem = component.getByText(
input.item[1].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
it("then prev and next controls are enabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).not.toHaveAttribute("aria-disabled");
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled");
});
}
});
describe("with 1 item per slide and 3 items at the end", () => {
const input = Object.assign({}, mock.discrete1PerSlide3Items, {
index: 2,
});
beforeEach(async () => {
component = await render(template, input);
await waitForCarouselUpdate();
});
it("then next control is disabled", () => {
expect(
component.getByLabelText(input.a11yNextText),
).toHaveAttribute("aria-disabled", "true");
});
describe("when previous button is clicked", () => {
beforeEach(async () => {
fireEvent.click(
component.getByLabelText(input.a11yPreviousText),
);
await waitForCarouselUpdate();
});
it("then it emitted the previous event", () => {
expect(component.emitted("previous")).has.length(1);
});
it("then it emitted the slide event", () => {
expect(component.emitted("slide")).has.nested.property(
"[0][0].slide",
2,
);
});
it("then it moved to the second item", () => {
const secondItem = component.getByText(
input.item[1].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
it("then prev and next controls are enabled", () => {
expect(
component.getByLabelText(input.a11yPreviousText),
).not.toHaveAttribute("aria-disabled");
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled");
});
});
});
describe("with 2 items per slide and 6 items at the beginning", () => {
const input = mock.discrete2PerSlide6Items;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
describe("when next button is clicked", () => {
beforeEach(async () => {
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
});
it("then it emitted the next event", () => {
expect(component.emitted("next")).has.length(1);
});
thenItMovedToTheSecondSlide();
});
function thenItMovedToTheSecondSlide() {
it("then it emitted the slide event", () => {
expect(component.emitted("slide")).has.length(1);
});
it("then it moved to the third item", () => {
const secondItem = component.getByText(
input.item[2].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
}
});
describe("with 2.1 items per slide and 3 items at the beginning", () => {
const input = mock.discrete21PerSlide3Items;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
it("then it shows part of the next slide", () => {
const thirdItem = component.getByText(
input.item[2].renderBody.text,
);
const list = thirdItem.parentElement;
const { right: slideRight } = list.getBoundingClientRect();
const { left: itemLeft, right: itemRight } =
thirdItem.getBoundingClientRect();
expect(itemLeft).lt(slideRight);
expect(itemRight).gt(slideRight);
});
describe("when next button is clicked", () => {
beforeEach(async () => {
fireEvent.click(component.getByLabelText(input.a11yNextText));
await waitForCarouselUpdate();
});
it("then it emitted the next event", () => {
expect(component.emitted("next")).has.length(1);
});
it("then it emitted the slide event", () => {
expect(component.emitted("slide")).has.length(1);
});
it("then it moved to the third item", () => {
const secondItem = component.getByText(
input.item[2].renderBody.text,
);
assertAtStartOfSlide(secondItem);
});
});
});
describe("with autoplay enabled", () => {
const input = mock.discrete1PerSlide3ItemsAutoPlay;
beforeEach(async () => {
component = await render(template, input);
// The carousel is not fully initialized until
// the next button is no longer disabled.
await waitFor(() =>
expect(
component.getByLabelText(input.a11yNextText),
).not.toHaveAttribute("aria-disabled"),
);
});
describe("when the autoplay runs twice", async () => {
beforeEach(async () => {
await waitForCarouselUpdate();
await waitForCarouselUpdate();
});
it("then it does not emit next or slide events", () => {
expect(component.emitted("next")).has.length(0);
expect(component.emitted("slide")).has.length(0);
});
it("then it moved to the third item", () => {
const thirdItem = component.getByText(
input.item[2].renderBody.text,
);
assertAtStartOfSlide(thirdItem);
});
describe("when auto play runs at the end", () => {
beforeEach(async () => {
await waitForCarouselUpdate();
});
it("then it does not emit the next event", () => {
expect(component.emitted("next")).has.length(0);
});
thenItIsOnTheFirstSlide();
});
describe("when next is clicked at the end", () => {
beforeEach(async () => {
fireEvent.click(
component.getByLabelText(input.a11yNextText),
);
await waitForCarouselUpdate();
});
it("then it emitted the next event", () => {
expect(component.emitted("next")).has.length(1);
});
it("then it emitted the slide event", () => {
expect(component.emitted("slide")).has.length(1);
});
thenItIsOnTheFirstSlide();
});
});
describe("when it is paused", () => {
beforeEach(async () => {
await component.rerender(
Object.assign({}, input, { paused: true }),
);
vi.advanceTimersByTime(600);
});
it("then it did not emit any updates", () => {
expect(component.emitted("move")).has.length(0);
});
thenItIsOnTheFirstSlide();
});
describe("when it is interacted with", () => {
beforeEach(async () => {
await fireEvent.mouseOver(component.getByRole("list"));
});
it("then the autoplay does not run", async () => {
vi.advanceTimersByTime(600);
expect(component.emitted("move")).has.length(0);
});
describe("when the interaction has finished", () => {
beforeEach(async () => {
await fireEvent.mouseOut(component.getByRole("list"));
});
it("then it does autoplay", async () => {
await waitForCarouselUpdate();
});
});
});
function thenItIsOnTheFirstSlide() {
it("then it is displaying the first item", () => {
const firstItem = component.getByText(
input.item[0].renderBody.text,
);
assertAtStartOfSlide(firstItem);
});
}
});
});
function waitForCarouselUpdate() {
return waitFor(() => expect(component.emitted("move")).has.length(1));
}
function doesNotEventuallyScroll() {
const err = new Error("Page should not have been scrolled");
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
window.removeEventListener("scroll", handleScroll);
resolve();
}, 200);
window.addEventListener("scroll", handleScroll, true);
function handleScroll() {
clearTimeout(timeout);
window.removeEventListener("scroll", handleScroll);
reject(err);
}
vi.advanceTimersByTime(400);
});
}
function assertIsNotVisible(item) {
const itemBounds = item.getBoundingClientRect();
const parentBounds = item.parentElement.getBoundingClientRect();
if (
itemBounds.left < parentBounds.left ||
itemBounds.right > parentBounds.right
) {
return;
}
throw new Error(
`Expected item ${getChildIndex(item)} to not be displayed.`,
);
}
function assertAtStartOfSlide(item) {
const list = item.parentElement;
const container = list.parentElement;
const itemBounds = item.getBoundingClientRect();
const containerBounds = container.getBoundingClientRect();
// Check if item is at the very start of the carousel.
if (Math.trunc(itemBounds.left) === Math.trunc(containerBounds.left)) {
return;
}
// Also accept if the carousel has scrolled as much as possible.
const lastItemBounds = list.lastElementChild.getBoundingClientRect();
if (Math.trunc(lastItemBounds.right) <= Math.trunc(containerBounds.right)) {
return;
}
throw new Error(
`Expected item ${getChildIndex(
item,
)} to be at the beginning of the carousel.`,
);
}
function assertAtEndOfSlide(item) {
const list = item.parentElement;
const container = list.parentElement;
const itemBounds = item.getBoundingClientRect();
const parentBounds = container.getBoundingClientRect();
// Check if item is at the very end of the carousel.
if (Math.trunc(itemBounds.right) === Math.trunc(parentBounds.right)) {
return;
}
// Also accept if the carousel has scrolled as much as possible.
const firstItemBounds = list.firstElementChild.getBoundingClientRect();
if (Math.trunc(firstItemBounds.left) <= Math.trunc(parentBounds.left)) {
return;
}
throw new Error(
`Expected item ${getChildIndex(item)} to be at the end of the carousel.`,
);
}
function getChildIndex(child) {
return [].indexOf.call(child.parentElement.children, child);
}