Skip to content

Commit 3ec1243

Browse files
committed
test(useConnectQuery): test for data refetching
1 parent d860178 commit 3ec1243

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

packages/react/src/data-connect/useConnectQuery.test.tsx

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test, beforeEach } from "vitest";
22
import { useConnectQuery } from "./useConnectQuery";
3-
import { renderHook, waitFor } from "@testing-library/react";
3+
import { renderHook, waitFor, act } from "@testing-library/react";
44
import {
55
listMoviesRef,
66
createMovie,
@@ -53,16 +53,34 @@ describe("useConnectQuery", () => {
5353
wrapper,
5454
});
5555

56-
await waitFor(() => expect(result.current.isSuccess).toBe(true));
56+
await waitFor(() => {
57+
expect(result.current.isSuccess).toBe(true);
58+
expect(result.current.data).toBeDefined();
59+
expect(result.current.data).toHaveProperty("ref");
60+
expect(result.current.data).toHaveProperty("source");
61+
expect(result.current.data).toHaveProperty("fetchTime");
62+
});
5763

58-
result.current.refetch();
64+
const initialFetchTime = result.current.data?.fetchTime;
5965

60-
await waitFor(() => expect(result.current.isSuccess).toBe(true));
66+
await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 seconds delay before refetching
6167

62-
expect(result.current.data).toBeDefined();
63-
expect(result.current.data).toHaveProperty("movies");
64-
expect(Array.isArray(result.current.data?.movies)).toBe(true);
65-
expect(result.current.data?.movies.length).toBeGreaterThanOrEqual(0);
68+
await act(async () => {
69+
result.current.refetch();
70+
});
71+
72+
await waitFor(() => {
73+
expect(result.current.isSuccess).toBe(true);
74+
expect(result.current.data).toBeDefined();
75+
expect(result.current.data).toHaveProperty("ref");
76+
expect(result.current.data).toHaveProperty("source");
77+
expect(result.current.data).toHaveProperty("fetchTime");
78+
expect(result.current.data).toHaveProperty("movies");
79+
expect(Array.isArray(result.current.data?.movies)).toBe(true);
80+
expect(result.current.data?.movies.length).toBeGreaterThanOrEqual(0);
81+
});
82+
83+
const refetchTime = result.current.data?.fetchTime;
6684
});
6785

6886
test("returns correct data", async () => {

0 commit comments

Comments
 (0)