-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
useCache.test.mjs
105 lines (87 loc) · 2.68 KB
/
useCache.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
// @ts-check
/** @import { ReactHookResult } from "./test/ReactHookTest.mjs" */
import { deepStrictEqual, ok, strictEqual } from "node:assert";
import { describe, it } from "node:test";
import React from "react";
import ReactTestRenderer from "react-test-renderer";
import Cache from "./Cache.mjs";
import CacheContext from "./CacheContext.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import createReactTestRenderer from "./test/createReactTestRenderer.mjs";
import ReactHookTest from "./test/ReactHookTest.mjs";
import useCache from "./useCache.mjs";
describe("React hook `useCache`.", { concurrency: true }, () => {
it("Bundle size.", async () => {
await assertBundleSize(new URL("./useCache.mjs", import.meta.url), 350);
});
it("Cache context missing.", () => {
/** @type {Array<ReactHookResult>} */
const results = [];
createReactTestRenderer(
React.createElement(ReactHookTest, {
useHook: useCache,
results,
}),
);
strictEqual(results.length, 1);
ok("threw" in results[0]);
deepStrictEqual(results[0].threw, new TypeError("Cache context missing."));
});
it("Cache context value not a `Cache` instance.", () => {
/** @type {Array<ReactHookResult>} */
const results = [];
createReactTestRenderer(
React.createElement(
CacheContext.Provider,
{
// @ts-expect-error Testing invalid.
value: true,
},
React.createElement(ReactHookTest, {
useHook: useCache,
results,
}),
),
);
strictEqual(results.length, 1);
ok("threw" in results[0]);
deepStrictEqual(
results[0].threw,
new TypeError("Cache context value must be a `Cache` instance."),
);
});
it("Getting the cache.", () => {
const cacheA = new Cache();
/** @type {Array<ReactHookResult>} */
const results = [];
const testRenderer = createReactTestRenderer(
React.createElement(
CacheContext.Provider,
{ value: cacheA },
React.createElement(ReactHookTest, {
useHook: useCache,
results,
}),
),
);
strictEqual(results.length, 1);
ok("returned" in results[0]);
strictEqual(results[0].returned, cacheA);
const cacheB = new Cache();
ReactTestRenderer.act(() => {
testRenderer.update(
React.createElement(
CacheContext.Provider,
{ value: cacheB },
React.createElement(ReactHookTest, {
useHook: useCache,
results,
}),
),
);
});
strictEqual(results.length, 2);
ok("returned" in results[1]);
strictEqual(results[1].returned, cacheB);
});
});