Skip to content

Commit d1bb2ca

Browse files
committed
chore(test): adding event tests
1 parent 37b91ef commit d1bb2ca

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

.jshintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"exports": true,
1010
"define": true,
1111
"SVGElement": true,
12+
"Element": true,
1213
"module": true,
1314
"console": true,
1415
"global": true,

tests/helper.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
export function find(selector) {
2-
return document.querySelector(selector);
2+
if (typeof selector === 'string') {
3+
return document.querySelector(selector);
4+
}
5+
6+
if (selector instanceof Element) {
7+
return selector;
8+
}
9+
10+
throw Error('invalid selector');
311
}
412

513
export function content(selector) {
@@ -21,3 +29,23 @@ export function className(selector) {
2129

2230
return null;
2331
}
32+
33+
export function skipButton() {
34+
return find('.introjs-skipbutton');
35+
}
36+
37+
export function nextButton() {
38+
return find('.introjs-nextbutton');
39+
}
40+
41+
export function prevButton() {
42+
return find('.introjs-prevbutton');
43+
}
44+
45+
export function doneButton() {
46+
return find('.introjs-donebutton');
47+
}
48+
49+
export function tooltipText() {
50+
return find('.introjs-tooltiptext');
51+
}

tests/index.test.js

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import introJs from "../src";
2-
import { find, content, className } from "./helper";
2+
import {content, className, skipButton, nextButton, prevButton, doneButton, tooltipText} from "./helper";
33

44
describe("intro", () => {
55
beforeEach(() => {
@@ -17,11 +17,11 @@ describe("intro", () => {
1717
})
1818
.start();
1919

20-
expect(content(".introjs-tooltiptext")).toBe("hello world");
20+
expect(content(tooltipText())).toBe("hello world");
2121

22-
expect(content(".introjs-donebutton")).toBe("Done");
22+
expect(content(doneButton())).toBe("Done");
2323

24-
expect(find(".introjs-prevbutton")).toBeNull();
24+
expect(prevButton()).toBeNull();
2525

2626
expect(className(".introjs-showElement")).toContain(
2727
"introjsFloatingElement"
@@ -31,6 +31,75 @@ describe("intro", () => {
3131
);
3232
});
3333

34+
test("should call onexit and oncomplete when there is one step", () => {
35+
const onexitMock = jest.fn();
36+
const oncompleteMMock = jest.fn();
37+
38+
introJs()
39+
.setOptions({
40+
steps: [
41+
{
42+
intro: "hello world",
43+
},
44+
],
45+
})
46+
.onexit(onexitMock)
47+
.oncomplete(oncompleteMMock)
48+
.start();
49+
50+
nextButton().click();
51+
52+
expect(onexitMock).toBeCalledTimes(1);
53+
expect(oncompleteMMock).toBeCalledTimes(1);
54+
});
55+
56+
test("should call onexit when skip is clicked", () => {
57+
const onexitMock = jest.fn();
58+
const oncompleteMMock = jest.fn();
59+
60+
introJs()
61+
.setOptions({
62+
steps: [
63+
{
64+
intro: "hello world",
65+
},
66+
],
67+
})
68+
.onexit(onexitMock)
69+
.oncomplete(oncompleteMMock)
70+
.start();
71+
72+
skipButton().click();
73+
74+
expect(onexitMock).toBeCalledTimes(1);
75+
expect(oncompleteMMock).toBeCalledTimes(1);
76+
});
77+
78+
test("should call not oncomplete when skip is clicked and there are two steps", () => {
79+
const onexitMock = jest.fn();
80+
const oncompleteMMock = jest.fn();
81+
82+
introJs()
83+
.setOptions({
84+
steps: [
85+
{
86+
intro: "first",
87+
},
88+
{
89+
intro: "second",
90+
},
91+
],
92+
})
93+
.onexit(onexitMock)
94+
.oncomplete(oncompleteMMock)
95+
.start();
96+
97+
skipButton().click();
98+
99+
expect(onexitMock).toBeCalledTimes(1);
100+
expect(oncompleteMMock).toBeCalledTimes(0);
101+
});
102+
34103
test("should start floating intro with two steps", () => {
35104
introJs()
36105
.setOptions({
@@ -45,15 +114,15 @@ describe("intro", () => {
45114
})
46115
.start();
47116

48-
expect(content(".introjs-tooltiptext")).toBe("step one");
117+
expect(content(tooltipText())).toBe("step one");
49118

50-
expect(find(".introjs-donebutton")).toBeNull();
119+
expect(doneButton()).toBeNull();
51120

52-
expect(find(".introjs-prevbutton")).not.toBeNull();
53-
expect(className(".introjs-prevbutton")).toContain("introjs-disabled");
121+
expect(prevButton()).not.toBeNull();
122+
expect(className(prevButton())).toContain("introjs-disabled");
54123

55-
expect(find(".introjs-nextbutton")).not.toBeNull();
56-
expect(className(".introjs-nextbutton")).not.toContain("introjs-disabled");
124+
expect(nextButton()).not.toBeNull();
125+
expect(className(nextButton())).not.toContain("introjs-disabled");
57126

58127
expect(className(".introjs-showElement")).toContain(
59128
"introjsFloatingElement"

0 commit comments

Comments
 (0)