Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(react-debug-tools): Improve coverage of currentDispatcher.current setter #24945

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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