|
| 1 | +import { options } from "preact"; |
| 2 | +import { afterEach, beforeEach, expect, test, vi } from "vitest"; |
| 3 | + |
| 4 | +import { ReflagClient } from "../src/client"; |
| 5 | +import { showToolbarToggle } from "../src/toolbar"; |
| 6 | +import { toolbarContainerId } from "../src/ui/constants"; |
| 7 | +import { cleanupUi } from "./cleanupUi"; |
| 8 | + |
| 9 | +let effects: (() => void)[]; |
| 10 | +let renders: (() => void)[]; |
| 11 | +const originalRaf = options.requestAnimationFrame; |
| 12 | +const originalDebounce = options.debounceRendering; |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + effects = []; |
| 16 | + renders = []; |
| 17 | + // Hold the actual Preact callbacks so teardown timing is deterministic. |
| 18 | + options.requestAnimationFrame = (callback) => effects.push(callback); |
| 19 | + options.debounceRendering = (callback) => renders.push(callback); |
| 20 | +}); |
| 21 | + |
| 22 | +function drainCallbacks() { |
| 23 | + while (effects.length || renders.length) { |
| 24 | + effects.shift()?.(); |
| 25 | + renders.shift()?.(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function withoutSessionStorage(callback: () => void) { |
| 30 | + const descriptor = Object.getOwnPropertyDescriptor( |
| 31 | + globalThis, |
| 32 | + "sessionStorage", |
| 33 | + )!; |
| 34 | + Reflect.deleteProperty(globalThis, "sessionStorage"); |
| 35 | + try { |
| 36 | + callback(); |
| 37 | + } finally { |
| 38 | + Object.defineProperty(globalThis, "sessionStorage", descriptor); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function mountToolbar() { |
| 43 | + const client = new ReflagClient({ |
| 44 | + publishableKey: "test-key", |
| 45 | + offline: true, |
| 46 | + toolbar: false, |
| 47 | + }); |
| 48 | + const unsubscribe = vi.fn(); |
| 49 | + const on = vi.spyOn(client, "on").mockReturnValue(unsubscribe); |
| 50 | + showToolbarToggle({ reflagClient: client }); |
| 51 | + expect(document.getElementById(toolbarContainerId)).not.toBeNull(); |
| 52 | + return { on, unsubscribe }; |
| 53 | +} |
| 54 | + |
| 55 | +afterEach(() => { |
| 56 | + cleanupUi(); |
| 57 | + drainCallbacks(); |
| 58 | + options.requestAnimationFrame = originalRaf; |
| 59 | + options.debounceRendering = originalDebounce; |
| 60 | + vi.restoreAllMocks(); |
| 61 | +}); |
| 62 | + |
| 63 | +test("unmounts before a delayed toolbar effect can run after jsdom teardown", () => { |
| 64 | + const { on } = mountToolbar(); |
| 65 | + expect(effects.length).toBeGreaterThan(0); |
| 66 | + |
| 67 | + cleanupUi(); |
| 68 | + |
| 69 | + withoutSessionStorage(() => { |
| 70 | + expect(drainCallbacks).not.toThrow(); |
| 71 | + }); |
| 72 | + expect(on).not.toHaveBeenCalled(); |
| 73 | + expect(document.getElementById(toolbarContainerId)).toBeNull(); |
| 74 | +}); |
| 75 | + |
| 76 | +test("cancels queued toolbar renders and unsubscribes mounted effects", () => { |
| 77 | + const { unsubscribe } = mountToolbar(); |
| 78 | + // Run useEffect: updateFlags queues a render and subscribes to flagsUpdated. |
| 79 | + while (effects.length) effects.shift()!(); |
| 80 | + expect(renders.length).toBeGreaterThan(0); |
| 81 | + |
| 82 | + cleanupUi(); |
| 83 | + |
| 84 | + withoutSessionStorage(() => { |
| 85 | + expect(drainCallbacks).not.toThrow(); |
| 86 | + }); |
| 87 | + expect(unsubscribe).toHaveBeenCalledOnce(); |
| 88 | + expect(document.getElementById(toolbarContainerId)).toBeNull(); |
| 89 | +}); |
0 commit comments