Skip to content

Commit 1797796

Browse files
committed
add cypress test
1 parent 331edab commit 1797796

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

src/packages/tour/position.cy.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ describe("Intro.js tooltip position with scrollable container", () => {
22
beforeEach(() => {
33
cy.visit("./cypress/setup/index.html");
44

5-
// Add scrollable container and target element dynamically
5+
// intro.js CSS
6+
cy.document().then((doc) => {
7+
if (!doc.getElementById("introjs-css")) {
8+
const link = doc.createElement("link");
9+
link.id = "introjs-css";
10+
link.rel = "stylesheet";
11+
link.href = "https://unpkg.com/intro.js/minified/introjs.min.css";
12+
doc.head.appendChild(link);
13+
}
14+
});
15+
16+
// scrollable container and target element
617
cy.document().then((doc) => {
718
const container = doc.createElement("div");
819
container.id = "scrollable-container";
@@ -23,20 +34,9 @@ describe("Intro.js tooltip position with scrollable container", () => {
2334
inner.appendChild(target);
2435
container.appendChild(inner);
2536
doc.body.prepend(container);
26-
27-
// Add start tour button
28-
let btn = doc.getElementById("start-tour");
29-
if (!btn) {
30-
btn = doc.createElement("button");
31-
btn.id = "start-tour";
32-
btn.textContent = "Start Tour";
33-
btn.style.display = "block";
34-
btn.className = "btn btn-success mb-4";
35-
doc.body.prepend(btn);
36-
}
3737
});
3838

39-
// Inject Intro.js script and initialize tour
39+
// intro.js script and initialize tour
4040
cy.window().then((win) => {
4141
return new Promise((resolve) => {
4242
const script = win.document.createElement("script");
@@ -51,9 +51,6 @@ describe("Intro.js tooltip position with scrollable container", () => {
5151
intro: "Scrollable test tooltip 2",
5252
},
5353
],
54-
scrollToElement: true,
55-
scrollTo: "element",
56-
tooltipPosition: "bottom",
5754
});
5855
win.__testTour = tour;
5956
resolve();
@@ -64,21 +61,17 @@ describe("Intro.js tooltip position with scrollable container", () => {
6461
});
6562

6663
it("scrolls and ensures tooltip is correctly positioned near target", () => {
67-
// Scroll target into view within the container
6864
cy.get("#scrollable-container").scrollTo("top");
6965
cy.get("#target-element")
7066
.scrollIntoView({ block: "center" })
7167
.should("be.visible");
7268

73-
// Start the tour
7469
cy.window().then((win) => {
7570
win.__testTour.start();
7671
});
7772

78-
// Wait for tooltip
79-
cy.get(".introjs-tooltip", { timeout: 5000 }).should("be.visible");
73+
cy.get(".introjs-tooltip", { timeout: 500 }).should("be.visible");
8074

81-
// Verify tooltip placement
8275
cy.get("#target-element").then(($target) => {
8376
const targetRect = $target[0].getBoundingClientRect();
8477

@@ -88,7 +81,6 @@ describe("Intro.js tooltip position with scrollable container", () => {
8881
cy.log("Target Rect:", JSON.stringify(targetRect));
8982
cy.log("Tooltip Rect:", JSON.stringify(tooltipRect));
9083

91-
// Ensure not overlapping
9284
const horizontallySeparate =
9385
tooltipRect.right < targetRect.left ||
9486
tooltipRect.left > targetRect.right;
@@ -97,12 +89,11 @@ describe("Intro.js tooltip position with scrollable container", () => {
9789
tooltipRect.top > targetRect.bottom;
9890
expect(horizontallySeparate || verticallySeparate).to.be.true;
9991

100-
// Ensure tooltip is close to target (±10px tolerance)
10192
const verticalDistance = Math.min(
10293
Math.abs(tooltipRect.top - targetRect.bottom),
10394
Math.abs(targetRect.top - tooltipRect.bottom)
10495
);
105-
expect(verticalDistance).to.be.lessThan(15);
96+
expect(verticalDistance).to.be.lessThan(16);
10697
});
10798
});
10899
});

0 commit comments

Comments
 (0)