-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseServerContext.test.mjs
88 lines (71 loc) · 2.4 KB
/
useServerContext.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
// @ts-check
import { strictEqual } from "node:assert";
import { describe, it } from "node:test";
import React from "react";
import ReactTestRenderer from "react-test-renderer";
import ServerContextContext from "./ServerContextContext.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import ReactHookTest from "./test/ReactHookTest.mjs";
import useServerContext from "./useServerContext.mjs";
describe("Function `useServerContext`.", { concurrency: true }, () => {
it("Bundle size.", async () => {
await assertBundleSize(
new URL("./useServerContext.mjs", import.meta.url),
150,
);
});
it("Server context context missing.", () => {
/** @type {Array<unknown>} */
const results = [];
ReactTestRenderer.act(() => {
ReactTestRenderer.create(
React.createElement(ReactHookTest, { hook: useServerContext, results }),
);
});
strictEqual(results.length, 1);
strictEqual(results[0], undefined);
});
it("Getting the server context.", () => {
/** @type {Array<unknown>} */
const results = [];
/** @type {import("react-test-renderer").ReactTestRenderer | undefined} */
let testRenderer;
const serverContextA =
/** @type {import("./ServerContextContext.mjs").ServerContext} */ ({});
ReactTestRenderer.act(() => {
testRenderer = ReactTestRenderer.create(
React.createElement(
ServerContextContext.Provider,
{ value: serverContextA },
React.createElement(ReactHookTest, {
hook: useServerContext,
results,
}),
),
);
});
strictEqual(results.length, 1);
strictEqual(results[0], serverContextA);
const tr =
/** @type {import("react-test-renderer").ReactTestRenderer} */
(testRenderer);
// The server context shouldn’t change, but it’s good to test the normal
// React context and hooks behavior anyway.
const serverContextB =
/** @type {import("./ServerContextContext.mjs").ServerContext} */ ({});
ReactTestRenderer.act(() => {
tr.update(
React.createElement(
ServerContextContext.Provider,
{ value: serverContextB },
React.createElement(ReactHookTest, {
hook: useServerContext,
results,
}),
),
);
});
strictEqual(results.length, 2);
strictEqual(results[1], serverContextB);
});
});