Skip to content

Commit

Permalink
add test for unmounting in StrictMode
Browse files Browse the repository at this point in the history
  • Loading branch information
brainkim committed Jun 22, 2021
1 parent 9e99bd1 commit 90b5aeb
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,60 @@ describe('useQuery Hook', () => {
}).then(resolve, reject);
});

itAsync('should stop polling when the component is unmounted when using StrictMode', async (resolve, reject) => {
const mocks = [
...CAR_MOCKS,
...CAR_MOCKS,
...CAR_MOCKS,
...CAR_MOCKS,
];

const mockLink = new MockLink(mocks).setOnError(reject);

const linkRequestSpy = jest.spyOn(mockLink, 'request');

let renderCount = 0;
const QueryComponent = ({ unmount }: { unmount: () => void }) => {
const { data, loading } = useQuery(CAR_QUERY, { pollInterval: 10 });
switch (++renderCount) {
case 1:
case 2:
expect(loading).toBeTruthy();
break;
case 3:
case 4:
expect(loading).toBeFalsy();
expect(data).toEqual(CAR_RESULT_DATA);
expect(linkRequestSpy).toHaveBeenCalledTimes(1);
if (renderCount === 3) {
unmount();
}
break;
default:
reject("unreached");
}
return null;
};

const Component = () => {
const [queryMounted, setQueryMounted] = useState(true);
const unmount = () => setTimeout(() => setQueryMounted(false), 0);
return <>{queryMounted && <QueryComponent unmount={unmount} />}</>;
};

render(
<React.StrictMode>
<MockedProvider mocks={CAR_MOCKS} link={mockLink}>
<Component />
</MockedProvider>
</React.StrictMode>
);

return wait(() => {
expect(linkRequestSpy).toHaveBeenCalledTimes(1);
}).then(resolve, reject);
});

itAsync(
'should not throw an error if `stopPolling` is called manually after ' +
'a component has unmounted (even though polling has already been ' +
Expand Down

0 comments on commit 90b5aeb

Please sign in to comment.