Skip to content

Commit 8569139

Browse files
committed
Add a StrictMode test
1 parent 9bcda65 commit 8569139

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

package-lock.json

Lines changed: 27 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
"jest-fetch-mock": "3.0.3",
122122
"jest-junit": "13.2.0",
123123
"lodash": "4.17.21",
124-
"react": "17.0.2",
125-
"react-dom": "17.0.2",
124+
"react": "^18.1.0",
125+
"react-dom": "^18.1.0",
126126
"recast": "0.21.1",
127127
"resolve": "1.22.0",
128128
"rimraf": "3.0.2",

src/react/hooks/__tests__/useSubscription.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,45 @@ describe('useSubscription Hook', () => {
504504
);
505505
errorSpy.mockRestore();
506506
});
507+
508+
it('should handle a subscription properly in StrictMode', async () => {
509+
const subscription = gql`
510+
subscription {
511+
car {
512+
make
513+
}
514+
}
515+
`;
516+
517+
const results = ['Audi', 'BMW'].map((make) => ({
518+
result: { data: { car: { make } } },
519+
}));
520+
521+
const link = new MockSubscriptionLink();
522+
const client = new ApolloClient({
523+
link,
524+
cache: new Cache({ addTypename: false }),
525+
});
526+
527+
const { result, waitForNextUpdate } = renderHook(() => useSubscription(subscription), {
528+
wrapper: ({ children }) => (
529+
<React.StrictMode>
530+
<ApolloProvider client={client}>{children}</ApolloProvider>
531+
</React.StrictMode>
532+
),
533+
});
534+
535+
expect(result.current.loading).toBe(true);
536+
expect(result.current.error).toBe(undefined);
537+
expect(result.current.data).toBe(undefined);
538+
setTimeout(() => link.simulateResult(results[0]));
539+
await waitForNextUpdate();
540+
expect(result.current.loading).toBe(false);
541+
expect(result.current.data).toEqual(results[0].result.data);
542+
setTimeout(() => link.simulateResult(results[1]));
543+
await waitForNextUpdate();
544+
expect(result.current.loading).toBe(false);
545+
expect(result.current.data).toEqual(results[1].result.data);
546+
setTimeout(() => link.simulateResult(results[2]));
547+
});
507548
});

0 commit comments

Comments
 (0)