-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionStorageTest.ts
332 lines (277 loc) · 8.9 KB
/
SessionStorageTest.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import "jest";
import { Storage, StorageProvider } from "../src";
let storage: Storage;
const KEY = "abc";
beforeEach(() => {
sessionStorage.clear();
jest.clearAllMocks();
storage = StorageProvider.sessionStorage("test");
});
describe("key concatenation works correctly", () => {
test("on get", () => {
storage.get(KEY);
expect(sessionStorage.getItem).toHaveBeenLastCalledWith("test_abc");
});
test("on set", () => {
storage.set(KEY, "someValue");
expect(sessionStorage.setItem).toHaveBeenLastCalledWith(
"test_abc",
"someValue",
);
});
test("on del", () => {
storage.del(KEY);
expect(sessionStorage.removeItem).toHaveBeenLastCalledWith("test_abc");
});
test("on del multiple", () => {
storage.del([KEY, "efg"]);
expect(sessionStorage.removeItem).toHaveBeenCalledWith("test_abc");
expect(sessionStorage.removeItem).toHaveBeenLastCalledWith("test_efg");
});
});
describe("set does correctly serialize values of type", () => {
test("string", () => {
storage.set(KEY, "someValue");
expect(sessionStorage.__STORE__["test_" + KEY]).toBe("someValue");
});
test("boolean", () => {
storage.set(KEY, false);
expect(sessionStorage.__STORE__["test_" + KEY]).toBe("false");
});
test("number", () => {
storage.set(KEY, -524.6);
expect(sessionStorage.__STORE__["test_" + KEY]).toBe("-524.6");
});
test("object", () => {
storage.set(KEY, { name: "Jane Doe" });
expect(sessionStorage.__STORE__["test_" + KEY]).toBe('{"name":"Jane Doe"}');
});
test("array", () => {
storage.set(KEY, ["eins", "zwei"]);
expect(sessionStorage.__STORE__["test_" + KEY]).toBe('["eins","zwei"]');
});
test("multiple types in one call", () => {
storage.set({
str: "someString",
bool: true,
obj: {
val: "innerValue",
},
});
expect(sessionStorage.__STORE__.test_str).toBe("someString");
expect(sessionStorage.__STORE__.test_bool).toBe("true");
expect(sessionStorage.__STORE__.test_obj).toBe('{"val":"innerValue"}');
});
});
describe("get value of type string as ", () => {
test("any returns value", () => {
storage.set(KEY, "someValue");
expect(storage.get(KEY)).toBe("someValue");
});
test("string returns value", () => {
storage.set(KEY, "someValue");
expect(storage.getAsString(KEY)).toBe("someValue");
});
test("number returns undefined", () => {
storage.set(KEY, "someValue");
expect(storage.getAsNumber(KEY)).toBe(undefined);
});
test("boolean returns undefined", () => {
storage.set(KEY, "someValue");
expect(storage.getAsBoolean(KEY)).toBe(undefined);
});
test("object returns undefined", () => {
storage.set(KEY, "someValue");
expect(storage.getAsRecord(KEY)).toBe(undefined);
});
test("array returns undefined", () => {
storage.set(KEY, "someValue");
expect(storage.getAsArray(KEY)).toBe(undefined);
});
});
describe("get value of type number as ", () => {
test("any returns value as string", () => {
storage.set(KEY, -25.6);
expect(storage.get(KEY)).toBe("-25.6");
});
test("string returns value as string", () => {
storage.set(KEY, -25.6);
expect(storage.getAsString(KEY)).toBe("-25.6");
});
test("number returns value", () => {
storage.set(KEY, -25.6);
expect(storage.getAsNumber(KEY)).toBe(-25.6);
});
test("boolean returns undefined", () => {
storage.set(KEY, -25.6);
expect(storage.getAsBoolean(KEY)).toBe(undefined);
});
test("object returns undefined", () => {
storage.set(KEY, -25.6);
expect(storage.getAsRecord(KEY)).toBe(undefined);
});
test("array returns undefined", () => {
storage.set(KEY, -25.6);
expect(storage.getAsArray(KEY)).toBe(undefined);
});
test("zero returns zero", () => {
storage.set(KEY, 0);
expect(storage.getAsNumber(KEY)).toBe(0);
});
});
describe("get value of type object as ", () => {
const object = { test: "someValue" };
test("any returns value as string", () => {
storage.set(KEY, object);
expect(storage.get(KEY)).toBe('{"test":"someValue"}');
});
test("string returns value as string", () => {
storage.set(KEY, object);
expect(storage.getAsString(KEY)).toBe('{"test":"someValue"}');
});
test("number returns undefined", () => {
storage.set(KEY, object);
expect(storage.getAsNumber(KEY)).toBe(undefined);
});
test("boolean returns undefined", () => {
storage.set(KEY, object);
expect(storage.getAsBoolean(KEY)).toBe(undefined);
});
test("object returns object", () => {
storage.set(KEY, object);
expect(storage.getAsRecord(KEY)).toEqual(object);
});
test("array returns undefined", () => {
storage.set(KEY, object);
expect(storage.getAsArray(KEY)).toBe(undefined);
});
});
describe("get value as Object", () => {
interface TestObj extends Record<string, unknown> {
field: string;
age: number;
optional?: string;
}
const validObject: TestObj = {
field: "foo",
age: 23,
};
const invalidObject = {
age: 24,
optional: "someString",
};
test("valid object returns valid object", () => {
storage.set(KEY, validObject);
expect(storage.getAsObject<TestObj>(KEY)).toEqual(validObject);
});
test("invalid object returns invalid object, without type check", () => {
storage.set(KEY, invalidObject);
expect(storage.getAsObject<TestObj>(KEY)).toEqual(invalidObject);
});
test("invalid object returns undefined, with type check", () => {
const check = (o: Record<string, unknown>): o is TestObj => {
return o.field !== undefined && o.age !== undefined;
};
storage.set(KEY, invalidObject);
expect(storage.getAsObject<TestObj>(KEY, check)).toEqual(undefined);
});
});
describe("get value of type array as ", () => {
const array = ["eins", "zwei"];
test("any returns value as string", () => {
storage.set(KEY, array);
expect(storage.get(KEY)).toBe('["eins","zwei"]');
});
test("string returns value as string", () => {
storage.set(KEY, array);
expect(storage.getAsString(KEY)).toBe('["eins","zwei"]');
});
test("number returns undefined", () => {
storage.set(KEY, array);
expect(storage.getAsNumber(KEY)).toBe(undefined);
});
test("boolean returns undefined", () => {
storage.set(KEY, array);
expect(storage.getAsBoolean(KEY)).toBe(undefined);
});
test("object returns undefined", () => {
storage.set(KEY, array);
expect(storage.getAsRecord(KEY)).toEqual(array);
});
test("array returns array", () => {
storage.set(KEY, array);
expect(storage.getAsArray(KEY)).toEqual(array);
});
});
describe("get value of type boolean as ", () => {
const value = true;
test("any returns value as string", () => {
storage.set(KEY, value);
expect(storage.get(KEY)).toBe("true");
});
test("string returns value as string", () => {
storage.set(KEY, false);
expect(storage.getAsString(KEY)).toBe("false");
});
test("number returns undefined", () => {
storage.set(KEY, value);
expect(storage.getAsNumber(KEY)).toBe(undefined);
});
test("boolean returns boolean", () => {
storage.set(KEY, value);
expect(storage.getAsBoolean(KEY)).toBe(true);
});
test("object returns undefined", () => {
storage.set(KEY, value);
expect(storage.getAsRecord(KEY)).toBe(undefined);
});
test("array returns undefined", () => {
storage.set(KEY, value);
expect(storage.getAsArray(KEY)).toBe(undefined);
});
});
describe("get* of non existent key returns undefined", () => {
test("get", () => {
expect(storage.get(KEY)).toBe(undefined);
});
test("getAsString", () => {
expect(storage.getAsString(KEY)).toBe(undefined);
});
test("getAsNumber", () => {
expect(storage.getAsNumber(KEY)).toBe(undefined);
});
test("getAsBoolean", () => {
expect(storage.getAsBoolean(KEY)).toBe(undefined);
});
test("getAsRecord", () => {
expect(storage.getAsRecord(KEY)).toBe(undefined);
});
test("getAsArray", () => {
expect(storage.getAsArray(KEY)).toBe(undefined);
});
});
describe("size and isEmpty works returns correct values", () => {
test("empty store returns empty", () => {
expect(storage.isEmpty()).toBe(true);
});
test("empty store size is zero", () => {
expect(storage.size()).toBe(0);
});
test("non empty store returns non empty", () => {
storage.set("some_key", "Value");
expect(storage.isEmpty()).toBe(false);
});
test("isEmpty only considers only keys with matching prefix", () => {
const rootStorage = StorageProvider.sessionStorage();
rootStorage.set("other_key", "otherVal");
expect(storage.isEmpty()).toBe(true);
expect(rootStorage.isEmpty()).toBe(false);
});
test("size only considers only keys with matching prefix", () => {
const rootStorage = StorageProvider.sessionStorage();
storage.set("some_key", "Value");
rootStorage.set("other_key", "otherVal");
expect(storage.size()).toBe(1);
expect(rootStorage.size()).toBe(2);
});
});