Skip to content

Commit

Permalink
Add a StrictMode test
Browse files Browse the repository at this point in the history
  • Loading branch information
kazekyo committed May 13, 2022
1 parent 9bcda65 commit 8569139
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 35 deletions.
60 changes: 27 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
"jest-fetch-mock": "3.0.3",
"jest-junit": "13.2.0",
"lodash": "4.17.21",
"react": "17.0.2",
"react-dom": "17.0.2",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"recast": "0.21.1",
"resolve": "1.22.0",
"rimraf": "3.0.2",
Expand Down
41 changes: 41 additions & 0 deletions src/react/hooks/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,45 @@ describe('useSubscription Hook', () => {
);
errorSpy.mockRestore();
});

it('should handle a subscription properly in StrictMode', async () => {
const subscription = gql`
subscription {
car {
make
}
}
`;

const results = ['Audi', 'BMW'].map((make) => ({
result: { data: { car: { make } } },
}));

const link = new MockSubscriptionLink();
const client = new ApolloClient({
link,
cache: new Cache({ addTypename: false }),
});

const { result, waitForNextUpdate } = renderHook(() => useSubscription(subscription), {
wrapper: ({ children }) => (
<React.StrictMode>
<ApolloProvider client={client}>{children}</ApolloProvider>
</React.StrictMode>
),
});

expect(result.current.loading).toBe(true);
expect(result.current.error).toBe(undefined);
expect(result.current.data).toBe(undefined);
setTimeout(() => link.simulateResult(results[0]));
await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual(results[0].result.data);
setTimeout(() => link.simulateResult(results[1]));
await waitForNextUpdate();
expect(result.current.loading).toBe(false);
expect(result.current.data).toEqual(results[1].result.data);
setTimeout(() => link.simulateResult(results[2]));
});
});

0 comments on commit 8569139

Please sign in to comment.