Skip to content

Commit d1558c1

Browse files
authored
test({react,preact}-query/usePrefetchQuery): inline the 'generateQueryFn' factory into each call site (#11080)
* test({react,preact}-query/usePrefetchQuery): inline the 'generateQueryFn' factory into each call site * test({react,preact}-query/usePrefetchQuery): drop the unnecessary 'vi.fn()' wrapper on the never-asserted sentinel queryFn
1 parent 99690d1 commit d1558c1

2 files changed

Lines changed: 30 additions & 28 deletions

File tree

packages/preact-query/src/__tests__/usePrefetchQuery.test.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ import {
1414
import { ErrorBoundary } from './ErrorBoundary'
1515
import { renderWithClient } from './utils'
1616

17-
const generateQueryFn = (data: string) =>
18-
vi
19-
.fn<(...args: Array<any>) => Promise<string>>()
20-
.mockImplementation(() => sleep(10).then(() => data))
21-
2217
describe('usePrefetchQuery', () => {
2318
let queryCache: QueryCache
2419
let queryClient: QueryClient
@@ -37,12 +32,12 @@ describe('usePrefetchQuery', () => {
3732
it('should prefetch query if query state does not exist', async () => {
3833
const queryOpts = {
3934
queryKey: queryKey(),
40-
queryFn: generateQueryFn('prefetchQuery'),
35+
queryFn: vi.fn(() => sleep(10).then(() => 'prefetchQuery')),
4136
}
4237

4338
const componentQueryOpts = {
4439
...queryOpts,
45-
queryFn: generateQueryFn('useSuspenseQuery'),
40+
queryFn: () => sleep(10).then(() => 'useSuspenseQuery'),
4641
}
4742

4843
function Page() {
@@ -72,7 +67,9 @@ describe('usePrefetchQuery', () => {
7267
it('should not prefetch query if query state exists', async () => {
7368
const queryOpts = {
7469
queryKey: queryKey(),
75-
queryFn: generateQueryFn('The usePrefetchQuery hook is smart!'),
70+
queryFn: vi.fn(() =>
71+
sleep(10).then(() => 'The usePrefetchQuery hook is smart!'),
72+
),
7673
}
7774

7875
function Page() {
@@ -105,7 +102,7 @@ describe('usePrefetchQuery', () => {
105102
it('should let errors fall through and not refetch failed queries', async () => {
106103
const consoleMock = vi.spyOn(console, 'error')
107104
consoleMock.mockImplementation(() => undefined)
108-
const queryFn = generateQueryFn('Not an error')
105+
const queryFn = vi.fn(() => sleep(10).then(() => 'Not an error'))
109106

110107
const queryOpts = {
111108
queryKey: queryKey(),
@@ -148,7 +145,7 @@ describe('usePrefetchQuery', () => {
148145
})
149146

150147
it('should not create an endless loop when using inside a suspense boundary', async () => {
151-
const queryFn = generateQueryFn('prefetchedQuery')
148+
const queryFn = vi.fn(() => sleep(10).then(() => 'prefetchedQuery'))
152149

153150
const queryOpts = {
154151
queryKey: queryKey(),
@@ -184,7 +181,9 @@ describe('usePrefetchQuery', () => {
184181
it('should be able to recover from errors and try fetching again', async () => {
185182
const consoleMock = vi.spyOn(console, 'error')
186183
consoleMock.mockImplementation(() => undefined)
187-
const queryFn = generateQueryFn('This is fine :dog: :fire:')
184+
const queryFn = vi.fn(() =>
185+
sleep(10).then(() => 'This is fine :dog: :fire:'),
186+
)
188187

189188
const queryOpts = {
190189
queryKey: queryKey(),
@@ -242,17 +241,19 @@ describe('usePrefetchQuery', () => {
242241
it('should not create a suspense waterfall if prefetch is fired', async () => {
243242
const firstQueryOpts = {
244243
queryKey: queryKey(),
245-
queryFn: generateQueryFn('Prefetch is nice!'),
244+
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is nice!')),
246245
}
247246

248247
const secondQueryOpts = {
249248
queryKey: queryKey(),
250-
queryFn: generateQueryFn('Prefetch is really nice!!'),
249+
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is really nice!!')),
251250
}
252251

253252
const thirdQueryOpts = {
254253
queryKey: queryKey(),
255-
queryFn: generateQueryFn('Prefetch does not create waterfalls!!'),
254+
queryFn: vi.fn(() =>
255+
sleep(10).then(() => 'Prefetch does not create waterfalls!!'),
256+
),
256257
}
257258

258259
const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)

packages/react-query/src/__tests__/usePrefetchQuery.test.tsx

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import {
1212
} from '..'
1313
import { renderWithClient } from './utils'
1414

15-
const generateQueryFn = (data: string) =>
16-
vi
17-
.fn<(...args: Array<any>) => Promise<string>>()
18-
.mockImplementation(() => sleep(10).then(() => data))
19-
2015
describe('usePrefetchQuery', () => {
2116
let queryCache: QueryCache
2217
let queryClient: QueryClient
@@ -35,12 +30,12 @@ describe('usePrefetchQuery', () => {
3530
it('should prefetch query if query state does not exist', async () => {
3631
const queryOpts = {
3732
queryKey: queryKey(),
38-
queryFn: generateQueryFn('prefetchQuery'),
33+
queryFn: vi.fn(() => sleep(10).then(() => 'prefetchQuery')),
3934
}
4035

4136
const componentQueryOpts = {
4237
...queryOpts,
43-
queryFn: generateQueryFn('useSuspenseQuery'),
38+
queryFn: () => sleep(10).then(() => 'useSuspenseQuery'),
4439
}
4540

4641
function Page() {
@@ -70,7 +65,9 @@ describe('usePrefetchQuery', () => {
7065
it('should not prefetch query if query state exists', async () => {
7166
const queryOpts = {
7267
queryKey: queryKey(),
73-
queryFn: generateQueryFn('The usePrefetchQuery hook is smart!'),
68+
queryFn: vi.fn(() =>
69+
sleep(10).then(() => 'The usePrefetchQuery hook is smart!'),
70+
),
7471
}
7572

7673
function Page() {
@@ -103,7 +100,7 @@ describe('usePrefetchQuery', () => {
103100
it('should let errors fall through and not refetch failed queries', async () => {
104101
const consoleMock = vi.spyOn(console, 'error')
105102
consoleMock.mockImplementation(() => undefined)
106-
const queryFn = generateQueryFn('Not an error')
103+
const queryFn = vi.fn(() => sleep(10).then(() => 'Not an error'))
107104

108105
const queryOpts = {
109106
queryKey: queryKey(),
@@ -146,7 +143,7 @@ describe('usePrefetchQuery', () => {
146143
})
147144

148145
it('should not create an endless loop when using inside a suspense boundary', async () => {
149-
const queryFn = generateQueryFn('prefetchedQuery')
146+
const queryFn = vi.fn(() => sleep(10).then(() => 'prefetchedQuery'))
150147

151148
const queryOpts = {
152149
queryKey: queryKey(),
@@ -182,7 +179,9 @@ describe('usePrefetchQuery', () => {
182179
it('should be able to recover from errors and try fetching again', async () => {
183180
const consoleMock = vi.spyOn(console, 'error')
184181
consoleMock.mockImplementation(() => undefined)
185-
const queryFn = generateQueryFn('This is fine :dog: :fire:')
182+
const queryFn = vi.fn(() =>
183+
sleep(10).then(() => 'This is fine :dog: :fire:'),
184+
)
186185

187186
const queryOpts = {
188187
queryKey: queryKey(),
@@ -240,17 +239,19 @@ describe('usePrefetchQuery', () => {
240239
it('should not create a suspense waterfall if prefetch is fired', async () => {
241240
const firstQueryOpts = {
242241
queryKey: queryKey(),
243-
queryFn: generateQueryFn('Prefetch is nice!'),
242+
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is nice!')),
244243
}
245244

246245
const secondQueryOpts = {
247246
queryKey: queryKey(),
248-
queryFn: generateQueryFn('Prefetch is really nice!!'),
247+
queryFn: vi.fn(() => sleep(10).then(() => 'Prefetch is really nice!!')),
249248
}
250249

251250
const thirdQueryOpts = {
252251
queryKey: queryKey(),
253-
queryFn: generateQueryFn('Prefetch does not create waterfalls!!'),
252+
queryFn: vi.fn(() =>
253+
sleep(10).then(() => 'Prefetch does not create waterfalls!!'),
254+
),
254255
}
255256

256257
const Fallback = vi.fn().mockImplementation(() => <div>Loading...</div>)

0 commit comments

Comments
 (0)