|
1 | 1 | import { describe, expect, test, beforeEach } from "vitest";
|
2 | 2 | import { useConnectQuery } from "./useConnectQuery";
|
3 |
| -import { renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import { renderHook, waitFor, act } from "@testing-library/react"; |
4 | 4 | import {
|
5 | 5 | listMoviesRef,
|
6 | 6 | createMovie,
|
@@ -53,16 +53,34 @@ describe("useConnectQuery", () => {
|
53 | 53 | wrapper,
|
54 | 54 | });
|
55 | 55 |
|
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 | + }); |
57 | 63 |
|
58 |
| - result.current.refetch(); |
| 64 | + const initialFetchTime = result.current.data?.fetchTime; |
59 | 65 |
|
60 |
| - await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 66 | + await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 seconds delay before refetching |
61 | 67 |
|
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; |
66 | 84 | });
|
67 | 85 |
|
68 | 86 | test("returns correct data", async () => {
|
|
0 commit comments