-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_providers.test.ts
181 lines (163 loc) · 5.41 KB
/
storage_providers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// window_storage_test.ts
import {
assert,
assertEquals,
assertNotEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { getProvider } from "./storage_providers.ts";
Deno.test("WindowProvider: should initialize with initialState", () => {
const initialState = { key: "initialValue" };
const provider = getProvider("window", initialState);
assertEquals(provider.getState(), initialState);
});
Deno.test("SessionStorageProvider: should initialize with initialState", () => {
const initialState = { key: "initialValue" };
const provider = getProvider("sessionStorage", initialState);
assertEquals(provider.getState(), initialState);
});
Deno.test("LocalStorageProvider: should initialize with initialState", () => {
const initialState = { key: "initialValue" };
const provider = getProvider("localStorage", initialState);
assertEquals(provider.getState(), initialState);
});
Deno.test("LocalStorageProvider: should set and get state", () => {
const provider = getProvider("localStorage");
const state = { key: "value" };
provider.setState(state);
assertEquals(provider.getState(), state);
});
Deno.test("LocalStorageProvider: should call listener when state is set", () => {
const provider = getProvider("localStorage");
let called = false;
provider.addListener(() => {
called = true;
});
provider.setState({ key: "newValue" });
assert(called);
});
Deno.test("LocalStorageProvider: should remove listener", () => {
const provider = getProvider("localStorage");
let callCount = 0;
const listener = () => {
callCount++;
};
const removeListener = provider.addListener(listener);
provider.setState({ key: "newValue" });
assertEquals(callCount, 1);
removeListener();
provider.setState({ key: "newerValue" });
assertEquals(callCount, 1); // No new calls
});
Deno.test("LocalStorageProvider: should handle custom namespace", () => {
const provider1 = getProvider<Record<string, unknown> | undefined>(
"localStorage",
undefined,
"namespace1",
);
const provider2 = getProvider<Record<string, unknown> | undefined>(
"localStorage",
undefined,
"namespace2",
);
const state1 = { key: "value1" };
const state2 = { key: "value2" };
provider1.setState(state1);
provider2.setState(state2);
assertEquals(provider1.getState(), state1);
assertEquals(provider2.getState(), state2);
assertNotEquals(provider1.getState(), provider2.getState());
});
Deno.test("WindowProvider: should set and get state", () => {
const provider = getProvider("window");
const state = { key: "value" };
provider.setState(state);
assertEquals(provider.getState(), state);
});
Deno.test("SessionStorageProvider: should set and get state", () => {
const provider = getProvider("sessionStorage");
const state = { key: "value" };
provider.setState(state);
assertEquals(provider.getState(), state);
});
Deno.test("WindowProvider: should call listener when state is set", () => {
const provider = getProvider("window");
let called = false;
provider.addListener(() => {
called = true;
});
provider.setState({ key: "newValue" });
assert(called);
});
Deno.test("SessionStorageProvider: should call listener when state is set", () => {
const provider = getProvider("sessionStorage");
let called = false;
provider.addListener(() => {
called = true;
});
provider.setState({ key: "newValue" });
assert(called);
});
Deno.test("WindowProvider: should remove listener", () => {
const provider = getProvider("window");
let callCount = 0;
const listener = () => {
callCount++;
};
const removeListener = provider.addListener(listener);
provider.setState({ key: "newValue" });
assertEquals(callCount, 1);
removeListener();
provider.setState({ key: "newerValue" });
assertEquals(callCount, 1); // No new calls
});
Deno.test("SessionStorageProvider: should remove listener", () => {
const provider = getProvider("sessionStorage");
let callCount = 0;
const listener = () => {
callCount++;
};
const removeListener = provider.addListener(listener);
provider.setState({ key: "newValue" });
assertEquals(callCount, 1);
removeListener();
provider.setState({ key: "newerValue" });
assertEquals(callCount, 1); // No new calls
});
Deno.test("WindowProvider: should handle custom namespace", () => {
const provider1 = getProvider<Record<string, unknown> | undefined>(
"window",
undefined,
"namespace1",
);
const provider2 = getProvider<Record<string, unknown> | undefined>(
"window",
undefined,
"namespace2",
);
const state1 = { key: "value1" };
const state2 = { key: "value2" };
provider1.setState(state1);
provider2.setState(state2);
assertEquals(provider1.getState(), state1);
assertEquals(provider2.getState(), state2);
assertNotEquals(provider1.getState(), provider2.getState());
});
Deno.test("SessionStorageProvider: should handle custom namespace", () => {
const provider1 = getProvider<Record<string, unknown> | undefined>(
"sessionStorage",
undefined,
"namespace1",
);
const provider2 = getProvider<Record<string, unknown> | undefined>(
"sessionStorage",
undefined,
"namespace2",
);
const state1 = { key: "value1" };
const state2 = { key: "value2" };
provider1.setState(state1);
provider2.setState(state2);
assertEquals(provider1.getState(), state1);
assertEquals(provider2.getState(), state2);
assertNotEquals(provider1.getState(), provider2.getState());
});