Open History to see the last commit
1import * as Schema from "effect/Schema";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3
4function createStorage(overrides: Partial<Storage> = {}): Storage {
5 const store = new Map<string, string>();
6 return {
7 clear: () => store.clear(),
8 getItem: (key) => store.get(key) ?? null,
9 key: (index) => [...store.keys()][index] ?? null,
10 get length() {
11 return store.size;
12 },
13 removeItem: (key) => {
14 store.delete(key);
15 },
16 setItem: (key, value) => {
17 store.set(key, value);
18 },
19 ...overrides,
20 };
21}
22
23async function loadWithStorage(storage: Storage) {
24 vi.stubGlobal("window", { localStorage: storage });
25 vi.stubGlobal("localStorage", storage);
26 return import("./useLocalStorage");
27}
28
29afterEach(() => {
30 vi.resetModules();
31 vi.unstubAllGlobals();
32});
33
34describe("local storage errors", () => {
35 it("preserves read failure context", async () => {
36 const cause = new Error("storage unavailable");
37 const { getLocalStorageItem, LocalStorageOperationError } = await loadWithStorage(
38 createStorage({
39 getItem: () => {
40 throw cause;
41 },
42 }),
43 );
44
45 try {
46 getLocalStorageItem("read-key", Schema.String);
47 expect.unreachable("expected the read to fail");
48 } catch (error) {
49 expect(error).toBeInstanceOf(LocalStorageOperationError);
50 expect(error).toMatchObject({
51 operation: "read",
52 storageKey: "read-key",
53 cause,
54 });
55 }
56 });
57
58 it("preserves decode failure context", async () => {
59 const { getLocalStorageItem, LocalStorageOperationError } = await loadWithStorage(
60 createStorage({ getItem: () => "not-json" }),
61 );
62
63 try {
64 getLocalStorageItem("decode-key", Schema.String);
65 expect.unreachable("expected decoding to fail");
66 } catch (error) {
67 expect(error).toBeInstanceOf(LocalStorageOperationError);
68 expect(error).toMatchObject({
69 operation: "decode",
70 storageKey: "decode-key",
71 cause: expect.anything(),
72 });
73 }
74 });
75
76 it("preserves write failure context", async () => {
77 const cause = new Error("storage quota exceeded");
78 const { LocalStorageOperationError, setLocalStorageItem } = await loadWithStorage(
79 createStorage({
80 setItem: () => {
81 throw cause;
82 },
83 }),
84 );
85
86 try {
87 setLocalStorageItem("write-key", "value", Schema.String);
88 expect.unreachable("expected the write to fail");
89 } catch (error) {
90 expect(error).toBeInstanceOf(LocalStorageOperationError);
91 expect(error).toMatchObject({
92 operation: "write",
93 storageKey: "write-key",
94 cause,
95 });
96 }
97 });
98
99 it("preserves removal failure context", async () => {
100 const cause = new Error("storage unavailable");
101 const { LocalStorageOperationError, removeLocalStorageItem } = await loadWithStorage(
102 createStorage({
103 removeItem: () => {
104 throw cause;
105 },
106 }),
107 );
108
109 try {
110 removeLocalStorageItem("remove-key");
111 expect.unreachable("expected the removal to fail");
112 } catch (error) {
113 expect(error).toBeInstanceOf(LocalStorageOperationError);
114 expect(error).toMatchObject({
115 operation: "remove",
116 storageKey: "remove-key",
117 cause,
118 });
119 }
120 });
121});