Skip to content

Commit 9bd0dd4

Browse files
authored
test(react-debug-tools): Improve coverage of currentDispatcher.current setter (#24945)
1 parent 59bc52a commit 9bd0dd4

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,11 @@ describe('ReactHooksInspection', () => {
256256
});
257257

258258
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+
};
265264
let current = initial;
266265
let getterCalls = 0;
267266
const setterCalls = [];
@@ -276,33 +275,14 @@ describe('ReactHooksInspection', () => {
276275
},
277276
};
278277

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);
304284

305-
expect(getterCalls).toBe(1);
285+
expect(getterCalls).toBe(2);
306286
expect(setterCalls).toHaveLength(2);
307287
expect(setterCalls[0]).not.toBe(initial);
308288
expect(setterCalls[1]).toBe(initial);

0 commit comments

Comments
 (0)