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