-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheEntryPrune.test.mjs
130 lines (103 loc) · 3.57 KB
/
cacheEntryPrune.test.mjs
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
// @ts-check
import { deepStrictEqual, strictEqual, throws } from "node:assert";
import Cache from "./Cache.mjs";
import cacheEntryPrune from "./cacheEntryPrune.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertInstanceOf from "./test/assertInstanceOf.mjs";
/**
* Adds `cacheEntryPrune` tests.
* @param {import("test-director").default} tests Test director.
*/
export default (tests) => {
tests.add("`cacheEntryPrune` bundle size.", async () => {
await assertBundleSize(
new URL("./cacheEntryPrune.mjs", import.meta.url),
350
);
});
tests.add(
"`cacheEntryPrune` argument 1 `cache` not a `Cache` instance.",
() => {
throws(() => {
cacheEntryPrune(
// @ts-expect-error Testing invalid.
true,
"a"
);
}, new TypeError("Argument 1 `cache` must be a `Cache` instance."));
}
);
tests.add("`cacheEntryPrune` argument 2 `cacheKey` not a string.", () => {
throws(() => {
cacheEntryPrune(
new Cache(),
// @ts-expect-error Testing invalid.
true
);
}, new TypeError("Argument 2 `cacheKey` must be a string."));
});
tests.add("`cacheEntryPrune` with entry not populated.", () => {
const initialCacheStore = { a: 1 };
const cache = new Cache({ ...initialCacheStore });
/** @type {Array<Event>} */
const events = [];
/** @type {EventListener} */
const listener = (event) => {
events.push(event);
};
cache.addEventListener("b/prune", listener);
cache.addEventListener("b/delete", listener);
cacheEntryPrune(cache, "b");
deepStrictEqual(events, []);
deepStrictEqual(cache.store, initialCacheStore);
});
tests.add(
"`cacheEntryPrune` with entry populated, prune event not canceled.",
() => {
const pruneCacheKey = "b";
const cache = new Cache({ a: 1, [pruneCacheKey]: 2 });
/** @type {Array<Event>} */
const events = [];
/** @type {EventListener} */
const listener = (event) => {
events.push(event);
};
cache.addEventListener(`${pruneCacheKey}/prune`, listener);
cache.addEventListener(`${pruneCacheKey}/delete`, listener);
cacheEntryPrune(cache, pruneCacheKey);
strictEqual(events.length, 2);
assertInstanceOf(events[0], CustomEvent);
strictEqual(events[0].type, `${pruneCacheKey}/prune`);
strictEqual(events[0].cancelable, true);
strictEqual(events[0].defaultPrevented, false);
assertInstanceOf(events[1], CustomEvent);
strictEqual(events[1].type, `${pruneCacheKey}/delete`);
strictEqual(events[1].cancelable, false);
deepStrictEqual(cache.store, { a: 1 });
}
);
tests.add(
"`cacheEntryPrune` with entry populated, prune event canceled.",
() => {
const pruneCacheKey = "b";
const initialCacheStore = { a: 1, [pruneCacheKey]: 2 };
const cache = new Cache({ ...initialCacheStore });
/** @type {Array<Event>} */
const events = [];
cache.addEventListener(`${pruneCacheKey}/prune`, (event) => {
event.preventDefault();
events.push(event);
});
cache.addEventListener(`${pruneCacheKey}/delete`, (event) => {
events.push(event);
});
cacheEntryPrune(cache, pruneCacheKey);
strictEqual(events.length, 1);
assertInstanceOf(events[0], CustomEvent);
strictEqual(events[0].type, `${pruneCacheKey}/prune`);
strictEqual(events[0].cancelable, true);
strictEqual(events[0].defaultPrevented, true);
deepStrictEqual(cache.store, initialCacheStore);
}
);
};