|
| 1 | +// viewTransitionRegistry.test.ts |
| 2 | +import { ViewTransitionOptions } from "../../lib/dom/global"; |
| 3 | +import type { AppliedViewTransitionMap } from "../../lib/router/router"; |
| 4 | +import { |
| 5 | + restoreAppliedTransitions, |
| 6 | + persistAppliedTransitions, |
| 7 | + ROUTER_TRANSITIONS_STORAGE_KEY, |
| 8 | +} from "../../lib/router/router"; |
| 9 | + |
| 10 | +describe("View Transition Registry persistence", () => { |
| 11 | + let fakeStorage: Record<string, string>; |
| 12 | + let localFakeWindow: Window; |
| 13 | + |
| 14 | + // Create a fresh fakeStorage and fakeWindow before each test. |
| 15 | + beforeEach(() => { |
| 16 | + fakeStorage = {}; |
| 17 | + localFakeWindow = { |
| 18 | + sessionStorage: { |
| 19 | + getItem: jest.fn((key: string) => fakeStorage[key] || null), |
| 20 | + setItem: jest.fn((key: string, value: string) => { |
| 21 | + fakeStorage[key] = value; |
| 22 | + }), |
| 23 | + clear: jest.fn(() => { |
| 24 | + fakeStorage = {}; |
| 25 | + }), |
| 26 | + }, |
| 27 | + } as unknown as Window; |
| 28 | + jest.clearAllMocks(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("persists applied view transitions to sessionStorage", () => { |
| 32 | + const transitions: AppliedViewTransitionMap = new Map(); |
| 33 | + const innerMap = new Map<string, ViewTransitionOptions>(); |
| 34 | + // Use a sample option that matches the expected type. |
| 35 | + innerMap.set("/to", { types: ["fade"] }); |
| 36 | + transitions.set("/from", innerMap); |
| 37 | + |
| 38 | + persistAppliedTransitions(localFakeWindow, transitions); |
| 39 | + |
| 40 | + // Verify that setItem was called using our expected key. |
| 41 | + const setItemCalls = (localFakeWindow.sessionStorage.setItem as jest.Mock) |
| 42 | + .mock.calls; |
| 43 | + expect(setItemCalls.length).toBeGreaterThan(0); |
| 44 | + const [keyUsed, valueUsed] = setItemCalls[0]; |
| 45 | + const expected = JSON.stringify({ |
| 46 | + "/from": { "/to": { types: ["fade"] } }, |
| 47 | + }); |
| 48 | + expect(keyUsed).toEqual(ROUTER_TRANSITIONS_STORAGE_KEY); |
| 49 | + expect(valueUsed).toEqual(expected); |
| 50 | + // Verify our fake storage was updated. |
| 51 | + expect(fakeStorage[keyUsed]).toEqual(expected); |
| 52 | + }); |
| 53 | + |
| 54 | + it("restores applied view transitions from sessionStorage", () => { |
| 55 | + // Prepopulate fakeStorage using the module's key. |
| 56 | + const jsonData = { "/from": { "/to": { types: ["fade"] } } }; |
| 57 | + fakeStorage[ROUTER_TRANSITIONS_STORAGE_KEY] = JSON.stringify(jsonData); |
| 58 | + |
| 59 | + const transitions: AppliedViewTransitionMap = new Map(); |
| 60 | + restoreAppliedTransitions(localFakeWindow, transitions); |
| 61 | + |
| 62 | + expect(transitions.size).toBe(1); |
| 63 | + const inner = transitions.get("/from"); |
| 64 | + expect(inner).toBeDefined(); |
| 65 | + expect(inner?.size).toBe(1); |
| 66 | + expect(inner?.get("/to")).toEqual({ types: ["fade"] }); |
| 67 | + }); |
| 68 | + |
| 69 | + it("does nothing if sessionStorage is empty", () => { |
| 70 | + (localFakeWindow.sessionStorage.getItem as jest.Mock).mockReturnValue(null); |
| 71 | + const transitions: AppliedViewTransitionMap = new Map(); |
| 72 | + restoreAppliedTransitions(localFakeWindow, transitions); |
| 73 | + expect(transitions.size).toBe(0); |
| 74 | + }); |
| 75 | + |
| 76 | + it("logs an error when sessionStorage.setItem fails", () => { |
| 77 | + const error = new Error("Failed to set"); |
| 78 | + (localFakeWindow.sessionStorage.setItem as jest.Mock).mockImplementation( |
| 79 | + () => { |
| 80 | + throw error; |
| 81 | + } |
| 82 | + ); |
| 83 | + |
| 84 | + const transitions: AppliedViewTransitionMap = new Map(); |
| 85 | + const innerMap = new Map<string, ViewTransitionOptions>(); |
| 86 | + innerMap.set("/to", { types: ["fade"] }); |
| 87 | + transitions.set("/from", innerMap); |
| 88 | + |
| 89 | + const consoleWarnSpy = jest |
| 90 | + .spyOn(console, "warn") |
| 91 | + .mockImplementation(() => {}); |
| 92 | + persistAppliedTransitions(localFakeWindow, transitions); |
| 93 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 94 | + expect.stringContaining( |
| 95 | + "Failed to save applied view transitions in sessionStorage" |
| 96 | + ) |
| 97 | + ); |
| 98 | + consoleWarnSpy.mockRestore(); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("complex cases", () => { |
| 102 | + // Persist test cases: an array where each item is [description, transitions, expected JSON string]. |
| 103 | + const persistCases: [string, AppliedViewTransitionMap, string][] = [ |
| 104 | + [ |
| 105 | + "Single mapping", |
| 106 | + new Map([["/from", new Map([["/to", { types: ["fade"] }]])]]), |
| 107 | + JSON.stringify({ "/from": { "/to": { types: ["fade"] } } }), |
| 108 | + ], |
| 109 | + [ |
| 110 | + "Multiple mappings for one 'from' key", |
| 111 | + new Map([ |
| 112 | + [ |
| 113 | + "/from", |
| 114 | + new Map([ |
| 115 | + ["/to1", { types: ["slide"] }], |
| 116 | + ["/to2", { types: ["fade"] }], |
| 117 | + ]), |
| 118 | + ], |
| 119 | + ]), |
| 120 | + JSON.stringify({ |
| 121 | + "/from": { |
| 122 | + "/to1": { types: ["slide"] }, |
| 123 | + "/to2": { types: ["fade"] }, |
| 124 | + }, |
| 125 | + }), |
| 126 | + ], |
| 127 | + [ |
| 128 | + "Multiple 'from' keys", |
| 129 | + new Map([ |
| 130 | + ["/from1", new Map([["/to", { types: ["fade"] }]])], |
| 131 | + ["/from2", new Map([["/to", { types: ["slide"] }]])], |
| 132 | + ]), |
| 133 | + JSON.stringify({ |
| 134 | + "/from1": { "/to": { types: ["fade"] } }, |
| 135 | + "/from2": { "/to": { types: ["slide"] } }, |
| 136 | + }), |
| 137 | + ], |
| 138 | + ]; |
| 139 | + |
| 140 | + test.each(persistCases)( |
| 141 | + "persists applied view transitions correctly: %s", |
| 142 | + (description, transitions, expected) => { |
| 143 | + fakeStorage = {}; |
| 144 | + jest.clearAllMocks(); |
| 145 | + persistAppliedTransitions(localFakeWindow, transitions); |
| 146 | + const stored = localFakeWindow.sessionStorage.getItem( |
| 147 | + ROUTER_TRANSITIONS_STORAGE_KEY |
| 148 | + ); |
| 149 | + expect(stored).toEqual(expected); |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + // Restore test cases: an array where each item is [description, jsonData, expected transitions map]. |
| 154 | + const restoreCases: [string, any, AppliedViewTransitionMap][] = [ |
| 155 | + [ |
| 156 | + "Single mapping", |
| 157 | + { "/from": { "/to": { types: ["fade"] } } }, |
| 158 | + new Map([["/from", new Map([["/to", { types: ["fade"] }]])]]), |
| 159 | + ], |
| 160 | + [ |
| 161 | + "Multiple mappings for one 'from' key", |
| 162 | + { |
| 163 | + "/from": { |
| 164 | + "/to1": { types: ["slide"] }, |
| 165 | + "/to2": { types: ["fade"] }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + new Map([ |
| 169 | + [ |
| 170 | + "/from", |
| 171 | + new Map([ |
| 172 | + ["/to1", { types: ["slide"] }], |
| 173 | + ["/to2", { types: ["fade"] }], |
| 174 | + ]), |
| 175 | + ], |
| 176 | + ]), |
| 177 | + ], |
| 178 | + [ |
| 179 | + "Multiple 'from' keys", |
| 180 | + { |
| 181 | + "/from1": { "/to": { types: ["fade"] } }, |
| 182 | + "/from2": { "/to": { types: ["slide"] } }, |
| 183 | + }, |
| 184 | + new Map([ |
| 185 | + ["/from1", new Map([["/to", { types: ["fade"] }]])], |
| 186 | + ["/from2", new Map([["/to", { types: ["slide"] }]])], |
| 187 | + ]), |
| 188 | + ], |
| 189 | + ]; |
| 190 | + |
| 191 | + test.each(restoreCases)( |
| 192 | + "restores applied view transitions correctly: %s", |
| 193 | + (description, jsonData, expected) => { |
| 194 | + fakeStorage = {}; |
| 195 | + // Prepopulate fakeStorage using the module's key. |
| 196 | + fakeStorage[ROUTER_TRANSITIONS_STORAGE_KEY] = JSON.stringify(jsonData); |
| 197 | + |
| 198 | + const transitions: AppliedViewTransitionMap = new Map(); |
| 199 | + restoreAppliedTransitions(localFakeWindow, transitions); |
| 200 | + |
| 201 | + expect(transitions.size).toEqual(expected.size); |
| 202 | + expected.forEach((innerExpected, from) => { |
| 203 | + const innerRestored = transitions.get(from); |
| 204 | + expect(innerRestored).toBeDefined(); |
| 205 | + expect(innerRestored?.size).toEqual(innerExpected.size); |
| 206 | + innerExpected.forEach((opts, to) => { |
| 207 | + expect(innerRestored?.get(to)).toEqual(opts); |
| 208 | + }); |
| 209 | + }); |
| 210 | + } |
| 211 | + ); |
| 212 | + }); |
| 213 | +}); |
0 commit comments