-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
useAutoLoad.test.mjs
181 lines (149 loc) · 4.64 KB
/
useAutoLoad.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
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
173
174
175
176
177
178
179
180
181
// @ts-check
/**
* @import { ReactHookResult } from "./test/ReactHookTest.mjs"
* @import { Loader } from "./types.mjs"
*/
import "./test/polyfillCustomEvent.mjs";
import { ok, strictEqual, throws } 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 cacheEntryDelete from "./cacheEntryDelete.mjs";
import cacheEntryPrune from "./cacheEntryPrune.mjs";
import cacheEntryStale from "./cacheEntryStale.mjs";
import Loading from "./Loading.mjs";
import LoadingCacheValue from "./LoadingCacheValue.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertTypeOf from "./test/assertTypeOf.mjs";
import createReactTestRenderer from "./test/createReactTestRenderer.mjs";
import ReactHookTest from "./test/ReactHookTest.mjs";
import useAutoLoad from "./useAutoLoad.mjs";
describe("React hook `useAutoLoad`.", { concurrency: true }, () => {
it("Bundle size.", async () => {
await assertBundleSize(new URL("./useAutoLoad.mjs", import.meta.url), 900);
});
it("Argument 1 `cacheKey` not a string.", () => {
throws(() => {
useAutoLoad(
// @ts-expect-error Testing invalid.
true,
new LoadingCacheValue(
new Loading(),
new Cache(),
"a",
Promise.resolve(),
new AbortController(),
),
);
}, new TypeError("Argument 1 `cacheKey` must be a string."));
});
it("Argument 2 `load` not a function.", () => {
throws(() => {
useAutoLoad(
"a",
// @ts-expect-error Testing invalid.
true,
);
}, new TypeError("Argument 2 `load` must be a function."));
});
it("Functionality.", async () => {
const cacheKey = "a";
const cache = new Cache({
// Populate the cache entry so it can be deleted.
[cacheKey]: 0,
});
const loading = new Loading();
/**
* @type {Array<{
* hadArgs: boolean,
* loadingCacheValue: LoadingCacheValue
* }>}
*/
const loadCalls = [];
/** @type {Loader} */
function load() {
const loadingCacheValue = new LoadingCacheValue(
loading,
cache,
cacheKey,
Promise.resolve(1),
new AbortController(),
);
loadCalls.push({
hadArgs: !!arguments.length,
loadingCacheValue,
});
return loadingCacheValue;
}
// Test load on mount.
/** @type {Array<ReactHookResult>} */
const results = [];
const testRenderer = createReactTestRenderer(
React.createElement(
CacheContext.Provider,
{ value: cache },
React.createElement(ReactHookTest, {
useHook: () => useAutoLoad(cacheKey, load),
results,
}),
),
);
strictEqual(results.length, 1);
ok("returned" in results[0]);
assertTypeOf(results[0].returned, "function");
strictEqual(loadCalls.length, 1);
strictEqual(loadCalls[0].hadArgs, false);
strictEqual(
loadCalls[0].loadingCacheValue.abortController.signal.aborted,
false,
);
// Test that the returned auto abort load function is memoized, and that
// re-rendering doesn’t result in another load.
ReactTestRenderer.act(() => {
results[0].rerender();
});
strictEqual(results.length, 2);
ok("returned" in results[1]);
strictEqual(results[1].returned, results[0].returned);
strictEqual(loadCalls.length, 1);
// Test prune prevention.
cacheEntryPrune(cache, cacheKey);
strictEqual(cacheKey in cache.store, true);
// Test load on stale.
cacheEntryStale(cache, cacheKey);
strictEqual(loadCalls.length, 2);
strictEqual(
loadCalls[0].loadingCacheValue.abortController.signal.aborted,
true,
);
strictEqual(loadCalls[1].hadArgs, false);
strictEqual(
loadCalls[1].loadingCacheValue.abortController.signal.aborted,
false,
);
// Test load on delete.
cacheEntryDelete(cache, cacheKey);
strictEqual(loadCalls.length, 3);
strictEqual(
loadCalls[1].loadingCacheValue.abortController.signal.aborted,
true,
);
strictEqual(loadCalls[2].hadArgs, false);
strictEqual(
loadCalls[2].loadingCacheValue.abortController.signal.aborted,
false,
);
// Nothing should have caused a re-render.
strictEqual(results.length, 2);
// Test that the last loading is aborted on unmount.
ReactTestRenderer.act(() => {
testRenderer.unmount();
});
strictEqual(
loadCalls[2].loadingCacheValue.abortController.signal.aborted,
true,
);
});
});