-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathSimpleSlider.test.js
135 lines (132 loc) · 4.51 KB
/
SimpleSlider.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
import React from "react";
import SimpleSlider from "../SimpleSlider";
import { render, fireEvent, waitFor, screen } from "@testing-library/react";
import { html as beautify_html } from "js-beautify";
import {
getActiveSlide,
clickNext,
clickPrevious,
hasClass,
getActiveSlides,
getActiveSlidesCount,
getActiveSlidesText,
getButtons,
getButtonsListItem,
getCurrentSlide
} from "../../test-utils";
describe("SimpleSlider example", () => {
it("should have 13 slides (1(preclone) + 6(actual) + 1(postclone))", function() {
const { container } = render(<SimpleSlider a={5} />);
expect(container.getElementsByClassName("slick-slide").length).toBe(8);
});
it("should have 3 clone slides", function() {
const { container } = render(<SimpleSlider />);
expect(container.getElementsByClassName("slick-cloned").length).toBe(2);
});
it("should have 1 current slide", function() {
const { container } = render(<SimpleSlider />);
expect(
container.querySelectorAll(".slick-slide.slick-current").length
).toBe(1);
expect(parseInt(getCurrentSlide(container).textContent) - 1).toBe(0);
});
it("should have 1 active slide", function() {
const { container } = render(<SimpleSlider />);
expect(container.querySelectorAll(".slick-slide.slick-active").length).toBe(
1
);
expect(
Array.from(getActiveSlide(container).children).map(
e => parseInt(e.textContent) - 1
)[0]
).toBe(0);
});
it("should have 6 dots", function() {
const { container } = render(<SimpleSlider />);
expect(
container.getElementsByClassName("slick-dots")[0].children.length
).toBe(6);
});
it("should have 1 active dot", function() {
const { container } = render(<SimpleSlider />);
expect(container.querySelectorAll(".slick-dots .slick-active").length).toBe(
1
);
});
it("should have a prev arrow", function() {
const { container } = render(<SimpleSlider />);
expect(container.getElementsByClassName("slick-prev").length).toBe(1);
});
it("should have a next arrow", function() {
const { container } = render(<SimpleSlider />);
expect(container.getElementsByClassName("slick-next").length).toBe(1);
});
it("should got to next slide when next button is clicked", function() {
const { container } = render(<SimpleSlider />);
clickNext(container);
expect(
container.querySelectorAll(".slick-slide.slick-active")[0].textContent
).toBe("2");
expect(container.querySelectorAll(".slick-dots .slick-active").length).toBe(
1
);
expect(
container.querySelectorAll(".slick-dots")[0].children[1]
).toHaveClass("slick-active");
});
it("should goto previous slide when prev button is clicked", function() {
const { container } = render(<SimpleSlider />);
clickPrevious(container);
expect(
container.querySelectorAll(".slick-slide.slick-active")[0].textContent
).toBe("6");
expect(container.querySelectorAll(".slick-dots .slick-active").length).toBe(
1
);
expect(
container.querySelectorAll(".slick-dots")[0].children[5]
).toHaveClass("slick-active");
});
it("should goto 4th slide when 4th dot is clicked", function() {
const { container } = render(<SimpleSlider />);
fireEvent(
container.querySelectorAll(".slick-dots button")[3],
new MouseEvent("click", {
bubbles: true,
cancelable: true
})
);
expect(getActiveSlidesText(container)[0]).toEqual("4");
expect(getActiveSlidesCount(container)).toEqual(1);
expect(hasClass(getButtonsListItem(container)[3], "slick-active")).toEqual(
true
);
});
});
describe("Simple Slider Snapshots", function() {
it("slider initial state", function() {
const { container } = render(<SimpleSlider />);
// expect(beautify_html(toString(container))).toMatchSnapshot();
});
it("click on next button", function() {
const { container } = render(<SimpleSlider />);
clickNext(container);
//expect(beautify_html(toString(container))).toMatchSnapshot();
});
it("click on prev button", function() {
const { container } = render(<SimpleSlider />);
clickPrevious(container);
// expect(beautify_html(toString(container))).toMatchSnapshot();
});
it("click on 3rd dot", function() {
const { container } = render(<SimpleSlider />);
fireEvent(
getButtons(container)[2],
new MouseEvent("click", {
bubbles: true,
cancelable: true
})
);
// expect(beautify_html(toString(container))).toMatchSnapshot();
});
});