|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { withRouteChunkReload } from "./route-chunk-reload.ts"; |
| 3 | + |
| 4 | +function createStorage() { |
| 5 | + const values = new Map<string, string>(); |
| 6 | + return { |
| 7 | + getItem: (key: string) => values.get(key) ?? null, |
| 8 | + setItem: (key: string, value: string) => void values.set(key, value), |
| 9 | + }; |
| 10 | +} |
| 11 | + |
| 12 | +const failingLoad = () => |
| 13 | + Promise.reject(new TypeError("Failed to fetch dynamically imported module")); |
| 14 | + |
| 15 | +describe("withRouteChunkReload", () => { |
| 16 | + let location: { href: string; reload: ReturnType<typeof vi.fn> }; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + vi.useFakeTimers(); |
| 20 | + location = { href: "http://localhost/", reload: vi.fn() }; |
| 21 | + vi.stubGlobal("location", location); |
| 22 | + vi.stubGlobal("sessionStorage", createStorage()); |
| 23 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.unstubAllGlobals(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | + vi.useRealTimers(); |
| 30 | + }); |
| 31 | + |
| 32 | + it("returns the module when the import succeeds", async () => { |
| 33 | + const module = { default: () => null }; |
| 34 | + |
| 35 | + expect(await withRouteChunkReload(() => Promise.resolve(module))()).toBe(module); |
| 36 | + await vi.runAllTimersAsync(); |
| 37 | + expect(location.reload).not.toHaveBeenCalled(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("resolves with an empty component and reloads once the router reaches the destination", async () => { |
| 41 | + const module = (await withRouteChunkReload(failingLoad)()) as { default: () => unknown }; |
| 42 | + expect(module.default()).toBeNull(); |
| 43 | + |
| 44 | + // The router has not finished the navigation yet. |
| 45 | + await vi.advanceTimersByTimeAsync(100); |
| 46 | + expect(location.reload).not.toHaveBeenCalled(); |
| 47 | + |
| 48 | + // It finishes, so the URL is the destination. |
| 49 | + location.href = "http://localhost/about"; |
| 50 | + await vi.advanceTimersByTimeAsync(50); |
| 51 | + expect(location.reload).toHaveBeenCalledTimes(1); |
| 52 | + }); |
| 53 | + |
| 54 | + it("reloads after the timeout when the URL does not change", async () => { |
| 55 | + await withRouteChunkReload(failingLoad)(); |
| 56 | + |
| 57 | + await vi.advanceTimersByTimeAsync(900); |
| 58 | + expect(location.reload).not.toHaveBeenCalled(); |
| 59 | + await vi.advanceTimersByTimeAsync(200); |
| 60 | + expect(location.reload).toHaveBeenCalledTimes(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("rethrows a second failure shortly after a reload instead of reloading again", async () => { |
| 64 | + const load = withRouteChunkReload(failingLoad); |
| 65 | + |
| 66 | + await load(); |
| 67 | + await expect(load()).rejects.toThrow("Failed to fetch dynamically imported module"); |
| 68 | + await vi.runAllTimersAsync(); |
| 69 | + expect(location.reload).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("reloads again once the earlier reload is long enough ago", async () => { |
| 73 | + const load = withRouteChunkReload(failingLoad); |
| 74 | + |
| 75 | + await load(); |
| 76 | + await vi.runAllTimersAsync(); |
| 77 | + vi.setSystemTime(Date.now() + 60_000); |
| 78 | + await load(); |
| 79 | + await vi.runAllTimersAsync(); |
| 80 | + |
| 81 | + expect(location.reload).toHaveBeenCalledTimes(2); |
| 82 | + }); |
| 83 | + |
| 84 | + it("rethrows without reloading when sessionStorage is unavailable", async () => { |
| 85 | + vi.stubGlobal("sessionStorage", { |
| 86 | + getItem() { |
| 87 | + throw new Error("SecurityError"); |
| 88 | + }, |
| 89 | + setItem() { |
| 90 | + throw new Error("SecurityError"); |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + await expect(withRouteChunkReload(failingLoad)()).rejects.toThrow(); |
| 95 | + await vi.runAllTimersAsync(); |
| 96 | + expect(location.reload).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments