|
| 1 | +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | + |
| 3 | +import { runMobileComposerTransition } from "./draftHeroTransition"; |
| 4 | + |
| 5 | +type StartViewTransition = (update: () => void | Promise<void>) => { |
| 6 | + finished: Promise<void>; |
| 7 | +}; |
| 8 | + |
| 9 | +function stubEnvironment(input: { |
| 10 | + mobile: boolean; |
| 11 | + reducedMotion?: boolean; |
| 12 | + startViewTransition?: StartViewTransition; |
| 13 | +}) { |
| 14 | + const styles = new Map<string, string>(); |
| 15 | + const root = { |
| 16 | + style: { |
| 17 | + setProperty: (name: string, value: string) => styles.set(name, value), |
| 18 | + removeProperty: (name: string) => styles.delete(name), |
| 19 | + getPropertyValue: (name: string) => styles.get(name) ?? "", |
| 20 | + }, |
| 21 | + dataset: {} as Record<string, string>, |
| 22 | + }; |
| 23 | + vi.stubGlobal("window", { |
| 24 | + matchMedia: (query: string) => ({ |
| 25 | + matches: query.includes("max-width") ? input.mobile : (input.reducedMotion ?? false), |
| 26 | + }), |
| 27 | + }); |
| 28 | + vi.stubGlobal("document", { |
| 29 | + documentElement: root, |
| 30 | + ...(input.startViewTransition ? { startViewTransition: input.startViewTransition } : {}), |
| 31 | + }); |
| 32 | + return root; |
| 33 | +} |
| 34 | + |
| 35 | +/** A view transition that runs its update after the old snapshot, like the browser does. */ |
| 36 | +function deferredViewTransition(): StartViewTransition { |
| 37 | + return (update) => ({ |
| 38 | + finished: Promise.resolve().then(update), |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe("runMobileComposerTransition", () => { |
| 43 | + afterEach(() => { |
| 44 | + vi.unstubAllGlobals(); |
| 45 | + }); |
| 46 | + |
| 47 | + it.each([ |
| 48 | + { name: "desktop viewport", mobile: false, active: true }, |
| 49 | + { name: "reduced motion", mobile: true, reducedMotion: true, active: true }, |
| 50 | + { name: "panel motion off", mobile: true, active: false }, |
| 51 | + { name: "no view transition support", mobile: true, active: true, unsupported: true }, |
| 52 | + ])("applies the update before returning on $name", ({ unsupported, active, ...env }) => { |
| 53 | + const startViewTransition = vi.fn(deferredViewTransition()); |
| 54 | + stubEnvironment({ ...env, ...(unsupported ? {} : { startViewTransition }) }); |
| 55 | + const update = vi.fn(); |
| 56 | + |
| 57 | + const docking = runMobileComposerTransition(update, { active, durationMs: 200 }); |
| 58 | + |
| 59 | + expect(docking).toBeUndefined(); |
| 60 | + expect(update).toHaveBeenCalledTimes(1); |
| 61 | + expect(startViewTransition).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("applies the update once inside a mobile view transition and resolves after it", async () => { |
| 65 | + let finish!: () => void; |
| 66 | + const finished = new Promise<void>((resolve) => { |
| 67 | + finish = resolve; |
| 68 | + }); |
| 69 | + const startViewTransition = vi.fn((update: () => void) => { |
| 70 | + void Promise.resolve().then(update); |
| 71 | + return { finished }; |
| 72 | + }); |
| 73 | + const root = stubEnvironment({ mobile: true, startViewTransition }); |
| 74 | + const update = vi.fn(); |
| 75 | + |
| 76 | + const docking = runMobileComposerTransition(update, { active: true, durationMs: 200 }); |
| 77 | + |
| 78 | + expect(update).not.toHaveBeenCalled(); |
| 79 | + expect(root.dataset.mobileComposerRouteTransition).toBe("true"); |
| 80 | + expect(root.style.getPropertyValue("--mobile-composer-transition-duration")).toBe("200ms"); |
| 81 | + await docking; |
| 82 | + expect(update).toHaveBeenCalledTimes(1); |
| 83 | + expect(root.dataset.mobileComposerRouteTransition).toBe("true"); |
| 84 | + finish(); |
| 85 | + await finished; |
| 86 | + expect(root.dataset.mobileComposerRouteTransition).toBeUndefined(); |
| 87 | + expect(root.style.getPropertyValue("--mobile-composer-transition-duration")).toBe(""); |
| 88 | + expect(update).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it.each([ |
| 92 | + { |
| 93 | + name: "the transition cannot start", |
| 94 | + startViewTransition: (): ReturnType<StartViewTransition> => { |
| 95 | + throw new Error("InvalidStateError"); |
| 96 | + }, |
| 97 | + update: vi.fn(), |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "the update throws", |
| 101 | + startViewTransition: deferredViewTransition(), |
| 102 | + update: vi.fn(() => { |
| 103 | + throw new Error("render failed"); |
| 104 | + }), |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "an inline update throws", |
| 108 | + mobile: false, |
| 109 | + startViewTransition: deferredViewTransition(), |
| 110 | + update: vi.fn(() => { |
| 111 | + throw new Error("render failed"); |
| 112 | + }), |
| 113 | + }, |
| 114 | + ])( |
| 115 | + "still releases the caller once when $name", |
| 116 | + async ({ mobile = true, startViewTransition, update }) => { |
| 117 | + stubEnvironment({ mobile, startViewTransition }); |
| 118 | + |
| 119 | + await runMobileComposerTransition(update, { active: true, durationMs: 200 }); |
| 120 | + |
| 121 | + expect(update).toHaveBeenCalledTimes(1); |
| 122 | + }, |
| 123 | + ); |
| 124 | +}); |
0 commit comments