Skip to content

Commit 41a7ab0

Browse files
test(query-core/utils): add tests for 'ensureQueryFn' with 'initialPromise' and 'skipToken' (TanStack#10527)
* test(query-core/utils): add tests for 'ensureQueryFn' initialPromise fallback and skipToken handling * test(query-core/utils): use typed QueryFunctionContext in ensureQueryFn tests Replace the `(resolved as unknown as () => Promise<...>)()` double-cast with `resolved(context)` using a shared `QueryFunctionContext` fixture. Per review suggestions on TanStack#10527. --------- Co-authored-by: Wonsuk Choi <sukvvon@gmail.com>
1 parent 2f9527e commit 41a7ab0

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

packages/query-core/src/__tests__/utils.test.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { QueryClient } from '..'
44
import {
55
addToEnd,
66
addToStart,
7+
ensureQueryFn,
78
hashKey,
89
hashQueryKeyByOptions,
910
isPlainArray,
@@ -14,8 +15,10 @@ import {
1415
replaceEqualDeep,
1516
shallowEqualObjects,
1617
shouldThrowError,
18+
skipToken,
1719
} from '../utils'
1820
import { Mutation } from '../mutation'
21+
import type { QueryFunctionContext } from '..'
1922

2023
describe('core/utils', () => {
2124
describe('hashQueryKeyByOptions', () => {
@@ -534,6 +537,55 @@ describe('core/utils', () => {
534537
})
535538
})
536539

540+
describe('ensureQueryFn', () => {
541+
const context = {} as QueryFunctionContext
542+
543+
it('should return a function that resolves to initialPromise when queryFn is missing and initialPromise is provided', async () => {
544+
const initialPromise = Promise.resolve('initial-data')
545+
546+
const resolved = ensureQueryFn(
547+
{ queryHash: '["key"]' },
548+
{ initialPromise },
549+
)
550+
551+
await expect(resolved(context)).resolves.toBe('initial-data')
552+
})
553+
554+
it('should return a function that rejects when initialPromise rejects', async () => {
555+
const error = new Error('initial-promise-error')
556+
const initialPromise = Promise.reject(error)
557+
558+
const resolved = ensureQueryFn(
559+
{ queryHash: '["key"]' },
560+
{ initialPromise },
561+
)
562+
563+
await expect(resolved(context)).rejects.toBe(error)
564+
})
565+
566+
it('should return a function that rejects with missing queryFn error when queryFn is set to skipToken', async () => {
567+
const consoleErrorSpy = vi
568+
.spyOn(console, 'error')
569+
.mockImplementation(() => undefined)
570+
571+
const resolved = ensureQueryFn({
572+
queryFn: skipToken,
573+
queryHash: '["skip"]',
574+
})
575+
576+
expect(consoleErrorSpy).toHaveBeenCalledWith(
577+
expect.stringContaining(
578+
'Attempted to invoke queryFn when set to skipToken',
579+
),
580+
)
581+
await expect(resolved(context)).rejects.toThrow(
582+
'Missing queryFn: \'["skip"]\'',
583+
)
584+
585+
consoleErrorSpy.mockRestore()
586+
})
587+
})
588+
537589
describe('shouldThrowError', () => {
538590
it('should return the result of executing throwOnError if throwOnError parameter is a function', () => {
539591
const throwOnError = (error: Error) => error.message === 'test error'

0 commit comments

Comments
 (0)