diff --git a/packages/stacks-svelte/src/components/Popover/Popover.test.ts b/packages/stacks-svelte/src/components/Popover/Popover.test.ts index 346694e032..96f1a278ac 100644 --- a/packages/stacks-svelte/src/components/Popover/Popover.test.ts +++ b/packages/stacks-svelte/src/components/Popover/Popover.test.ts @@ -18,6 +18,13 @@ const defaultProps = { id: "test", }; +const waitForFloatingPosition = () => + new Promise((resolve) => { + requestAnimationFrame(() => { + requestAnimationFrame(() => resolve()); + }); + }); + const stubHoverSupport = (initialMatches: boolean) => { let matches = initialMatches; const listeners = new Set<(event: MediaQueryListEvent) => void>(); @@ -655,13 +662,6 @@ describe("Popover", () => { it("should shift the popover content horizontally when it would overflow the viewport", async () => { await setViewport({ width: 260, height: 260 }); - const waitForFloatingPosition = () => - new Promise((resolve) => { - requestAnimationFrame(() => { - requestAnimationFrame(() => resolve()); - }); - }); - render(Popover, { props: { ...defaultProps, @@ -1864,6 +1864,8 @@ describe("Popover", () => { it("keeps the tooltip open while selecting text after focusing its trigger", async () => { const clock = sinon.useFakeTimers({ + // Drive dismissal timers while letting the browser lay out real frames. + toFake: ["setTimeout", "clearTimeout"], shouldAdvanceTime: true, shouldClearNativeTimers: true, }); @@ -1884,12 +1886,23 @@ describe("Popover", () => { await clock.runAllAsync(); await tick(); const text = screen.getByText("Popover Content"); - const { left, right, top, height } = text.getBoundingClientRect(); - const y = Math.round(top + height / 2); + await waitFor(async () => { + const before = text.getBoundingClientRect().toJSON(); + await waitForFloatingPosition(); + expect(text.getBoundingClientRect().toJSON()).to.deep.equal( + before + ); + }); + const lines = text.getClientRects(); + const first = lines[0]; + const last = lines[lines.length - 1]; try { await sendMouse({ type: "move", - position: [Math.ceil(left), y], + position: [ + Math.ceil(first.left), + Math.round(first.top + first.height / 2), + ], }); await sendMouse({ type: "down" }); await clock.runAllAsync(); @@ -1898,7 +1911,10 @@ describe("Popover", () => { await sendMouse({ type: "move", - position: [Math.floor(right), y], + position: [ + Math.floor(last.right), + Math.round(last.top + last.height / 2), + ], }); await sendMouse({ type: "up" }); expect(window.getSelection()?.toString()).to.equal(