-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
578 lines (564 loc) · 21.1 KB
/
index.test.js
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
import React from "react";
import { render, act, fireEvent } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import Slider from "./src/components/slider";
describe("[Slidish] methods", () => {
let ref = { current: null },
getByTestId,
getByRole;
beforeEach(() => {
({ getByRole, getByTestId } = render(
<Slider ref={ref} testing>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
));
});
afterEach(() => {
ref.current = null;
});
it("should go to next or previous slide on calling next or previous methods", () => {
const slidesContainer = getByTestId("slidesContainer");
expect(slidesContainer.style.transform).toBe("translate3d(-0%,0,0)");
act(() => ref.current.next());
expect(slidesContainer.style.transform).toBe("translate3d(-100%,0,0)");
act(() => ref.current.previous());
expect(slidesContainer.style.transform).toBe("translate3d(-0%,0,0)");
});
it("should go to the specified slide on calling goto method", () => {
const slidesContainer = getByTestId("slidesContainer");
expect(slidesContainer.style.transform).toBe("translate3d(-0%,0,0)");
act(() => ref.current.goto(2));
expect(slidesContainer.style.transform).toBe("translate3d(-200%,0,0)");
});
it("should go fullscreen on calling goFullscreen and exit fullscreen on calling exitFullscreen", () => {
const root = getByTestId("root");
expect(root.classList.contains("Slidish__root")).toBe(true);
expect(root.classList.contains("Slidish__root--fullscreen")).toBe(false);
act(() => ref.current.goFullscreen());
expect(root.classList.contains("Slidish__root--fullscreen")).toBe(true);
expect(root.classList.contains("Slidish__root")).toBe(false);
act(() => ref.current.exitFullscreen());
expect(root.classList.contains("Slidish__root")).toBe(true);
expect(root.classList.contains("Slidish__root--fullscreen")).toBe(false);
});
it("should change isPlaying state to true when calling play and change it to false when calling pause", () => {
const playButton = getByRole("button", { name: "start slideshow" });
jest.useFakeTimers();
act(() => ref.current.play());
expect(playButton.getAttribute("aria-label")).toBe("pause slideshow");
act(() => ref.current.pause());
expect(playButton.getAttribute("aria-label")).toBe("start slideshow");
jest.clearAllTimers();
jest.useRealTimers();
});
it("should return the current state of slider", () => {
const defaultState = ref.current.getState();
expect(defaultState).toMatchObject({
currentIndex: 0,
isPlaying: false,
isFullscreen: false,
slidesCount: 3,
});
act(() => ref.current.next());
const updatedState = ref.current.getState();
expect(updatedState).toMatchObject({ currentIndex: 1 });
});
});
describe("[Slidish] functionality", () => {
let slidesContainer,
rerender,
getByTestId,
getAllByTestId,
getByRole,
getAllByRole,
ref = { current: null };
beforeEach(() => {
({ rerender, getByTestId, getAllByTestId, getByRole, getAllByRole } = render(
<Slider testing ref={ref}>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
));
slidesContainer = getByTestId("slidesContainer");
});
afterEach(() => {
ref.current = null;
});
it("should render 3 slides", () => {
const slides = document.querySelectorAll(".Slidish__slide");
expect(slides.length).toBe(3);
});
it("should go to next or previous slide on clicking the next or previous button", () => {
const nextButton = getByRole("button", { name: "next slide" });
const previousButton = getByRole("button", { name: "previous slide" });
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
userEvent.click(nextButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
userEvent.click(previousButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
userEvent.click(previousButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
userEvent.click(nextButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
});
it("should go fullscreen on clicking the fullscreen button and vise versa", () => {
const fullscreenButton = getByRole("button", { name: "go fullscreen" });
let isFullscreen = ref.current.getState().isFullscreen;
expect(isFullscreen).toBe(false);
userEvent.click(fullscreenButton);
isFullscreen = ref.current.getState().isFullscreen;
expect(isFullscreen).toBe(true);
userEvent.click(fullscreenButton);
isFullscreen = ref.current.getState().isFullscreen;
expect(isFullscreen).toBe(false);
});
it("should start playing slideshow on clicking the play button and vise versa", async () => {
const playButton = getByRole("button", { name: "start slideshow" });
jest.useFakeTimers("legacy");
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
userEvent.click(playButton);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3000);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
userEvent.click(playButton);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
act(() => jest.advanceTimersByTime(60000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
expect(setTimeout).toHaveBeenCalledTimes(4);
jest.clearAllTimers();
jest.useRealTimers();
});
it("should go to the associated slide when clicking an indicator or pressing enter or space while indicator has focus", () => {
const indicators = getAllByRole("button", { name: /slide indicator/ });
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
userEvent.click(indicators[2]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
userEvent.click(indicators[1]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
userEvent.click(indicators[0]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
indicators[2].focus();
userEvent.type(indicators[2], "{enter}", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
indicators[1].focus();
userEvent.type(indicators[1], " ", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
indicators[0].focus();
userEvent.type(indicators[0], "b", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
});
it("should go to the associated slide when clicking a thumbnail", () => {
const thumbs = getAllByRole("button", { name: /thumbnail/ });
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
userEvent.click(thumbs[2]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
userEvent.click(thumbs[1]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
userEvent.click(thumbs[0]);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
thumbs[2].focus();
userEvent.type(thumbs[2], "{enter}", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
thumbs[1].focus();
userEvent.type(thumbs[1], " ", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
thumbs[2].focus();
userEvent.type(thumbs[2], "any", { skipClick: true });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
});
it("should go to next or previous slide on pressing right or left key", () => {
const root = getByTestId("root");
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
root.focus();
userEvent.type(root, "{arrowright}");
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
userEvent.type(root, "{arrowleft}");
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
});
it("should go to next or previous slide on slide", () => {
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
fireEvent.mouseDown(slidesContainer, { clientX: 0 });
fireEvent.mouseMove(slidesContainer, { clientX: -50 });
expect(slidesContainer.style.transform).toBe("translate3d(-50%,0,0)");
fireEvent.mouseUp(slidesContainer, { clientX: -70 });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
fireEvent.mouseDown(slidesContainer, { clientX: 0 });
fireEvent.mouseMove(slidesContainer, { clientX: -50 });
fireEvent.mouseUp(slidesContainer, { clientX: 20 });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
fireEvent.touchStart(slidesContainer, { targetTouches: [{ clientX: 0 }] });
fireEvent.touchMove(slidesContainer, { targetTouches: [{ clientX: -40 }] });
expect(slidesContainer.style.transform).toBe("translate3d(-40%,0,0)");
fireEvent.touchEnd(slidesContainer, { changedTouches: [{ clientX: -60 }] });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
});
it("shouldn't slide when mouse is not down and moving on or leaving the slidesContainer", () => {
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
fireEvent.mouseMove(slidesContainer, { clientX: -70 });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
fireEvent.mouseLeave(slidesContainer);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
});
it("should pause on hover while pauseOnHover prop is true and slideshow is playing", () => {
rerender(
<Slider testing ref={ref} slideshow={{ pauseOnHover: true }}>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
);
jest.useFakeTimers();
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
act(() => ref.current.play());
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
fireEvent.mouseOver(slidesContainer);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
fireEvent.mouseLeave(slidesContainer);
act(() => jest.advanceTimersByTime(3000));
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
jest.clearAllTimers();
jest.useRealTimers();
});
it("discard slide's click event when slideStopPropagation prop is true", () => {
rerender(
<Slider testing ref={ref} advanced={{ slideStopPropagation: false }}>
<span>
<button data-testid="buttonInsideSlide" onClick={() => ref.current.goto(2)}>
click me
</button>
</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
);
let testButton = getAllByTestId("buttonInsideSlide")[0];
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
userEvent.click(testButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
rerender(
<Slider testing ref={ref} advanced={{ slideStopPropagation: true }}>
<span>
<button data-testid="buttonInsideSlide" onClick={() => ref.current.goto(1)}>
click me
</button>
</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
testButton = getAllByTestId("buttonInsideSlide")[0];
userEvent.click(testButton);
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(2);
});
});
describe("[Slidish] event callbacks", () => {
let ref = { current: null },
getByTestId,
slidesContainer,
getByRole,
getAllByRole;
const onNext = jest.fn();
const onPrevious = jest.fn();
const onSlide = jest.fn();
const onPlay = jest.fn();
const onPause = jest.fn();
const onFullscreen = jest.fn();
const onFullscreenExit = jest.fn();
const onIndicatorClick = jest.fn();
const onThumbClick = jest.fn();
beforeEach(() => {
({ getByRole, getByTestId, getAllByRole } = render(
<Slider
ref={ref}
testing
handlers={{
onNext,
onPrevious,
onSlide,
onPlay,
onPause,
onFullscreen,
onFullscreenExit,
onIndicatorClick,
onThumbClick,
}}
>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
));
slidesContainer = getByTestId("slidesContainer");
});
afterEach(() => {
ref.current = null;
});
it("Should call the event callbacks when clicking the corresponding buttons or calling the corresponding methods", () => {
const nextButton = getByRole("button", { name: "next slide" });
const previousButton = getByRole("button", { name: "previous slide" });
const playButton = getByRole("button", { name: "start slideshow" });
const fullscreenButton = getByRole("button", { name: "go fullscreen" });
const indicator = getAllByRole("button", { name: /slide indicator/ })[2];
const thumbnail = getAllByRole("button", { name: /thumbnail/ })[1];
userEvent.click(nextButton);
expect(onNext).toBeCalledTimes(1);
userEvent.click(previousButton);
expect(onPrevious).toBeCalledTimes(1);
userEvent.click(playButton);
expect(onPlay).toBeCalledTimes(1);
userEvent.click(playButton);
expect(onPlay).toBeCalledTimes(1);
expect(onPause).toBeCalledTimes(1);
userEvent.click(fullscreenButton);
expect(onFullscreen).toBeCalledTimes(1);
userEvent.click(fullscreenButton);
expect(onFullscreen).toBeCalledTimes(1);
expect(onFullscreenExit).toBeCalledTimes(1);
userEvent.click(indicator);
expect(onIndicatorClick).toBeCalledTimes(1);
userEvent.click(thumbnail);
expect(onThumbClick).toBeCalledTimes(1);
fireEvent.mouseDown(slidesContainer, { clientX: 0 });
fireEvent.mouseMove(slidesContainer, { clientX: -50 });
fireEvent.mouseUp(slidesContainer, { clientX: -70 });
expect(onSlide).toBeCalledTimes(1);
});
});
describe("[Slidish] thumbnails functionality", () => {
let thumbsContainer,
rerender,
getByTestId,
getAllByRole,
ref = { current: null };
beforeEach(() => {
({ rerender, getByTestId, getAllByRole } = render(
<Slider testing ref={ref}>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
));
thumbsContainer = getByTestId("thumbsContainer");
});
afterEach(() => {
ref.current = null;
});
it("should move to the desired slide on thumbnails' slide", () => {
let { currentIndex } = ref.current.getState();
expect(currentIndex).toBe(0);
fireEvent.mouseDown(thumbsContainer, { clientX: 0 });
fireEvent.mouseMove(thumbsContainer, { clientX: -50 });
fireEvent.mouseUp(thumbsContainer, { clientX: -70 });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(1);
fireEvent.mouseDown(thumbsContainer, { clientX: 0 });
fireEvent.mouseMove(thumbsContainer, { clientX: 50 });
fireEvent.mouseUp(thumbsContainer, { clientX: 70 });
({ currentIndex } = ref.current.getState());
expect(currentIndex).toBe(0);
});
it("should render placeHolder when thumbnail is not in view in lazy mode", () => {
class IntersectionObserver {
constructor(cb) {
cb([{ isIntersecting: false }]);
}
observe() {}
disconnect() {}
}
global.IntersectionObserver = IntersectionObserver;
rerender(
<Slider testing ref={ref} lazyLoading={{ enabled: true, thumbPlaceHolder: "loading" }}>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
);
const thumbnail = getAllByRole("button", { name: /thumbnail/ })[0];
expect(thumbnail).toHaveTextContent("loading");
});
it("should render original content when thumbnail is in view in lazy mode", () => {
class IntersectionObserver {
constructor(cb) {
cb([{ isIntersecting: true }]);
}
observe() {}
disconnect() {}
}
global.IntersectionObserver = IntersectionObserver;
rerender(
<Slider testing ref={ref} lazyLoading={{ enabled: true, thumbPlaceHolder: "loading" }}>
<span>1</span>
<iframe title="iframe">2</iframe>
<div>3</div>
</Slider>
);
const thumbnail = getAllByRole("button", { name: /thumbnail/ })[0];
expect(thumbnail).toContainHTML("<span>1</span>");
});
});
describe("[Slidish] Layout", () => {
let rerender,
getByTestId,
getAllByRole,
getByRole,
getByText,
queryByText,
queryByRole,
queryAllByRole,
queryByTestId;
beforeEach(() => {
({
rerender,
getByTestId,
getAllByRole,
getByRole,
getByText,
queryByText,
queryByRole,
queryAllByRole,
queryByTestId,
} = render(
<Slider testing options={{ startFullscreen: true }}>
<span>1</span>
<span>2</span>
<div>3</div>
</Slider>
));
});
it("should render all components by default", () => {
const fsButtons = getAllByRole("button", { name: "exit fullscreen" });
expect(fsButtons).toHaveLength(2);
const nextButton = getByRole("button", { name: "next slide" });
const prevButton = getByRole("button", { name: "previous slide" });
const playButton = getByRole("button", { name: "start slideshow" });
const indexIndicator = getByText("1 / 3");
const indicators = getAllByRole("button", { name: /slide indicator/, exact: false });
expect(indicators).toHaveLength(3);
const progressBar = getByTestId("progressBar");
const slidesContainer = getByTestId("slidesContainer");
const thumbsRoot = getByTestId("thumbsRoot");
});
it("should not render any components when all layout prop keys are set to false", () => {
rerender(
<Slider
testing
options={{ startFullscreen: true }}
layout={{
fullscreenButton: false,
fullscreenCloseButton: false,
indexIndicator: false,
indicators: false,
nextButton: false,
previousButton: false,
playButton: false,
slides: false,
thumbs: false,
progressBar: false,
}}
>
<span>1</span>
<span>2</span>
<div>3</div>
</Slider>
);
const fsButtons = queryAllByRole("button", { name: "exit fullscreen" });
expect(fsButtons).toHaveLength(0);
const nextButton = queryByRole("button", { name: "next slide" });
const prevButton = queryByRole("button", { name: "previous slide" });
const playButton = queryByRole("button", { name: "start slideshow" });
const indexIndicator = queryByText("1 / 3");
const indicators = queryAllByRole("button", { name: /slide indicator/, exact: false });
expect(indicators).toHaveLength(0);
const progressBar = queryByTestId("progressBar");
const slidesContainer = queryByTestId("slidesContainer");
const thumbsRoot = queryByTestId("thumbsRoot");
const isEverythingNull = [
nextButton,
prevButton,
playButton,
indexIndicator,
progressBar,
slidesContainer,
thumbsRoot,
].every((i) => i === null);
expect(isEverythingNull).toBe(true);
});
});
describe("[Slidish] Hardware acceleration", () => {
it("shouldn't contain ant translate3d transformations when hardware acceleration is not set", () => {
const { getByTestId } = render(
<Slider advanced={{ hardwareAcceleration: false }}>
<span>1</span>
<span>2</span>
<span>3</span>
</Slider>
);
const slidesContainer = getByTestId("slidesContainer");
const progressBar = getByTestId("progressBar").firstChild;
const thumbsContainer = getByTestId("thumbsContainer");
expect(slidesContainer.style.transform).toMatch(/translate/);
expect(thumbsContainer.style.transform).toMatch(/translate/);
expect(progressBar.style.transform).toMatch(/translate/);
expect(slidesContainer.style.transform).not.toMatch(/translate3d/);
expect(thumbsContainer.style.transform).not.toMatch(/translate3d/);
expect(progressBar.style.transform).not.toMatch(/translate3d/);
});
});