|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { type ControlsCallbacks, createControls } from "./controls"; |
| 3 | + |
| 4 | +function noopCallbacks(): ControlsCallbacks { |
| 5 | + return { |
| 6 | + onPlay: () => {}, |
| 7 | + onPause: () => {}, |
| 8 | + onSeek: () => {}, |
| 9 | + onSpeedChange: () => {}, |
| 10 | + onMuteToggle: () => {}, |
| 11 | + onVolumeChange: () => {}, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +describe("createControls host listeners", () => { |
| 16 | + it("removes every host listener it added on destroy", () => { |
| 17 | + const host = document.createElement("div"); |
| 18 | + document.body.appendChild(host); |
| 19 | + |
| 20 | + const addSpy = vi.spyOn(host, "addEventListener"); |
| 21 | + const removeSpy = vi.spyOn(host, "removeEventListener"); |
| 22 | + |
| 23 | + const api = createControls(host, noopCallbacks()); |
| 24 | + |
| 25 | + // Capture the exact handler references registered on the host element. |
| 26 | + const added = new Map<string, EventListenerOrEventListenerObject>(); |
| 27 | + for (const [type, handler] of addSpy.mock.calls) { |
| 28 | + added.set(type, handler as EventListenerOrEventListenerObject); |
| 29 | + } |
| 30 | + expect(added.has("mousemove")).toBe(true); |
| 31 | + expect(added.has("mouseleave")).toBe(true); |
| 32 | + |
| 33 | + api.destroy(); |
| 34 | + |
| 35 | + // Each host listener must be torn down with the same reference; anonymous |
| 36 | + // handlers (the previous bug) could never be removed, so toggling the |
| 37 | + // `controls` attribute leaked a duplicate pair on every cycle. |
| 38 | + for (const [type, handler] of added) { |
| 39 | + expect(removeSpy).toHaveBeenCalledWith(type, handler); |
| 40 | + } |
| 41 | + |
| 42 | + host.remove(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("stops reacting to host mousemove after destroy", () => { |
| 46 | + const host = document.createElement("div"); |
| 47 | + document.body.appendChild(host); |
| 48 | + |
| 49 | + const api = createControls(host, noopCallbacks()); |
| 50 | + const controls = host.querySelector<HTMLElement>(".hfp-controls"); |
| 51 | + expect(controls).not.toBeNull(); |
| 52 | + |
| 53 | + api.destroy(); |
| 54 | + |
| 55 | + // A mousemove after destroy must not revive the controls overlay. |
| 56 | + controls!.classList.add("hfp-hidden"); |
| 57 | + host.dispatchEvent(new Event("mousemove")); |
| 58 | + expect(controls!.classList.contains("hfp-hidden")).toBe(true); |
| 59 | + |
| 60 | + host.remove(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("removes its controls element from the host on destroy", () => { |
| 64 | + const host = document.createElement("div"); |
| 65 | + document.body.appendChild(host); |
| 66 | + |
| 67 | + const api = createControls(host, noopCallbacks()); |
| 68 | + const controls = host.querySelector<HTMLElement>(".hfp-controls"); |
| 69 | + expect(controls).not.toBeNull(); |
| 70 | + |
| 71 | + api.destroy(); |
| 72 | + |
| 73 | + expect(host.querySelector(".hfp-controls")).toBeNull(); |
| 74 | + expect(controls!.isConnected).toBe(false); |
| 75 | + |
| 76 | + host.remove(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments