-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcarousel.spec.ts
More file actions
1143 lines (925 loc) · 37 KB
/
carousel.spec.ts
File metadata and controls
1143 lines (925 loc) · 37 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
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 {
elementUpdated,
expect,
fixture,
html,
nextFrame,
waitUntil,
} from '@open-wc/testing';
import { type SinonFakeTimers, spy, stub, useFakeTimers } from 'sinon';
import IgcButtonComponent from '../button/button.js';
import {
arrowLeft,
arrowRight,
endKey,
enterKey,
homeKey,
spaceBar,
} from '../common/controllers/key-bindings.js';
import { defineComponents } from '../common/definitions/defineComponents.js';
import {
finishAnimationsFor,
simulateClick,
simulateKeyboard,
simulateLostPointerCapture,
simulatePointerDown,
simulatePointerMove,
} from '../common/utils.spec.js';
import IgcCarouselComponent from './carousel.js';
import IgcCarouselIndicatorComponent from './carousel-indicator.js';
import IgcCarouselSlideComponent from './carousel-slide.js';
describe('Carousel', () => {
before(() => {
defineComponents(IgcCarouselComponent);
});
const DIFF_OPTIONS = {
ignoreAttributes: ['id'],
};
async function slideChangeComplete(
current: IgcCarouselSlideComponent,
next: IgcCarouselSlideComponent
) {
finishAnimationsFor(current.shadowRoot!);
finishAnimationsFor(next.shadowRoot!);
await elementUpdated(carousel);
await nextFrame();
await nextFrame();
}
const createCarouselComponent = () => html`
<igc-carousel>
<igc-carousel-slide>
<span>1</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>2</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>3</span>
</igc-carousel-slide>
</igc-carousel>
`;
let carousel: IgcCarouselComponent;
let slides: IgcCarouselSlideComponent[];
let carouselSlidesContainer: Element;
let nextButton: IgcButtonComponent;
let prevButton: IgcButtonComponent;
let defaultIndicators: IgcCarouselIndicatorComponent[];
let clock: SinonFakeTimers;
beforeEach(async () => {
carousel = await fixture<IgcCarouselComponent>(createCarouselComponent());
slides = Array.from(
carousel.querySelectorAll(IgcCarouselSlideComponent.tagName)
);
[prevButton, nextButton] = carousel.renderRoot.querySelectorAll(
IgcButtonComponent.tagName
);
defaultIndicators = Array.from(
carousel.renderRoot.querySelectorAll(
IgcCarouselIndicatorComponent.tagName
)
);
});
describe('Initialization', () => {
it('passes the a11y audit', async () => {
await expect(carousel).to.be.accessible();
await expect(carousel).shadowDom.to.be.accessible();
});
it('is correctly initialized with its default component state', () => {
expect(carousel.disableLoop).to.be.false;
expect(carousel.disablePauseOnInteraction).to.be.false;
expect(carousel.hideNavigation).to.be.false;
expect(carousel.hideIndicators).to.be.false;
expect(carousel.vertical).to.be.false;
expect(carousel.indicatorsOrientation).to.equal('end');
expect(carousel.interval).to.be.undefined;
expect(carousel.maximumIndicatorsCount).to.equal(10);
expect(carousel.animationType).to.equal('slide');
expect(carousel.total).to.equal(slides.length);
expect(carousel.current).to.equal(0);
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.false;
});
it('is rendered correctly', () => {
expect(carousel).dom.to.equal(
`<igc-carousel>
<igc-carousel-slide active>
<span>1</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>2</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>3</span>
</igc-carousel-slide>
</igc-carousel>`,
DIFF_OPTIONS
);
const carouselId = carousel.shadowRoot?.querySelector(
'div[aria-live="polite"]'
)?.id;
expect(carousel).shadowDom.to.equal(
`<section>
<igc-button aria-label="Previous slide" aria-controls="${carouselId}">
<slot name="previous-button">
<igc-icon aria-hidden="true" collection="default" name="carousel_prev"></igc-icon>
</slot>
</igc-button>
<igc-button aria-label="Next slide" aria-controls="${carouselId}">
<slot name="next-button">
<igc-icon aria-hidden="true" collection="default" name="carousel_next"></igc-icon>
</slot>
</igc-button>
<igc-carousel-indicator-container>
<div role="tablist">
<slot name="indicator">
<igc-carousel-indicator
role="tab"
tabindex="0"
slot="indicator"
exportparts="indicator, active, inactive"
>
<div></div>
<div slot="active"></div>
</igc-carousel-indicator>
<igc-carousel-indicator
role="tab"
tabindex="-1"
slot="indicator"
exportparts="indicator, active, inactive"
>
<div></div>
<div slot="active"></div>
</igc-carousel-indicator>
<igc-carousel-indicator
role="tab"
tabindex="-1"
slot="indicator"
exportparts="indicator, active, inactive"
>
<div></div>
<div slot="active"></div>
</igc-carousel-indicator>
</slot>
</div>
</igc-carousel-indicator-container>
<div id="${carouselId}" aria-live="polite">
<slot></slot>
</div>
</section>`,
{
ignoreAttributes: ['type', 'variant', 'part', 'style'],
}
);
});
it('slide is correctly rendered both in active/inactive states', async () => {
expect(slides[0]).dom.to.equal(
`<igc-carousel-slide active>
<span>1</span>
</igc-carousel-slide>`,
DIFF_OPTIONS
);
expect(slides[1]).dom.to.equal(
`<igc-carousel-slide>
<span>2</span>
</igc-carousel-slide>`,
DIFF_OPTIONS
);
slides[1].active = true;
await elementUpdated(slides[1]);
expect(slides[0]).dom.to.equal(
`<igc-carousel-slide>
<span>1</span>
</igc-carousel-slide>`,
DIFF_OPTIONS
);
expect(slides[1]).dom.to.equal(
`<igc-carousel-slide active>
<span>2</span>
</igc-carousel-slide>`,
DIFF_OPTIONS
);
});
it('should not render indicators if `hideIndicators` is true', async () => {
let indicators = carousel.shadowRoot?.querySelector(
'div[role="tablist"]'
);
expect(indicators).to.not.be.null;
carousel.hideIndicators = true;
await elementUpdated(carousel);
indicators = carousel.shadowRoot?.querySelector('div[role="tablist"]');
expect(indicators).to.be.null;
});
it('should not render navigation if `hideNavigation` is true', async () => {
let navigation = carousel.shadowRoot?.querySelectorAll('igc-button');
expect(navigation?.length).to.equal(2);
carousel.hideNavigation = true;
await elementUpdated(carousel);
navigation = carousel.shadowRoot?.querySelectorAll('igc-button');
expect(navigation?.length).to.equal(0);
});
it('should render indicators label if slides count is greater than `maximumIndicatorsCount`', async () => {
let label = carousel.shadowRoot?.querySelector(
'div[part="label indicators"]'
);
expect(label).to.be.null;
carousel.maximumIndicatorsCount = 2;
await elementUpdated(carousel);
label = carousel.shadowRoot?.querySelector(
'div[part="label indicators"]'
);
expect(label).to.not.be.null;
expect(label?.textContent?.trim()).to.equal('1 of 3');
// slidesLabelFormat integration
carousel.slidesLabelFormat = 'Showing picture {0} of {1} total slides';
await elementUpdated(carousel);
expect(label?.textContent?.trim()).to.equal(
'Showing picture 1 of 3 total slides'
);
});
it('should not render indicators label if `hideIndicators` is true', async () => {
let label = carousel.shadowRoot?.querySelector(
'div[part="label indicators"]'
);
expect(label).to.be.null;
carousel.maximumIndicatorsCount = 2;
await elementUpdated(carousel);
label = carousel.shadowRoot?.querySelector(
'div[part="label indicators"]'
);
expect(label).to.not.be.null;
expect(label?.textContent?.trim()).to.equal('1 of 3');
carousel.hideIndicators = true;
await elementUpdated(carousel);
label = carousel.shadowRoot?.querySelector(
'div[part="label indicators"]'
);
expect(label).to.be.null;
});
it('should set the first slide as active if none is set initially', async () => {
// if none is set initially
expect(carousel.current).to.equal(0);
// if set initially, nothing changes
carousel = await fixture<IgcCarouselComponent>(
html`<igc-carousel>
<igc-carousel-slide>
<span>1</span>
</igc-carousel-slide>
<igc-carousel-slide active>
<span>2</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>3</span>
</igc-carousel-slide>
</igc-carousel>`
);
expect(carousel.current).to.equal(1);
});
it('should set the active slide to be the last one if there are multiple active slides', async () => {
// on initial rendering
carousel = await fixture<IgcCarouselComponent>(
html`<igc-carousel>
<igc-carousel-slide active>
<span>1</span>
</igc-carousel-slide>
<igc-carousel-slide active>
<span>2</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>3</span>
</igc-carousel-slide>
</igc-carousel>`
);
expect(carousel.current).to.equal(1);
// when adding active slides runtime
const slide = document.createElement(IgcCarouselSlideComponent.tagName);
slide.setAttribute('active', '');
carousel.appendChild(slide);
await elementUpdated(carousel);
expect(carousel.total).to.equal(4);
expect(carousel.current).to.equal(3);
});
});
describe('Methods', () => {
it('calls `play`, `pause` methods successfully', () => {
carousel.play();
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
carousel.pause();
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
});
it('calls `next`, `prev` methods successfully', async () => {
carousel = await fixture<IgcCarouselComponent>(
html`<igc-carousel>
<igc-carousel-slide>
<span>1</span>
</igc-carousel-slide>
<igc-carousel-slide>
<span>2</span>
</igc-carousel-slide>
</igc-carousel>`
);
carousel.disableLoop = true;
await elementUpdated(carousel);
let animation = await carousel.next();
expect(animation).to.be.true;
expect(carousel.current).to.equal(1);
animation = await carousel.next();
expect(animation).to.be.false;
expect(carousel.current).to.equal(1);
animation = await carousel.prev();
expect(animation).to.be.true;
expect(carousel.current).to.equal(0);
animation = await carousel.prev();
expect(animation).to.be.false;
expect(carousel.current).to.equal(0);
});
it('calls `select` method successfully', async () => {
expect(carousel.current).to.equal(0);
// select current slide
let animation = await carousel.select(slides[0]);
expect(animation).to.be.false;
expect(carousel.current).to.equal(0);
// select invalid slide
animation = await carousel.select(slides[3]);
expect(animation).to.be.false;
expect(carousel.current).to.equal(0);
// select last slide
animation = await carousel.select(slides[2]);
expect(animation).to.be.true;
expect(carousel.current).to.equal(2);
// select current slide by index
animation = await carousel.select(2);
expect(animation).to.be.false;
expect(carousel.current).to.equal(2);
// select invalid slide by index
animation = await carousel.select(3);
expect(animation).to.be.false;
expect(carousel.current).to.equal(2);
// select fist slide by index
animation = await carousel.select(0);
expect(animation).to.be.true;
expect(carousel.current).to.equal(0);
});
});
describe('Slots', () => {
beforeEach(async () => {
carousel = await fixture<IgcCarouselComponent>(
html`<igc-carousel>
<span slot="previous-button">left</span>
<span slot="next-button">right</span>
<igc-carousel-indicator>
<span>empty</span>
<span slot="active">full</span>
</igc-carousel-indicator>
<igc-carousel-slide>
<span>1</span>
</igc-carousel-slide>
</igc-carousel>`
);
nextButton = carousel.shadowRoot?.querySelectorAll(
'igc-button'
)[1] as IgcButtonComponent;
prevButton = carousel.shadowRoot?.querySelectorAll(
'igc-button'
)[0] as IgcButtonComponent;
});
it('should slot previous button icon', async () => {
const slottedContent = prevButton
?.querySelector('slot')
?.assignedNodes()[0];
expect(slottedContent?.nodeName.toLocaleLowerCase()).to.equal('span');
expect(slottedContent?.textContent).to.equal('left');
});
it('should slot next button icon', async () => {
const slottedContent = nextButton
?.querySelector('slot')
?.assignedNodes()[0];
expect(slottedContent?.nodeName.toLocaleLowerCase()).to.equal('span');
expect(slottedContent?.textContent).to.equal('right');
});
it('should slot indicator', async () => {
const indicator = carousel?.querySelector('igc-carousel-indicator');
expect(indicator).dom.to.equal(
`<igc-carousel-indicator
slot="indicator"
role="tab"
tabindex="0"
>
<span>empty</span>
<span slot="active">full</span>
</igc-carousel-indicator>`,
{
ignoreAttributes: ['aria-controls'],
}
);
expect(indicator).shadowDom.to.equal(
`<div part="indicator inactive">
<slot></slot>
</div>
<div part="indicator active">
<slot name="active"></slot>
</div>`,
{
ignoreAttributes: ['style'],
}
);
});
});
describe('Interactions', () => {
describe('Click', () => {
it('should change slide when clicking next button', async () => {
const eventSpy = spy(carousel, 'emitEvent');
expect(carousel.current).to.equal(0);
expect(defaultIndicators[0].active).to.be.true;
simulateClick(nextButton!);
await waitUntil(() => eventSpy.calledWith('igcSlideChanged'));
expect(carousel.current).to.equal(1);
expect(defaultIndicators[0].active).to.be.false;
expect(defaultIndicators[1].active).to.be.true;
expect(eventSpy.firstCall).calledWith('igcSlideChanged', { detail: 1 });
});
it('should change slide when clicking previous button', async () => {
const eventSpy = spy(carousel, 'emitEvent');
expect(carousel.current).to.equal(0);
expect(defaultIndicators[0].active).to.be.true;
simulateClick(prevButton!);
await waitUntil(() => eventSpy.calledWith('igcSlideChanged'));
expect(carousel.current).to.equal(2);
expect(defaultIndicators[0].active).to.be.false;
expect(defaultIndicators[2].active).to.be.true;
expect(eventSpy.firstCall).calledWith('igcSlideChanged', { detail: 2 });
});
it('should change slide when clicking indicators', async () => {
const eventSpy = spy(carousel, 'emitEvent');
expect(carousel.current).to.equal(0);
expect(defaultIndicators[0].active).to.be.true;
// select second slide
simulateClick(defaultIndicators[1]);
await waitUntil(() =>
eventSpy.calledWith('igcSlideChanged', { detail: 1 })
);
expect(carousel.current).to.equal(1);
expect(defaultIndicators[0].active).to.be.false;
expect(defaultIndicators[1].active).to.be.true;
expect(eventSpy.firstCall).calledWith('igcSlideChanged', { detail: 1 });
// select first slide
simulateClick(defaultIndicators[0]);
await waitUntil(() =>
eventSpy.calledWith('igcSlideChanged', { detail: 0 })
);
expect(carousel.current).to.equal(0);
expect(defaultIndicators[0].active).to.be.true;
expect(defaultIndicators[1].active).to.be.false;
expect(eventSpy.secondCall).calledWith('igcSlideChanged', {
detail: 0,
});
});
it('should properly call `igcSlideChanged` event', async () => {
const eventSpy = spy(carousel, 'emitEvent');
stub(carousel, 'select')
.onFirstCall()
.resolves(true)
.onSecondCall()
.resolves(false);
// select second indicator
simulateClick(defaultIndicators[1]);
await slideChangeComplete(slides[0], slides[1]);
// select second indicator again
simulateClick(defaultIndicators[1]);
await slideChangeComplete(slides[0], slides[1]);
expect(eventSpy.callCount).to.equal(1);
});
});
describe('Keyboard', () => {
it('should change to next slide on Enter/Space keys', async () => {
carousel.vertical = true;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulateKeyboard(nextButton!, spaceBar);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
simulateKeyboard(nextButton!, enterKey);
await slideChangeComplete(slides[1], slides[2]);
expect(carousel.current).to.equal(2);
});
it('should change to previous slide on Enter/Space keys', async () => {
carousel.vertical = true;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulateKeyboard(prevButton!, spaceBar);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
simulateKeyboard(prevButton!, enterKey);
await slideChangeComplete(slides[2], slides[1]);
expect(carousel.current).to.equal(1);
});
it('should change slides on ArrowLeft/ArrowRight/Home/End keys (LTR)', async () => {
const indicatorsContainer = carousel.shadowRoot?.querySelector(
'div[role="tablist"]'
) as HTMLDivElement;
expect(carousel.current).to.equal(0);
simulateKeyboard(indicatorsContainer, arrowRight);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
simulateKeyboard(indicatorsContainer, arrowLeft);
await slideChangeComplete(slides[1], slides[0]);
expect(carousel.current).to.equal(0);
simulateKeyboard(indicatorsContainer, endKey);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
simulateKeyboard(indicatorsContainer, homeKey);
await slideChangeComplete(slides[2], slides[0]);
expect(carousel.current).to.equal(0);
});
it('should change slides on ArrowLeft/ArrowRight/Home/End keys (RTL)', async () => {
carousel.dir = 'rtl';
await elementUpdated(carousel);
const indicatorsContainer = carousel.shadowRoot?.querySelector(
'div[role="tablist"]'
) as HTMLDivElement;
expect(carousel.current).to.equal(0);
simulateKeyboard(indicatorsContainer, arrowRight);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
simulateKeyboard(indicatorsContainer, arrowLeft);
await slideChangeComplete(slides[2], slides[0]);
expect(carousel.current).to.equal(0);
simulateKeyboard(indicatorsContainer, homeKey);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
simulateKeyboard(indicatorsContainer, endKey);
await slideChangeComplete(slides[2], slides[0]);
expect(carousel.current).to.equal(0);
});
});
describe('Automatic rotation', () => {
beforeEach(async () => {
clock = useFakeTimers({ toFake: ['setInterval'] });
});
afterEach(() => clock.restore());
it('should automatically change slides', async () => {
expect(carousel.current).to.equal(0);
carousel.interval = 200;
await elementUpdated(carousel);
await clock.tickAsync(200);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
});
it('should properly call `igcSlideChanged` event', async () => {
const eventSpy = spy(carousel, 'emitEvent');
carousel.disableLoop = true;
carousel.interval = 100;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
await clock.tickAsync(300);
expect(carousel.current).to.equal(2);
expect(eventSpy.callCount).to.equal(2);
});
it('should pause/play on pointerenter/pointerleave', async () => {
const eventSpy = spy(carousel, 'emitEvent');
const divContainer = carousel.shadowRoot?.querySelector(
'div[aria-live]'
) as HTMLDivElement;
expect(divContainer.ariaLive).to.equal('polite');
carousel.interval = 2000;
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
carousel.dispatchEvent(new PointerEvent('pointerenter'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(divContainer.ariaLive).to.equal('polite');
carousel.dispatchEvent(new PointerEvent('pointerleave'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
expect(eventSpy.callCount).to.equal(2);
expect(eventSpy.firstCall).calledWith('igcPaused');
expect(eventSpy.secondCall).calledWith('igcPlaying');
});
it('should pause/play on keyboard interaction', async () => {
const eventSpy = spy(carousel, 'emitEvent');
const divContainer = carousel.shadowRoot?.querySelector(
'div[aria-live]'
) as HTMLDivElement;
expect(divContainer.ariaLive).to.equal('polite');
carousel.interval = 2000;
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
// hover carousel
carousel.dispatchEvent(new PointerEvent('pointerenter'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(divContainer.ariaLive).to.equal('polite');
// focus a focusable element
carousel.dispatchEvent(new FocusEvent('focusin'));
carousel.dispatchEvent(new PointerEvent('pointerleave'));
await elementUpdated(carousel);
// element focus/interaction is present
// -> should not start rotation on pointerleave
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(divContainer.ariaLive).to.equal('polite');
// hover carousel
carousel.dispatchEvent(new PointerEvent('pointerenter'));
await elementUpdated(carousel);
// loose focus
carousel.dispatchEvent(new FocusEvent('focusout'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(divContainer.ariaLive).to.equal('polite');
// hover out of the carousel
carousel.dispatchEvent(new PointerEvent('pointerleave'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
expect(eventSpy.callCount).to.equal(2);
expect(eventSpy.firstCall).calledWith('igcPaused');
expect(eventSpy.secondCall).calledWith('igcPlaying');
});
it('should pause when focusing an interactive element - issue #1731', async () => {
carousel.interval = 200;
await elementUpdated(carousel);
await clock.tickAsync(199);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(carousel.current).to.equal(0);
// hover carousel
carousel.dispatchEvent(new PointerEvent('pointerenter'));
await elementUpdated(carousel);
await clock.tickAsync(1);
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(carousel.current).to.equal(0);
// focus a focusable element
carousel.dispatchEvent(new FocusEvent('focusin'));
await elementUpdated(carousel);
// hover out of the carousel
carousel.dispatchEvent(new PointerEvent('pointerleave'));
await elementUpdated(carousel);
await clock.tickAsync(200);
// an interactive element is focused
// -> should not start rotation on pointerleave
expect(carousel.isPlaying).to.be.false;
expect(carousel.isPaused).to.be.true;
expect(carousel.current).to.equal(0);
// loose focus
carousel.dispatchEvent(new FocusEvent('focusout'));
await elementUpdated(carousel);
await clock.tickAsync(200);
// the interactive element loses focus
// -> should start rotation
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(carousel.current).to.equal(2);
});
it('should not pause on interaction if `disablePauseOnInteraction` is true', async () => {
const eventSpy = spy(carousel, 'emitEvent');
const divContainer = carousel.shadowRoot?.querySelector(
'div[aria-live]'
) as HTMLDivElement;
expect(divContainer.ariaLive).to.equal('polite');
carousel.interval = 2000;
carousel.disablePauseOnInteraction = true;
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
carousel.dispatchEvent(new PointerEvent('pointerenter'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
carousel.dispatchEvent(new PointerEvent('pointerleave'));
await elementUpdated(carousel);
expect(carousel.isPlaying).to.be.true;
expect(carousel.isPaused).to.be.false;
expect(divContainer.ariaLive).to.equal('off');
expect(eventSpy.callCount).to.equal(0);
});
});
describe('Swipe', () => {
beforeEach(() => {
carouselSlidesContainer = carousel.shadowRoot?.querySelector(
'div[aria-live="polite"]'
) as Element;
});
it('should change to next slide on swipe-left', async () => {
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: -100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
});
it('should change to previous slide on swipe-left (RTL)', async () => {
carousel.dir = 'rtl';
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: -100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
});
it('should change to previous slide on swipe-right', async () => {
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: 100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
});
it('should change to next slide on swipe-right (RTL)', async () => {
carousel.dir = 'rtl';
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: 100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
});
it('should not change to next/previous slide on swipe left/right when `vertical` is true', async () => {
carousel.vertical = true;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
// swipe left
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: -100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(0);
// swipe right
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { x: 100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(0);
});
it('should change to next slide on swipe-up', async () => {
carousel.vertical = true;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { y: -100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(1);
});
it('should change to previous slide on swipe-down', async () => {
carousel.vertical = true;
await elementUpdated(carousel);
expect(carousel.current).to.equal(0);
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { y: 100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(2);
});
it('should not change to next/previous slide on swipe up/down when `vertical` is false', async () => {
expect(carousel.current).to.equal(0);
expect(carousel.vertical).to.be.false;
// swipe up
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { y: -100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[1]);
expect(carousel.current).to.equal(0);
// swipe down
simulatePointerDown(carouselSlidesContainer);
simulatePointerMove(carouselSlidesContainer, {}, { y: 100 }, 10);
simulateLostPointerCapture(carouselSlidesContainer);
await slideChangeComplete(slides[0], slides[2]);
expect(carousel.current).to.equal(0);
});
it('should not change to next/previous slide on mouse swipe', async () => {
expect(carousel.current).to.equal(0);
// swipe left
simulatePointerDown(carouselSlidesContainer, { pointerType: 'mouse' });