Skip to content

Commit 65c0197

Browse files
committed
refactor the multiple useQuery calls per component tests
1 parent 2dbd196 commit 65c0197

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

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

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,114 +2606,97 @@ describe('useQuery Hook', () => {
26062606
switch (operation.operationName) {
26072607
case "A":
26082608
observer.next({ data: aData });
2609+
observer.complete();
26092610
break;
26102611
case "B":
2611-
observer.next({ data: bData });
2612+
setTimeout(() => {
2613+
observer.next({ data: bData });
2614+
observer.complete();
2615+
}, 10);
26122616
break;
26132617
}
2614-
observer.complete();
26152618
})),
26162619
});
26172620
}
26182621

2619-
function check(
2622+
async function check(
26202623
aFetchPolicy: WatchQueryFetchPolicy,
26212624
bFetchPolicy: WatchQueryFetchPolicy,
26222625
) {
2623-
return (
2624-
resolve: (result: any) => any,
2625-
reject: (reason: any) => any,
2626-
) => {
2627-
let renderCount = 0;
2628-
2629-
function App() {
2630-
const a = useQuery(aQuery, {
2631-
fetchPolicy: aFetchPolicy,
2632-
});
2626+
const client = makeClient();
2627+
const { result, waitForNextUpdate } = renderHook(
2628+
() => ({
2629+
a: useQuery(aQuery, { fetchPolicy: aFetchPolicy }),
2630+
b: useQuery(bQuery, { fetchPolicy: bFetchPolicy }),
2631+
}),
2632+
{
2633+
wrapper: ({ children }) => (
2634+
<ApolloProvider client={client}>{children}</ApolloProvider>
2635+
),
2636+
},
2637+
);
26332638

2634-
const b = useQuery(bQuery, {
2635-
fetchPolicy: bFetchPolicy,
2636-
});
2639+
expect(result.current.a.loading).toBe(true);
2640+
expect(result.current.b.loading).toBe(true);
2641+
expect(result.current.a.data).toBe(undefined);
2642+
expect(result.current.b.data).toBe(undefined);
26372643

2638-
switch (++renderCount) {
2639-
case 1:
2640-
expect(a.loading).toBe(true);
2641-
expect(b.loading).toBe(true);
2642-
expect(a.data).toBeUndefined();
2643-
expect(b.data).toBeUndefined();
2644-
break;
2645-
case 2:
2646-
expect(a.loading).toBe(false);
2647-
expect(b.loading).toBe(true);
2648-
expect(a.data).toEqual(aData);
2649-
expect(b.data).toBeUndefined();
2650-
break;
2651-
case 3:
2652-
expect(a.loading).toBe(false);
2653-
expect(b.loading).toBe(false);
2654-
expect(a.data).toEqual(aData);
2655-
expect(b.data).toEqual(bData);
2656-
break;
2657-
default:
2658-
reject("too many renders: " + renderCount);
2659-
}
2660-
2661-
return null;
2662-
}
2644+
await waitForNextUpdate();
2645+
expect(result.current.a.loading).toBe(false);
2646+
expect(result.current.b.loading).toBe(true);
2647+
expect(result.current.a.data).toEqual(aData);
2648+
expect(result.current.b.data).toBe(undefined);
26632649

2664-
render(
2665-
<ApolloProvider client={makeClient()}>
2666-
<App/>
2667-
</ApolloProvider>
2668-
);
2650+
await waitForNextUpdate();
26692651

2670-
return wait(() => {
2671-
expect(renderCount).toBe(3);
2672-
}).then(resolve, reject);
2673-
};
2652+
expect(result.current.a.loading).toBe(false);
2653+
expect(result.current.b.loading).toBe(false);
2654+
expect(result.current.a.data).toEqual(aData);
2655+
expect(result.current.b.data).toEqual(bData);
2656+
await expect(waitForNextUpdate({ timeout: 20 })).rejects.toThrow('Timed out');
26742657
}
26752658

2676-
itAsync("cache-first for both", check(
2659+
it("cache-first for both", () => check(
26772660
"cache-first",
26782661
"cache-first",
26792662
));
26802663

2681-
itAsync("cache-first first, cache-and-network second", check(
2664+
it("cache-first first, cache-and-network second", () => check(
26822665
"cache-first",
26832666
"cache-and-network",
26842667
));
26852668

2686-
itAsync("cache-first first, network-only second", check(
2669+
it("cache-first first, network-only second", () => check(
26872670
"cache-first",
26882671
"network-only",
26892672
));
26902673

2691-
itAsync("cache-and-network for both", check(
2674+
it("cache-and-network for both", () => check(
26922675
"cache-and-network",
26932676
"cache-and-network",
26942677
));
26952678

2696-
itAsync("cache-and-network first, cache-first second", check(
2679+
it("cache-and-network first, cache-first second", () => check(
26972680
"cache-and-network",
26982681
"cache-first",
26992682
));
27002683

2701-
itAsync("cache-and-network first, network-only second", check(
2684+
it("cache-and-network first, network-only second", () => check(
27022685
"cache-and-network",
27032686
"network-only",
27042687
));
27052688

2706-
itAsync("network-only for both", check(
2689+
it("network-only for both", () => check(
27072690
"network-only",
27082691
"network-only",
27092692
));
27102693

2711-
itAsync("network-only first, cache-first second", check(
2694+
it("network-only first, cache-first second", () => check(
27122695
"network-only",
27132696
"cache-first",
27142697
));
27152698

2716-
itAsync("network-only first, cache-and-network second", check(
2699+
it("network-only first, cache-and-network second", () => check(
27172700
"network-only",
27182701
"cache-and-network",
27192702
));

0 commit comments

Comments
 (0)