-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.test.mjs
71 lines (53 loc) · 1.75 KB
/
Cache.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
// @ts-check
import { deepStrictEqual, strictEqual, throws } from "node:assert";
import Cache from "./Cache.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertInstanceOf from "./test/assertInstanceOf.mjs";
/**
* Adds `Cache` tests.
* @param {import("test-director").default} tests Test director.
*/
export default (tests) => {
tests.add("`Cache` bundle size.", async () => {
await assertBundleSize(new URL("./Cache.mjs", import.meta.url), 200);
});
tests.add("`Cache` constructor argument 1 `store`, not an object.", () => {
throws(() => {
new Cache(
// @ts-expect-error Testing invalid.
null
);
}, new TypeError("Constructor argument 1 `store` must be an object."));
});
tests.add("`Cache` constructor argument 1 `store`, missing", () => {
const cache = new Cache();
deepStrictEqual(cache.store, {});
});
tests.add("`Cache` constructor argument 1 `store`, object.", () => {
const initialStore = {
a: 1,
b: 2,
};
const cache = new Cache({ ...initialStore });
deepStrictEqual(cache.store, initialStore);
});
tests.add("`Cache` events.", () => {
const cache = new Cache();
assertInstanceOf(cache, EventTarget);
/** @type {Event | null} */
let listenedEvent = null;
/** @type {EventListener} */
const listener = (event) => {
listenedEvent = event;
};
const eventName = "a";
const event = new CustomEvent(eventName);
cache.addEventListener(eventName, listener);
cache.dispatchEvent(event);
strictEqual(listenedEvent, event);
listenedEvent = null;
cache.removeEventListener(eventName, listener);
cache.dispatchEvent(new CustomEvent(eventName));
strictEqual(listenedEvent, null);
});
};