-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cacheEntrySet.test.mjs
72 lines (58 loc) · 1.88 KB
/
cacheEntrySet.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
// @ts-check
import "./test/polyfillCustomEvent.mjs";
import { deepStrictEqual, strictEqual, throws } from "node:assert";
import { describe, it } from "node:test";
import Cache from "./Cache.mjs";
import cacheEntrySet from "./cacheEntrySet.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertInstanceOf from "./test/assertInstanceOf.mjs";
describe("Function `cacheEntrySet`.", { concurrency: true }, () => {
it("Bundle size.", async () => {
await assertBundleSize(
new URL("./cacheEntrySet.mjs", import.meta.url),
350,
);
});
it("Argument 1 `cache` not a `Cache` instance.", () => {
throws(() => {
cacheEntrySet(
// @ts-expect-error Testing invalid.
true,
"a",
{},
);
}, new TypeError("Argument 1 `cache` must be a `Cache` instance."));
});
it("Argument 2 `cacheKey` not a string.", () => {
throws(() => {
cacheEntrySet(
new Cache(),
// @ts-expect-error Testing invalid.
true,
{},
);
}, new TypeError("Argument 2 `cacheKey` must be a string."));
});
it("Sets a cache entry.", () => {
const initialCacheStore = { a: 1 };
const cache = new Cache({ ...initialCacheStore });
/** @type {Array<Event>} */
const events = [];
const setCacheKey = "b";
const setCacheValue = 2;
const setEventName = `${setCacheKey}/set`;
cache.addEventListener(setEventName, (event) => {
events.push(event);
});
cacheEntrySet(cache, setCacheKey, setCacheValue);
strictEqual(events.length, 1);
assertInstanceOf(events[0], CustomEvent);
strictEqual(events[0].type, setEventName);
strictEqual(events[0].cancelable, false);
deepStrictEqual(events[0].detail, { cacheValue: setCacheValue });
deepStrictEqual(cache.store, {
...initialCacheStore,
[setCacheKey]: setCacheValue,
});
});
});