|
1 | 1 | // @ts-check |
2 | 2 |
|
3 | | -import { cleanup, renderHook } from "@testing-library/react-hooks/lib/pure.js"; |
4 | | -import { notStrictEqual, strictEqual, throws } from "assert"; |
| 3 | +import { notStrictEqual, ok, strictEqual, throws } from "assert"; |
5 | 4 | import React from "react"; |
| 5 | +import ReactTestRenderer from "react-test-renderer"; |
6 | 6 |
|
7 | 7 | import Cache from "./Cache.mjs"; |
8 | 8 | import CacheContext from "./CacheContext.mjs"; |
9 | 9 | import Loading from "./Loading.mjs"; |
10 | 10 | import LoadingCacheValue from "./LoadingCacheValue.mjs"; |
11 | 11 | import assertBundleSize from "./test/assertBundleSize.mjs"; |
12 | 12 | import assertTypeOf from "./test/assertTypeOf.mjs"; |
| 13 | +import createReactTestRenderer from "./test/createReactTestRenderer.mjs"; |
| 14 | +import ReactHookTest from "./test/ReactHookTest.mjs"; |
13 | 15 | import useAutoAbortLoad from "./useAutoAbortLoad.mjs"; |
14 | 16 |
|
15 | 17 | /** |
@@ -84,95 +86,104 @@ export default (tests) => { |
84 | 86 | return loadingCacheValue; |
85 | 87 | } |
86 | 88 |
|
87 | | - /** @param {{ children?: React.ReactNode }} props Props. */ |
88 | | - const wrapper = ({ children }) => |
89 | | - React.createElement(CacheContext.Provider, { value: cache }, children); |
90 | | - |
91 | | - try { |
92 | | - const { result, rerender, unmount } = renderHook( |
93 | | - ({ load }) => useAutoAbortLoad(load), |
94 | | - { |
95 | | - wrapper, |
96 | | - initialProps: { |
97 | | - load: loadA, |
98 | | - }, |
99 | | - } |
100 | | - ); |
| 89 | + /** @type {Array<import("./test/ReactHookTest.mjs").ReactHookResult>} */ |
| 90 | + const results = []; |
| 91 | + |
| 92 | + const testRenderer = createReactTestRenderer( |
| 93 | + React.createElement( |
| 94 | + CacheContext.Provider, |
| 95 | + { value: cache }, |
| 96 | + React.createElement(ReactHookTest, { |
| 97 | + useHook: () => useAutoAbortLoad(loadA), |
| 98 | + results, |
| 99 | + }) |
| 100 | + ) |
| 101 | + ); |
101 | 102 |
|
102 | | - strictEqual(result.all.length, 1); |
103 | | - assertTypeOf(result.current, "function"); |
104 | | - strictEqual(result.error, undefined); |
105 | | - strictEqual(loadCalls.length, 0); |
| 103 | + strictEqual(results.length, 1); |
| 104 | + ok("returned" in results[0]); |
| 105 | + assertTypeOf(results[0].returned, "function"); |
| 106 | + strictEqual(loadCalls.length, 0); |
106 | 107 |
|
107 | | - // Test that the returned auto abort load function is memoized. |
108 | | - rerender(); |
| 108 | + // Test that the returned auto abort load function is memoized. |
| 109 | + ReactTestRenderer.act(() => { |
| 110 | + results[0].rerender(); |
| 111 | + }); |
109 | 112 |
|
110 | | - strictEqual(result.all.length, 2); |
111 | | - strictEqual(result.current, result.all[0]); |
112 | | - strictEqual(result.error, undefined); |
113 | | - strictEqual(loadCalls.length, 0); |
| 113 | + strictEqual(results.length, 2); |
| 114 | + strictEqual(loadCalls.length, 0); |
114 | 115 |
|
115 | | - // Start the first loading. |
116 | | - result.current(); |
| 116 | + // Start the first loading. |
| 117 | + results[0].returned(); |
117 | 118 |
|
118 | | - strictEqual(loadCalls.length, 1); |
119 | | - strictEqual(loadCalls[0].loader, loadA); |
120 | | - strictEqual(loadCalls[0].hadArgs, false); |
121 | | - strictEqual( |
122 | | - loadCalls[0].loadingCacheValue.abortController.signal.aborted, |
123 | | - false |
124 | | - ); |
| 119 | + strictEqual(loadCalls.length, 1); |
| 120 | + strictEqual(loadCalls[0].loader, loadA); |
| 121 | + strictEqual(loadCalls[0].hadArgs, false); |
| 122 | + strictEqual( |
| 123 | + loadCalls[0].loadingCacheValue.abortController.signal.aborted, |
| 124 | + false |
| 125 | + ); |
125 | 126 |
|
126 | | - // Start the second loading, before the first ends. This should abort the |
127 | | - // first. |
128 | | - result.current(); |
| 127 | + // Start the second loading, before the first ends. This should abort the |
| 128 | + // first. |
| 129 | + results[0].returned(); |
129 | 130 |
|
130 | | - strictEqual(loadCalls.length, 2); |
131 | | - strictEqual( |
132 | | - loadCalls[0].loadingCacheValue.abortController.signal.aborted, |
133 | | - true |
134 | | - ); |
135 | | - strictEqual(loadCalls[1].hadArgs, false); |
136 | | - strictEqual(loadCalls[1].loader, loadA); |
137 | | - strictEqual( |
138 | | - loadCalls[1].loadingCacheValue.abortController.signal.aborted, |
139 | | - false |
140 | | - ); |
| 131 | + strictEqual(loadCalls.length, 2); |
| 132 | + strictEqual( |
| 133 | + loadCalls[0].loadingCacheValue.abortController.signal.aborted, |
| 134 | + true |
| 135 | + ); |
| 136 | + strictEqual(loadCalls[1].hadArgs, false); |
| 137 | + strictEqual(loadCalls[1].loader, loadA); |
| 138 | + strictEqual( |
| 139 | + loadCalls[1].loadingCacheValue.abortController.signal.aborted, |
| 140 | + false |
| 141 | + ); |
141 | 142 |
|
142 | | - // Test that changing the loader causes the returned memoized auto abort |
143 | | - // load function to change, and the last loading to abort. |
144 | | - rerender({ load: loadB }); |
145 | | - |
146 | | - strictEqual(result.all.length, 3); |
147 | | - assertTypeOf(result.current, "function"); |
148 | | - notStrictEqual(result.current, result.all[1]); |
149 | | - strictEqual(result.error, undefined); |
150 | | - strictEqual(loadCalls.length, 2); |
151 | | - strictEqual( |
152 | | - loadCalls[1].loadingCacheValue.abortController.signal.aborted, |
153 | | - true |
| 143 | + // Test that changing the loader causes the returned memoized auto abort |
| 144 | + // load function to change, and the last loading to abort. |
| 145 | + ReactTestRenderer.act(() => { |
| 146 | + testRenderer.update( |
| 147 | + React.createElement( |
| 148 | + CacheContext.Provider, |
| 149 | + { value: cache }, |
| 150 | + React.createElement(ReactHookTest, { |
| 151 | + useHook: () => useAutoAbortLoad(loadB), |
| 152 | + results, |
| 153 | + }) |
| 154 | + ) |
154 | 155 | ); |
| 156 | + }); |
| 157 | + |
| 158 | + strictEqual(results.length, 3); |
| 159 | + ok("returned" in results[2]); |
| 160 | + assertTypeOf(results[2].returned, "function"); |
| 161 | + notStrictEqual(results[2].returned, results[1]); |
| 162 | + strictEqual(loadCalls.length, 2); |
| 163 | + strictEqual( |
| 164 | + loadCalls[1].loadingCacheValue.abortController.signal.aborted, |
| 165 | + true |
| 166 | + ); |
155 | 167 |
|
156 | | - // Test that the returned newly memoized abort load function works. |
157 | | - result.current(); |
| 168 | + // Test that the returned newly memoized abort load function works. |
| 169 | + results[2].returned(); |
158 | 170 |
|
159 | | - strictEqual(loadCalls.length, 3); |
160 | | - strictEqual(loadCalls[2].loader, loadB); |
161 | | - strictEqual(loadCalls[2].hadArgs, false); |
162 | | - strictEqual( |
163 | | - loadCalls[2].loadingCacheValue.abortController.signal.aborted, |
164 | | - false |
165 | | - ); |
| 171 | + strictEqual(loadCalls.length, 3); |
| 172 | + strictEqual(loadCalls[2].loader, loadB); |
| 173 | + strictEqual(loadCalls[2].hadArgs, false); |
| 174 | + strictEqual( |
| 175 | + loadCalls[2].loadingCacheValue.abortController.signal.aborted, |
| 176 | + false |
| 177 | + ); |
166 | 178 |
|
167 | | - // Test that the last loading is aborted on unmount. |
168 | | - unmount(); |
| 179 | + // Test that the last loading is aborted on unmount. |
| 180 | + ReactTestRenderer.act(() => { |
| 181 | + testRenderer.unmount(); |
| 182 | + }); |
169 | 183 |
|
170 | | - strictEqual( |
171 | | - loadCalls[2].loadingCacheValue.abortController.signal.aborted, |
172 | | - true |
173 | | - ); |
174 | | - } finally { |
175 | | - cleanup(); |
176 | | - } |
| 184 | + strictEqual( |
| 185 | + loadCalls[2].loadingCacheValue.abortController.signal.aborted, |
| 186 | + true |
| 187 | + ); |
177 | 188 | }); |
178 | 189 | }; |
0 commit comments