Skip to content

Commit fc3cdfd

Browse files
committed
Several tests for elements that are added after configuration
1 parent 7112475 commit fc3cdfd

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

tests/core/fetchIntroSteps.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ describe("fetchIntroSteps", () => {
8686
expect(steps[2].step).toBe(3);
8787
});
8888

89+
test("should throw an error on calling step.element if it is not exists yet and return element after adding", () => {
90+
const targetElement = document.createElement("div");
91+
const elId = "later_added";
92+
const steps = fetchIntroSteps.call(
93+
{
94+
_options: {
95+
tooltipPosition: "bottom",
96+
steps: [
97+
{
98+
element: "#" + elId,
99+
},
100+
],
101+
},
102+
},
103+
targetElement
104+
);
105+
106+
expect(steps.length).toBe(1);
107+
108+
try {
109+
const element = steps[0].element; // jshint ignore:line
110+
} catch (e) {
111+
if (!e.message.includes("There is no element with given selector:"))
112+
throw e;
113+
}
114+
115+
const laterAdded = document.createElement("div");
116+
laterAdded.setAttribute("id", elId);
117+
document.body.appendChild(laterAdded);
118+
119+
expect(steps[0].element).toBe(laterAdded);
120+
});
121+
89122
test("should find the data-* elements from the DOM", () => {
90123
const targetElement = document.createElement("div");
91124

tests/index.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,37 @@ describe("intro", () => {
146146
);
147147
});
148148

149+
test("should find added later element", () => {
150+
const latterAddedElId = "later_added";
151+
var laterAddedEl;
152+
var stepCounter = 0;
153+
const intro = introJs()
154+
.setOptions({
155+
steps: [
156+
{
157+
intro: "step one",
158+
},
159+
{
160+
intro: "later aded",
161+
element: "#" + latterAddedElId,
162+
},
163+
],
164+
})
165+
.onchange(function (el) {
166+
if (el && stepCounter === 1) expect(el).toBe(laterAddedEl);
167+
stepCounter++;
168+
})
169+
.start();
170+
171+
laterAddedEl = appendDummyElement();
172+
laterAddedEl.setAttribute("id", latterAddedElId);
173+
174+
intro.nextStep();
175+
const step = intro.currentStep();
176+
expect(step).toBe(1);
177+
expect(intro._introItems[step].element).toBe(laterAddedEl);
178+
});
179+
149180
test("should highlight the target element", () => {
150181
const p = appendDummyElement();
151182

@@ -164,6 +195,34 @@ describe("intro", () => {
164195
expect(p.className).toContain("introjs-relativePosition");
165196
});
166197

198+
test("should highlight added later target element", (done) => {
199+
const latterAddedElId = "later_added";
200+
const intro = introJs()
201+
.setOptions({
202+
steps: [
203+
{
204+
intro: "step one",
205+
},
206+
{
207+
intro: "later added",
208+
element: "#" + latterAddedElId,
209+
},
210+
],
211+
})
212+
.start();
213+
214+
const laterAdded = appendDummyElement();
215+
laterAdded.setAttribute("id", latterAddedElId);
216+
217+
intro.nextStep();
218+
setTimeout(() => {
219+
// Waiting for animation for avoiding error
220+
expect(laterAdded.className).toContain("introjs-showElement");
221+
expect(laterAdded.className).toContain("introjs-relativePosition");
222+
done();
223+
}, 500);
224+
});
225+
167226
test("should not highlight the target element if queryString is incorrect", () => {
168227
const p = appendDummyElement();
169228

0 commit comments

Comments
 (0)