Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

draftHeroTransition.test.ts

123 lines3.8 KB

1import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 DRAFT_HERO_TRANSITION_ANIMATION_ID,
5 runMobileComposerTransition,
6 waitForDraftHeroTransition,
7} from "./draftHeroTransition";
8​
9afterEach(() => {
10 vi.unstubAllGlobals();
11});
12​
13describe("waitForDraftHeroTransition", () => {
14 it("waits for active draft hero animations and ignores unrelated animations", async () => {
15 let finishTransition: (() => void) | undefined;
16 const transitionFinished = new Promise<void>((resolve) => {
17 finishTransition = resolve;
18 });
19 vi.stubGlobal("document", {
20 getAnimations: () => [
21 { id: "unrelated-animation", finished: new Promise<void>(() => undefined) },
22 { id: DRAFT_HERO_TRANSITION_ANIMATION_ID, finished: transitionFinished },
23 ],
24 });
25​
26 let handoffComplete = false;
27 const handoff = waitForDraftHeroTransition().then(() => {
28 handoffComplete = true;
29 });
30 await Promise.resolve();
31 expect(handoffComplete).toBe(false);
32​
33 finishTransition?.();
34 await handoff;
35 expect(handoffComplete).toBe(true);
36 });
37​
38 it("allows the handoff when an active transition is cancelled", async () => {
39 vi.stubGlobal("document", {
40 getAnimations: () => [
41 {
42 id: DRAFT_HERO_TRANSITION_ANIMATION_ID,
43 finished: Promise.reject(new Error("cancelled")),
44 },
45 ],
46 });
47​
48 await expect(waitForDraftHeroTransition()).resolves.toBeUndefined();
49 });
50});
51​
52describe("runMobileComposerTransition", () => {
53 it("keeps the route handoff waiting while the mobile composer morph is active", async () => {
54 let finishTransition: (() => void) | undefined;
55 const transitionFinished = new Promise<void>((resolve) => {
56 finishTransition = resolve;
57 });
58 const dataset: Record<string, string> = {};
59 vi.stubGlobal("document", {
60 documentElement: { dataset },
61 getAnimations: () => [],
62 startViewTransition: (update: () => void | Promise<void>) => {
63 void update();
64 return { finished: transitionFinished };
65 },
66 });
67 vi.stubGlobal("window", {
68 matchMedia: (query: string) => ({ matches: query === "(max-width: 639px)" }),
69 });
70​
71 const transition = runMobileComposerTransition(() => undefined);
72 await Promise.resolve();
73​
74 let handoffComplete = false;
75 const handoff = waitForDraftHeroTransition().then(() => {
76 handoffComplete = true;
77 });
78 await Promise.resolve();
79 expect(handoffComplete).toBe(false);
80​
81 finishTransition?.();
82 await Promise.all([transition, handoff]);
83 expect(handoffComplete).toBe(true);
84 });
85​
86 it("uses a scoped view transition on mobile", async () => {
87 const dataset: Record<string, string> = {};
88 const startViewTransition = vi.fn((update: () => void | Promise<void>) => ({
89 finished: Promise.resolve(update()).then(() => undefined),
90 }));
91 vi.stubGlobal("document", {
92 documentElement: { dataset },
93 startViewTransition,
94 });
95 vi.stubGlobal("window", {
96 matchMedia: (query: string) => ({ matches: query === "(max-width: 639px)" }),
97 });
98 const update = vi.fn();
99​
100 await runMobileComposerTransition(update);
101​
102 expect(startViewTransition).toHaveBeenCalledOnce();
103 expect(update).toHaveBeenCalledOnce();
104 expect(dataset).not.toHaveProperty("mobileComposerRouteTransition");
105 });
106​
107 it("updates without a view transition when reduced motion is preferred", async () => {
108 const startViewTransition = vi.fn();
109 vi.stubGlobal("document", {
110 documentElement: { dataset: {} },
111 startViewTransition,
112 });
113 vi.stubGlobal("window", {
114 matchMedia: () => ({ matches: true }),
115 });
116 const update = vi.fn();
117​
118 await runMobileComposerTransition(update);
119​
120 expect(startViewTransition).not.toHaveBeenCalled();
121 expect(update).toHaveBeenCalledOnce();
122 });
123});