|
| 1 | +import { renderHook } from "@testing-library/react" |
| 2 | +import { useQuery } from "@tanstack/react-query" |
| 3 | +import { usePrefetchWarnings } from "./usePrefetchWarnings" |
| 4 | +import { setupReactQueryTest } from "../hooks/test-utils" |
| 5 | +import { urls, factories, setMockResponse } from "../test-utils" |
| 6 | +import { |
| 7 | + learningResourcesKeyFactory, |
| 8 | + useLearningResourcesDetail, |
| 9 | +} from "../hooks/learningResources" |
| 10 | + |
| 11 | +jest.mock("./usePrefetchWarnings", () => { |
| 12 | + const originalModule = jest.requireActual("./usePrefetchWarnings") |
| 13 | + return { |
| 14 | + ...originalModule, |
| 15 | + logQueries: jest.fn(), |
| 16 | + } |
| 17 | +}) |
| 18 | + |
| 19 | +describe("SSR prefetch warnings", () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.spyOn(console, "info").mockImplementation(() => {}) |
| 22 | + jest.spyOn(console, "table").mockImplementation(() => {}) |
| 23 | + }) |
| 24 | + |
| 25 | + it("Warns if a query is requested on the client that has not been prefetched", async () => { |
| 26 | + const { wrapper, queryClient } = setupReactQueryTest() |
| 27 | + |
| 28 | + const data = factories.learningResources.resource() |
| 29 | + setMockResponse.get(urls.learningResources.details({ id: 1 }), data) |
| 30 | + |
| 31 | + renderHook(() => useLearningResourcesDetail(1), { wrapper }) |
| 32 | + |
| 33 | + renderHook(usePrefetchWarnings, { |
| 34 | + wrapper, |
| 35 | + initialProps: { queryClient }, |
| 36 | + }) |
| 37 | + |
| 38 | + expect(console.info).toHaveBeenCalledWith( |
| 39 | + "The following queries were requested in first render but not prefetched.", |
| 40 | + "If these queries are user-specific, they cannot be prefetched - responses are cached on public CDN.", |
| 41 | + "Otherwise, consider fetching on the server with prefetch:", |
| 42 | + ) |
| 43 | + expect(console.table).toHaveBeenCalledWith( |
| 44 | + [ |
| 45 | + expect.objectContaining({ |
| 46 | + disabled: false, |
| 47 | + initialStatus: "loading", |
| 48 | + key: learningResourcesKeyFactory.detail(1).queryKey, |
| 49 | + observerCount: 1, |
| 50 | + }), |
| 51 | + ], |
| 52 | + ["hash", "initialStatus", "status", "observerCount", "disabled"], |
| 53 | + ) |
| 54 | + }) |
| 55 | + |
| 56 | + it("Ignores exempted queries requested on the client that have not been prefetched", async () => { |
| 57 | + const { wrapper, queryClient } = setupReactQueryTest() |
| 58 | + |
| 59 | + const data = factories.learningResources.resource() |
| 60 | + setMockResponse.get(urls.learningResources.details({ id: 1 }), data) |
| 61 | + |
| 62 | + renderHook(() => useLearningResourcesDetail(1), { wrapper }) |
| 63 | + |
| 64 | + renderHook(usePrefetchWarnings, { |
| 65 | + wrapper, |
| 66 | + initialProps: { |
| 67 | + queryClient, |
| 68 | + exemptions: [learningResourcesKeyFactory.detail(1).queryKey], |
| 69 | + }, |
| 70 | + }) |
| 71 | + |
| 72 | + expect(console.info).not.toHaveBeenCalled() |
| 73 | + expect(console.table).not.toHaveBeenCalled() |
| 74 | + }) |
| 75 | + |
| 76 | + it("Warns for queries prefetched on the server but not requested on the client", async () => { |
| 77 | + const { wrapper, queryClient } = setupReactQueryTest() |
| 78 | + |
| 79 | + const data = factories.learningResources.resource() |
| 80 | + setMockResponse.get(urls.learningResources.details({ id: 1 }), data) |
| 81 | + |
| 82 | + // Emulate server prefetch |
| 83 | + const { unmount } = renderHook( |
| 84 | + () => |
| 85 | + useQuery({ |
| 86 | + ...learningResourcesKeyFactory.detail(1), |
| 87 | + initialData: data, |
| 88 | + }), |
| 89 | + { wrapper }, |
| 90 | + ) |
| 91 | + |
| 92 | + // Removes observer |
| 93 | + unmount() |
| 94 | + |
| 95 | + renderHook(usePrefetchWarnings, { |
| 96 | + wrapper, |
| 97 | + initialProps: { queryClient }, |
| 98 | + }) |
| 99 | + |
| 100 | + expect(console.info).toHaveBeenCalledWith( |
| 101 | + "The following queries were prefetched on the server but not accessed during initial render.", |
| 102 | + "If these queries are no longer in use they should removed from prefetch:", |
| 103 | + ) |
| 104 | + expect(console.table).toHaveBeenCalledWith( |
| 105 | + [ |
| 106 | + { |
| 107 | + disabled: false, |
| 108 | + hash: JSON.stringify(learningResourcesKeyFactory.detail(1).queryKey), |
| 109 | + initialStatus: "success", |
| 110 | + key: learningResourcesKeyFactory.detail(1).queryKey, |
| 111 | + observerCount: 0, |
| 112 | + status: "success", |
| 113 | + }, |
| 114 | + ], |
| 115 | + ["hash", "initialStatus", "status", "observerCount", "disabled"], |
| 116 | + ) |
| 117 | + }) |
| 118 | +}) |
0 commit comments