|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createDriver, navButton, nextFrame, SAMPLE_STEPS, useDriverHarness } from "./utils"; |
| 3 | + |
| 4 | +useDriverHarness(); |
| 5 | + |
| 6 | +afterEach(() => { |
| 7 | + vi.restoreAllMocks(); |
| 8 | +}); |
| 9 | + |
| 10 | +const DRIVER_EVENT_TYPES = ["click", "pointerdown", "mousedown", "pointerup", "mouseup"]; |
| 11 | + |
| 12 | +type ListenerRecord = { |
| 13 | + type: string; |
| 14 | + listener: EventListenerOrEventListenerObject; |
| 15 | + capture: boolean; |
| 16 | +}; |
| 17 | + |
| 18 | +function normalizeCapture(options?: boolean | AddEventListenerOptions | EventListenerOptions): boolean { |
| 19 | + if (typeof options === "boolean") { |
| 20 | + return options; |
| 21 | + } |
| 22 | + |
| 23 | + return !!options?.capture; |
| 24 | +} |
| 25 | + |
| 26 | +function trackDocumentListeners() { |
| 27 | + const added: ListenerRecord[] = []; |
| 28 | + const removed: ListenerRecord[] = []; |
| 29 | + |
| 30 | + const originalAdd = document.addEventListener.bind(document); |
| 31 | + const originalRemove = document.removeEventListener.bind(document); |
| 32 | + |
| 33 | + vi.spyOn(document, "addEventListener").mockImplementation((type, listener, options) => { |
| 34 | + if (DRIVER_EVENT_TYPES.includes(type)) { |
| 35 | + added.push({ |
| 36 | + type, |
| 37 | + listener: listener as EventListenerOrEventListenerObject, |
| 38 | + capture: normalizeCapture(options), |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + return originalAdd(type, listener as EventListener, options); |
| 43 | + }); |
| 44 | + |
| 45 | + vi.spyOn(document, "removeEventListener").mockImplementation((type, listener, options) => { |
| 46 | + if (DRIVER_EVENT_TYPES.includes(type)) { |
| 47 | + removed.push({ |
| 48 | + type, |
| 49 | + listener: listener as EventListenerOrEventListenerObject, |
| 50 | + capture: normalizeCapture(options), |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + return originalRemove(type, listener as EventListener, options); |
| 55 | + }); |
| 56 | + |
| 57 | + function liveCount(): number { |
| 58 | + const outstanding = [...removed]; |
| 59 | + let live = 0; |
| 60 | + |
| 61 | + for (const record of added) { |
| 62 | + const idx = outstanding.findIndex( |
| 63 | + candidate => |
| 64 | + candidate.type === record.type && |
| 65 | + candidate.listener === record.listener && |
| 66 | + candidate.capture === record.capture |
| 67 | + ); |
| 68 | + |
| 69 | + if (idx === -1) { |
| 70 | + live++; |
| 71 | + } else { |
| 72 | + outstanding.splice(idx, 1); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return live; |
| 77 | + } |
| 78 | + |
| 79 | + return { liveCount }; |
| 80 | +} |
| 81 | + |
| 82 | +describe("document listener cleanup", () => { |
| 83 | + it("attaches document click listeners while the tour is active", () => { |
| 84 | + const tracker = trackDocumentListeners(); |
| 85 | + const d = createDriver({ animate: false, steps: SAMPLE_STEPS }); |
| 86 | + d.drive(); |
| 87 | + |
| 88 | + expect(tracker.liveCount()).toBeGreaterThan(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it("removes every document click listener once the tour is destroyed", () => { |
| 92 | + const tracker = trackDocumentListeners(); |
| 93 | + const d = createDriver({ animate: false, steps: SAMPLE_STEPS }); |
| 94 | + d.drive(); |
| 95 | + d.destroy(); |
| 96 | + |
| 97 | + expect(tracker.liveCount()).toBe(0); |
| 98 | + }); |
| 99 | + |
| 100 | + it("does not accumulate document listeners as the tour moves between steps", () => { |
| 101 | + const tracker = trackDocumentListeners(); |
| 102 | + const d = createDriver({ animate: false, steps: SAMPLE_STEPS }); |
| 103 | + d.drive(); |
| 104 | + |
| 105 | + const afterFirstStep = tracker.liveCount(); |
| 106 | + |
| 107 | + navButton("next")?.click(); |
| 108 | + navButton("next")?.click(); |
| 109 | + |
| 110 | + expect(tracker.liveCount()).toBe(afterFirstStep); |
| 111 | + }); |
| 112 | + |
| 113 | + it("removes the keydown focus-trap listener when destroyed", () => { |
| 114 | + const removeSpy = vi.spyOn(window, "removeEventListener"); |
| 115 | + const d = createDriver({ animate: false, steps: SAMPLE_STEPS }); |
| 116 | + d.drive(); |
| 117 | + d.destroy(); |
| 118 | + |
| 119 | + expect(removeSpy.mock.calls.some(([type]) => type === "keydown")).toBe(true); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe("overlay pointer suppression", () => { |
| 124 | + it("prevents default on pointer events over the overlay to block page interaction", async () => { |
| 125 | + const d = createDriver({ animate: false, steps: SAMPLE_STEPS }); |
| 126 | + d.drive(); |
| 127 | + await nextFrame(); |
| 128 | + |
| 129 | + const path = document.querySelector(".driver-overlay path"); |
| 130 | + expect(path).not.toBeNull(); |
| 131 | + |
| 132 | + for (const type of ["pointerdown", "mousedown", "pointerup", "mouseup"]) { |
| 133 | + const event = new MouseEvent(type, { bubbles: true, cancelable: true }); |
| 134 | + path!.dispatchEvent(event); |
| 135 | + expect(event.defaultPrevented).toBe(true); |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it("still routes overlay clicks to the click handler", async () => { |
| 140 | + const onNextClick = vi.fn(); |
| 141 | + const d = createDriver({ |
| 142 | + animate: false, |
| 143 | + overlayClickBehavior: "nextStep", |
| 144 | + steps: SAMPLE_STEPS, |
| 145 | + onNextClick, |
| 146 | + }); |
| 147 | + d.drive(); |
| 148 | + await nextFrame(); |
| 149 | + |
| 150 | + document.querySelector(".driver-overlay path")?.dispatchEvent(new MouseEvent("click", { bubbles: true })); |
| 151 | + |
| 152 | + expect(onNextClick).toHaveBeenCalledTimes(1); |
| 153 | + }); |
| 154 | +}); |
0 commit comments