|
| 1 | +describe("Intro.js tooltip position with scrollable container", () => { |
| 2 | + beforeEach(() => { |
| 3 | + cy.visit("./cypress/setup/index.html"); |
| 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 | + // scrollable container and target element |
| 17 | + cy.document().then((doc) => { |
| 18 | + const container = doc.createElement("div"); |
| 19 | + container.id = "scrollable-container"; |
| 20 | + 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); |
| 36 | + doc.body.prepend(container); |
| 37 | + }); |
| 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 | + }); |
| 62 | + |
| 63 | + it("scrolls and ensures tooltip is correctly positioned near target", () => { |
| 64 | + cy.get("#scrollable-container").scrollTo("top"); |
| 65 | + cy.get("#target-element") |
| 66 | + .scrollIntoView({ block: "center" }) |
| 67 | + .should("be.visible"); |
| 68 | + |
| 69 | + 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(); |
| 80 | + |
| 81 | + cy.log("Target Rect:", JSON.stringify(targetRect)); |
| 82 | + cy.log("Tooltip Rect:", JSON.stringify(tooltipRect)); |
| 83 | + |
| 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; |
| 91 | + |
| 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 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments