forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageTestBase.ts
More file actions
93 lines (76 loc) · 2.93 KB
/
StorageTestBase.ts
File metadata and controls
93 lines (76 loc) · 2.93 KB
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
import { IStorage } from "../../src/storage/IStorage.js";
export function describeStorage(
name: string,
storageFactory: () => IStorage,
afterEachCallback?: () => void,
beforeEachCallback?: () => void
): void {
describe(name, () => {
if (beforeEachCallback) {
beforeEach(beforeEachCallback);
}
if (afterEachCallback) {
afterEach(afterEachCallback);
}
test("can save item", async () => {
const storage = storageFactory();
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
const file: string = "event.json";
const value: string = "test";
await storage.setItem(file, value);
expect(await storage.length()).toBe(1);
expect(await storage.getItem(file)).toEqual(value);
expect(await storage.keys()).toEqual([file]);
expect(await storage.key(0)).toEqual(file);
});
test("can remove item", async () => {
const storage = storageFactory();
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
const file: string = "event.json";
const value: string = "test";
await storage.setItem(file, value);
expect(await storage.length()).toBe(1);
expect(await storage.keys()).toEqual([file]);
expect(await storage.key(0)).toEqual(file);
await storage.removeItem(file);
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
});
test("can clear", async () => {
const storage = storageFactory();
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
await storage.setItem("event.json", "test");
expect(await storage.length()).toBe(1);
await storage.clear();
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
});
test("can handle missing files", async () => {
const storage = storageFactory();
expect(await storage.length()).toBe(0);
expect(await storage.keys()).toEqual([]);
expect(await storage.key(0)).toBeNull();
const file: string = "event.json";
const value: string = "test";
await storage.setItem(file, value);
expect(await storage.length()).toBe(1);
expect(await storage.keys()).toEqual([file]);
expect(await storage.key(0)).toEqual(file);
await storage.removeItem("random.json");
expect(await storage.length()).toBe(1);
expect(await storage.keys()).toEqual([file]);
expect(await storage.key(0)).toEqual(file);
expect(await storage.key(1)).toBeNull();
expect(await storage.getItem("random.json")).toBeNull();
});
// what to do about null or undefined keys?
});
}