|
1 | | -describe("Intro.js tooltip position with scrollable container", () => { |
| 1 | +context("Intro.js tooltip position with scrollable container", () => { |
2 | 2 | beforeEach(() => { |
3 | 3 | cy.visit("./cypress/setup/index.html"); |
4 | 4 |
|
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 | 5 | // scrollable container and target element |
17 | 6 | cy.document().then((doc) => { |
18 | 7 | const container = doc.createElement("div"); |
19 | 8 | container.id = "scrollable-container"; |
20 | 9 | container.style.cssText = |
21 | | - "height: 600px; overflow-y: auto; border: 2px solid gray; margin-bottom: 20px;"; |
22 | | - |
23 | | - const inner = doc.createElement("div"); |
24 | | - inner.style.height = "1000px"; |
25 | | - inner.style.position = "relative"; |
26 | | - |
27 | | - const target = doc.createElement("div"); |
28 | | - target.id = "target-element"; |
29 | | - target.style.cssText = |
30 | | - "margin-top: 800px; background-color: yellow; padding: 10px;"; |
31 | | - target.setAttribute("data-intro", "Scrollable test element"); |
32 | | - target.textContent = "Step Element (scrollable test)"; |
33 | | - |
34 | | - inner.appendChild(target); |
35 | | - container.appendChild(inner); |
| 10 | + "height: 600px; overflow-y: scroll; border: 2px solid gray; margin-bottom: 20px;"; |
| 11 | + |
| 12 | + const getTargetElement = (id, marginTop, label) => { |
| 13 | + const target = doc.createElement("div"); |
| 14 | + target.id = id; |
| 15 | + target.style.cssText = `margin-top: ${marginTop}; background-color: yellow; padding: 10px;`; |
| 16 | + target.setAttribute("data-intro", label); |
| 17 | + target.textContent = `Step Element (${label})`; |
| 18 | + return target; |
| 19 | + }; |
| 20 | + |
| 21 | + container.appendChild( |
| 22 | + getTargetElement( |
| 23 | + "scrollable-target-element-1", |
| 24 | + "5px", |
| 25 | + "First Scrollable Step" |
| 26 | + ) |
| 27 | + ); |
| 28 | + container.appendChild( |
| 29 | + getTargetElement( |
| 30 | + "scrollable-target-element-2", |
| 31 | + "800px", |
| 32 | + "Second Scrollable Step" |
| 33 | + ) |
| 34 | + ); |
36 | 35 | doc.body.prepend(container); |
37 | 36 | }); |
38 | | - |
39 | | - // intro.js script and initialize tour |
40 | | - cy.window().then((win) => { |
41 | | - return new Promise((resolve) => { |
42 | | - const script = win.document.createElement("script"); |
43 | | - script.src = "https://unpkg.com/intro.js/minified/intro.min.js"; |
44 | | - script.onload = () => { |
45 | | - const tour = win.introJs.tour(); |
46 | | - tour.setOptions({ |
47 | | - steps: [ |
48 | | - { element: "#target-element", intro: "Scrollable test tooltip" }, |
49 | | - { |
50 | | - element: "#target-element", |
51 | | - intro: "Scrollable test tooltip 2", |
52 | | - }, |
53 | | - ], |
54 | | - }); |
55 | | - win.__testTour = tour; |
56 | | - resolve(); |
57 | | - }; |
58 | | - win.document.head.appendChild(script); |
59 | | - }); |
60 | | - }); |
61 | 37 | }); |
62 | 38 |
|
63 | 39 | it("scrolls and ensures tooltip is correctly positioned near target", () => { |
64 | 40 | cy.get("#scrollable-container").scrollTo("top"); |
65 | | - cy.get("#target-element") |
66 | | - .scrollIntoView({ block: "center" }) |
67 | | - .should("be.visible"); |
68 | 41 |
|
69 | 42 | cy.window().then((win) => { |
70 | | - win.__testTour.start(); |
71 | | - }); |
72 | | - |
73 | | - cy.get(".introjs-tooltip", { timeout: 500 }).should("be.visible"); |
74 | | - |
75 | | - cy.get("#target-element").then(($target) => { |
76 | | - const targetRect = $target[0].getBoundingClientRect(); |
77 | | - |
78 | | - cy.get(".introjs-tooltip").then(($tooltip) => { |
79 | | - const tooltipRect = $tooltip[0].getBoundingClientRect(); |
| 43 | + cy.compareSnapshot("scrollable-first-step"); |
80 | 44 |
|
81 | | - cy.log("Target Rect:", JSON.stringify(targetRect)); |
82 | | - cy.log("Tooltip Rect:", JSON.stringify(tooltipRect)); |
| 45 | + win.introJs.tour().start(); |
| 46 | + cy.wait(500); |
| 47 | + cy.compareSnapshot("scrollable-second-step"); |
83 | 48 |
|
84 | | - const horizontallySeparate = |
85 | | - tooltipRect.right < targetRect.left || |
86 | | - tooltipRect.left > targetRect.right; |
87 | | - const verticallySeparate = |
88 | | - tooltipRect.bottom < targetRect.top || |
89 | | - tooltipRect.top > targetRect.bottom; |
90 | | - expect(horizontallySeparate || verticallySeparate).to.be.true; |
| 49 | + cy.nextStep(); |
| 50 | + cy.wait(500); |
| 51 | + cy.compareSnapshot("scrollable-third-step"); |
91 | 52 |
|
92 | | - const verticalDistance = Math.min( |
93 | | - Math.abs(tooltipRect.top - targetRect.bottom), |
94 | | - Math.abs(targetRect.top - tooltipRect.bottom) |
95 | | - ); |
96 | | - expect(verticalDistance).to.be.lessThan(16); |
97 | | - }); |
| 53 | + cy.nextStep(); |
| 54 | + cy.wait(800); |
| 55 | + cy.compareSnapshot("scrollable-fourth-step"); |
98 | 56 | }); |
99 | 57 | }); |
100 | 58 | }); |
0 commit comments