test(react-debug-tools): Improve coverage of currentDispatcher.current setter (#24945)
Sebastian Silbermann committed
Jul 21, 2022 at 21:46 UTC
9bd0dd4c181a5be8b6c62cc5d90a727570f08b9c
1 file changed
+12
-32
packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js
+12
-32
@@ -256,12 +256,11 @@ describe('ReactHooksInspection', () => {
256
});
257
258
it('should support an injected dispatcher', () => {
259
- function Foo(props) {
260
- const [state] = React.useState('hello world');
261
- return <div>{state}</div>;
262
- }
263
-
264
- const initial = {};
259
+ const initial = {
260
+ useState() {
261
+ throw new Error("Should've been proxied");
262
+ },
263
+ };
264
let current = initial;
265
let getterCalls = 0;
266
const setterCalls = [];
@@ -276,33 +275,14 @@ describe('ReactHooksInspection', () => {
275
},
276
};
277
279
- let didCatch = false;
280
- expect(() => {
281
- // mock the Error constructor to check the internal of the error instance
282
- try {
283
- ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);
284
- } catch (error) {
285
- expect(error.message).toBe('Error rendering inspected component');
286
- // error.cause is the original error
287
- expect(error.cause).toBeInstanceOf(Error);
288
- expect(error.cause.message).toBe(
289
- "Cannot read property 'useState' of null",
290
- );
291
- }
292
- didCatch = true;
293
- }).toErrorDev(
294
- 'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
295
- ' one of the following reasons:\n' +
296
- '1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
297
- '2. You might be breaking the Rules of Hooks\n' +
298
- '3. You might have more than one copy of React in the same app\n' +
299
- 'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
300
- {withoutStack: true},
301
- );
302
- // avoid false positive if no error was thrown at all
303
- expect(didCatch).toBe(true);
278
+ function Foo(props) {
279
+ const [state] = FakeDispatcherRef.current.useState('hello world');
280
+ return <div>{state}</div>;
281
+ }
282
+
283
+ ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);
284
305
- expect(getterCalls).toBe(1);
285
+ expect(getterCalls).toBe(2);
286
expect(setterCalls).toHaveLength(2);
287
expect(setterCalls[0]).not.toBe(initial);
288
expect(setterCalls[1]).toBe(initial);