-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProvider.test.mjs
108 lines (97 loc) · 3.23 KB
/
Provider.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
// @ts-check
import { strictEqual, throws } from "node:assert";
import React from "react";
import ReactTestRenderer from "react-test-renderer";
import Cache from "./Cache.mjs";
import CacheContext from "./CacheContext.mjs";
import HydrationTimeStampContext from "./HydrationTimeStampContext.mjs";
import Loading from "./Loading.mjs";
import LoadingContext from "./LoadingContext.mjs";
import Provider from "./Provider.mjs";
import assertBundleSize from "./test/assertBundleSize.mjs";
import assertInstanceOf from "./test/assertInstanceOf.mjs";
import assertTypeOf from "./test/assertTypeOf.mjs";
import createReactTestRenderer from "./test/createReactTestRenderer.mjs";
import suppressReactRenderErrorConsoleOutput from "./test/suppressReactRenderErrorConsoleOutput.mjs";
/**
* Adds `Provider` tests.
* @param {import("test-director").default} tests Test director.
*/
export default (tests) => {
tests.add("`Provider` bundle size.", async () => {
await assertBundleSize(new URL("./Provider.mjs", import.meta.url), 500);
});
tests.add("`Provider` with prop `cache` missing.", () => {
const revertConsole = suppressReactRenderErrorConsoleOutput();
try {
throws(() => {
createReactTestRenderer(
React.createElement(
Provider,
// @ts-expect-error Testing invalid.
{}
)
);
}, new TypeError("Prop `cache` must be a `Cache` instance."));
} finally {
revertConsole();
}
});
tests.add("`Provider` used correctly.", () => {
/**
* @type {Array<{
* hydrationTimeStampContextValue: number | undefined,
* cacheContextValue: Cache | undefined,
* loadingContextValue: Loading | undefined
* }>}
*/
const results = [];
function TestComponent() {
results.push({
hydrationTimeStampContextValue: React.useContext(
HydrationTimeStampContext
),
cacheContextValue: React.useContext(CacheContext),
loadingContextValue: React.useContext(LoadingContext),
});
return null;
}
const cache = new Cache();
const testRenderer = createReactTestRenderer(
React.createElement(
Provider,
{ cache },
React.createElement(TestComponent)
)
);
strictEqual(results.length, 1);
assertTypeOf(results[0].hydrationTimeStampContextValue, "number");
strictEqual(
performance.now() - results[0].hydrationTimeStampContextValue < 100,
true
);
strictEqual(results[0].cacheContextValue, cache);
assertInstanceOf(results[0].loadingContextValue, Loading);
ReactTestRenderer.act(() => {
testRenderer.update(
React.createElement(
Provider,
{
// @ts-ignore Force the component to re-render by setting a new
// arbitrary prop.
a: true,
cache,
},
React.createElement(TestComponent)
)
);
});
strictEqual(results.length, 2);
strictEqual(
results[1].hydrationTimeStampContextValue,
results[0].hydrationTimeStampContextValue
);
strictEqual(results[1].cacheContextValue, results[0].cacheContextValue);
strictEqual(results[1].loadingContextValue, results[0].loadingContextValue);
});
};