| 1 | import { afterEach, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | function storage() { |
| 4 | const values = new Map<string, string>(); |
| 5 | vi.stubGlobal("window", { |
| 6 | localStorage: { |
| 7 | getItem: (key: string) => values.get(key) ?? null, |
| 8 | setItem: (key: string, value: string) => values.set(key, value), |
| 9 | removeItem: (key: string) => values.delete(key), |
| 10 | }, |
| 11 | }); |
| 12 | } |
| 13 | |
| 14 | afterEach(() => vi.unstubAllGlobals()); |
| 15 | |
| 16 | it("resumes once after a restart and retains the opt-in until setup is closed", async () => { |
| 17 | storage(); |
| 18 | vi.resetModules(); |
| 19 | const before = await import("./snapShotSetupResume"); |
| 20 | expect(before.shouldResumeSnapShotSetupOnStartup()).toBe(false); |
| 21 | before.saveSnapShotSetupResume(false); |
| 22 | expect(before.shouldResumeSnapShotSetupOnStartup()).toBe(false); |
| 23 | |
| 24 | vi.resetModules(); |
| 25 | const after = await import("./snapShotSetupResume"); |
| 26 | expect(after.shouldResumeSnapShotSetupOnStartup()).toBe(true); |
| 27 | expect(after.shouldResumeSnapShotSetupOnStartup()).toBe(false); |
| 28 | expect(after.readSnapShotSetupResume()).toEqual({ wasEnabled: false }); |
| 29 | after.clearSnapShotSetupResume(); |
| 30 | |
| 31 | vi.resetModules(); |
| 32 | const closed = await import("./snapShotSetupResume"); |
| 33 | expect(closed.shouldResumeSnapShotSetupOnStartup()).toBe(false); |
| 34 | }); |
| 35 | |
| 36 | it("does not interrupt setup when storage is unavailable", async () => { |
| 37 | vi.stubGlobal("window", {}); |
| 38 | const resume = await import("./snapShotSetupResume"); |
| 39 | expect(() => resume.saveSnapShotSetupResume(true)).not.toThrow(); |
| 40 | expect(resume.readSnapShotSetupResume()).toBeNull(); |
| 41 | expect(() => resume.clearSnapShotSetupResume()).not.toThrow(); |
| 42 | }); |