-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ts
172 lines (162 loc) · 5.46 KB
/
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
import {
assertEquals,
assertExists,
assertRejects,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { Adapter } from "./adapter.ts";
import { flush, setup } from "./mod.ts";
const testAdapter = new class implements Adapter {
items: Record<string, string> = {};
setItems(items: Record<string, string>) {
this.items = { ...this.items, ...items };
}
getItems() {
return this.items;
}
deleteItems(items: string[]) {
this.items = Object.fromEntries(
Object.entries(this.items).filter(([k]) => !items.includes(k)),
);
}
}();
function wait(duration = 10) {
return new Promise((r) => setTimeout(r, duration));
}
function flushAndWait() {
flush();
return wait();
}
Deno.test("errors", async (t) => {
await t.step("flush before setup", () => {
assertThrows(flush);
});
await t.step("TTL smaller than or equal to zero", () => {
assertRejects(() => setup({ adapter: testAdapter, ttl: -0 }));
assertRejects(() => setup({ adapter: testAdapter, ttl: -1 }));
});
});
Deno.test("opearations", async (t) => {
localStorage.clear();
await setup({ adapter: testAdapter, ttl: null });
await t.step("set", async () => {
localStorage.setItem("test_key", "test_value");
localStorage.setItem("test_key2", "test_value2");
localStorage.setItem("test_key3", "test_value3");
await flushAndWait();
assertEquals(
Object.fromEntries(Object.entries(localStorage)),
testAdapter.items,
);
});
await t.step("update", async () => {
localStorage.setItem("test_key2", "test_value3");
localStorage.setItem("test_key3", "test_value2");
await flushAndWait();
assertEquals(testAdapter.items["test_key2"], "test_value3");
assertEquals(testAdapter.items["test_key3"], "test_value2");
});
await t.step("delete", async () => {
localStorage.removeItem("test_key");
await flushAndWait();
assertThrows(() => assertExists(testAdapter.items["test_key"]));
});
});
Deno.test("opearations with TTL", async (t) => {
localStorage.clear();
testAdapter.items = {};
// WARNING: Do NOT use a TTL as low as 50. This is only for testing purposes.
await setup({ adapter: testAdapter, ttl: 50 });
await t.step("set", async () => {
localStorage.setItem("test_key", "test_value");
localStorage.setItem("test_key2", "test_value2");
localStorage.setItem("test_key3", "test_value3");
const fn0 = () =>
assertEquals(
Object.fromEntries(Object.entries(localStorage)),
testAdapter.items,
);
await wait(10);
assertThrows(fn0);
await wait(40);
fn0();
});
await t.step("update", async () => {
localStorage.setItem("test_key2", "test_value3");
localStorage.setItem("test_key3", "test_value2");
const fn0 = () =>
assertEquals(testAdapter.items["test_key2"], "test_value3");
const fn1 = () =>
assertEquals(testAdapter.items["test_key3"], "test_value2");
await wait(10);
assertThrows(fn0);
assertThrows(fn1);
await wait(40);
fn0();
fn1();
});
await t.step("delete", async () => {
localStorage.removeItem("test_key");
const fn0 = () =>
assertThrows(() => assertExists(testAdapter.items["test_key"]));
await wait(10);
assertThrows(fn0);
await wait(40);
});
await setup({ adapter: testAdapter, ttl: null });
});
Deno.test("include/exclude", async (t) => {
localStorage.clear();
testAdapter.items = {};
const rest = { adapter: testAdapter, ttl: null };
await t.step("exclude", async () => {
await setup({
exclude: [/_excl$/, "notSomething"],
...rest,
});
localStorage.setItem("._excll", "test_value0");
localStorage.setItem(".._excl", "test_value1");
localStorage.setItem("___excl", "test_value2");
localStorage.setItem("notSometh1ng", "test_value3");
localStorage.setItem("notSomething", "test_value4");
await flushAndWait();
assertExists(testAdapter.items["._excll"]);
assertThrows(() => assertExists(testAdapter.items[".._excl"]));
assertThrows(() => assertExists(testAdapter.items["___excl"]));
assertExists(testAdapter.items["notSometh1ng"]);
assertThrows(() => assertExists(testAdapter.items["notSomething"]));
});
await t.step("include", async () => {
await setup({
include: [/^incl_/, "yetSomething"],
...rest,
});
localStorage.setItem("incll_.", "test_value0");
localStorage.setItem("incl_..", "test_value1");
localStorage.setItem("incl___", "test_value2");
localStorage.setItem("yetSometh1ng", "test_value3");
localStorage.setItem("yetSomething", "test_value4");
await flushAndWait();
assertThrows(() => assertExists(testAdapter.items["incll_."]));
assertExists(testAdapter.items["incl_.."]);
assertExists(testAdapter.items["incl___"]);
assertThrows(() => assertExists(testAdapter.items["yetSometh1ng"]));
assertExists(testAdapter.items["yetSomething"]);
});
await t.step("both", async () => {
await setup({
include: [/^incl_/],
exclude: ["incl_s", /[0-9]/],
...rest,
});
localStorage.setItem("incl_y", "test_value0");
localStorage.setItem("incl_n", "test_value1");
localStorage.setItem("incl_s", "test_value2");
localStorage.setItem("incl_0", "test_value3");
await flushAndWait();
assertExists(testAdapter.items["incl_y"]);
assertExists(testAdapter.items["incl_n"]);
assertThrows(() => assertExists(testAdapter.items["incl_s"]));
assertThrows(() => assertExists(testAdapter.items["incl_0"]));
});
});