Skip to content

Commit bc91d03

Browse files
test(clientQuery): adds tests for type-errors when using strict keys
1 parent 15de9ce commit bc91d03

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

src/core/tests/queryClient.test.tsx

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sleep, queryKey, mockConsoleError } from '../../react/tests/utils'
2-
import { QueryCache, QueryClient, QueryObserver } from '../..'
2+
import { QueryCache, QueryClient, QueryFunction, QueryObserver } from '../..'
33

44
describe('queryClient', () => {
55
let queryClient: QueryClient
@@ -186,6 +186,23 @@ describe('queryClient', () => {
186186
})
187187

188188
describe('fetchQuery', () => {
189+
test('should not type-error with strict query key', async () => {
190+
type StrictData = 'data'
191+
type StrictQueryKey = ['strict', string]
192+
const key: StrictQueryKey = ['strict', queryKey()]
193+
194+
const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () => (
195+
Promise.resolve('data')
196+
)
197+
198+
await expect(
199+
queryClient.fetchQuery<StrictData, any, StrictData, StrictQueryKey>(
200+
key,
201+
fetchFn,
202+
)
203+
).resolves.toEqual('data')
204+
})
205+
189206
// https://github.com/tannerlinsley/react-query/issues/652
190207
test('should not retry by default', async () => {
191208
const consoleMock = mockConsoleError()
@@ -282,6 +299,28 @@ describe('queryClient', () => {
282299
})
283300

284301
describe('fetchInfiniteQuery', () => {
302+
test('should not type-error with strict query key', async () => {
303+
type StrictData = string
304+
type StrictQueryKey = ['strict', string]
305+
const key: StrictQueryKey = ['strict', queryKey()]
306+
307+
const data = {
308+
pages: ['data'],
309+
pageParams: [undefined],
310+
}
311+
312+
const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () => (
313+
Promise.resolve(data.pages[0])
314+
)
315+
316+
await expect(
317+
queryClient.fetchInfiniteQuery<StrictData, any, StrictData, StrictQueryKey>(
318+
key,
319+
fetchFn,
320+
)
321+
).resolves.toEqual(data)
322+
})
323+
285324
test('should return infinite query data', async () => {
286325
const key = queryKey()
287326
const result = await queryClient.fetchInfiniteQuery(
@@ -301,6 +340,25 @@ describe('queryClient', () => {
301340
})
302341

303342
describe('prefetchInfiniteQuery', () => {
343+
test('should not type-error with strict query key', async () => {
344+
type StrictData = 'data'
345+
type StrictQueryKey = ['strict', string]
346+
const key: StrictQueryKey = ['strict', queryKey()]
347+
348+
const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () => (
349+
Promise.resolve('data')
350+
)
351+
352+
await queryClient.prefetchInfiniteQuery<StrictData, any, StrictData, StrictQueryKey>(key, fetchFn)
353+
354+
const result = queryClient.getQueryData(key)
355+
356+
expect(result).toEqual({
357+
pages: ['data'],
358+
pageParams: [undefined],
359+
})
360+
})
361+
304362
test('should return infinite query data', async () => {
305363
const key = queryKey()
306364

@@ -318,6 +376,22 @@ describe('queryClient', () => {
318376
})
319377

320378
describe('prefetchQuery', () => {
379+
test('should not type-error with strict query key', async () => {
380+
type StrictData = 'data'
381+
type StrictQueryKey = ['strict', string]
382+
const key: StrictQueryKey = ['strict', queryKey()]
383+
384+
const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () => (
385+
Promise.resolve('data')
386+
)
387+
388+
await queryClient.prefetchQuery<StrictData, any, StrictData, StrictQueryKey>(key, fetchFn);
389+
390+
const result = queryClient.getQueryData(key);
391+
392+
expect(result).toEqual('data')
393+
})
394+
321395
test('should return undefined when an error is thrown', async () => {
322396
const consoleMock = mockConsoleError()
323397

0 commit comments

Comments
 (0)