Skip to content

Commit

Permalink
test(react-debug-tools): Improve coverage of currentDispatcher.curren…
Browse files Browse the repository at this point in the history
…t setter (#24945)
  • Loading branch information
eps1lon committed Jul 21, 2022
1 parent 59bc52a commit 9bd0dd4
Showing 1 changed file with 12 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,11 @@ describe('ReactHooksInspection', () => {
});

it('should support an injected dispatcher', () => {
function Foo(props) {
const [state] = React.useState('hello world');
return <div>{state}</div>;
}

const initial = {};
const initial = {
useState() {
throw new Error("Should've been proxied");
},
};
let current = initial;
let getterCalls = 0;
const setterCalls = [];
Expand All @@ -276,33 +275,14 @@ describe('ReactHooksInspection', () => {
},
};

let didCatch = false;
expect(() => {
// mock the Error constructor to check the internal of the error instance
try {
ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);
} catch (error) {
expect(error.message).toBe('Error rendering inspected component');
// error.cause is the original error
expect(error.cause).toBeInstanceOf(Error);
expect(error.cause.message).toBe(
"Cannot read property 'useState' of null",
);
}
didCatch = true;
}).toErrorDev(
'Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for' +
' one of the following reasons:\n' +
'1. You might have mismatching versions of React and the renderer (such as React DOM)\n' +
'2. You might be breaking the Rules of Hooks\n' +
'3. You might have more than one copy of React in the same app\n' +
'See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.',
{withoutStack: true},
);
// avoid false positive if no error was thrown at all
expect(didCatch).toBe(true);
function Foo(props) {
const [state] = FakeDispatcherRef.current.useState('hello world');
return <div>{state}</div>;
}

ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef);

expect(getterCalls).toBe(1);
expect(getterCalls).toBe(2);
expect(setterCalls).toHaveLength(2);
expect(setterCalls[0]).not.toBe(initial);
expect(setterCalls[1]).toBe(initial);
Expand Down

0 comments on commit 9bd0dd4

Please sign in to comment.