Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryClient.test-d.ts

132 lines4.2 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { QueryClient } from '../queryClient'
3import type { DataTag, InfiniteData } from '@tanstack/query-core'
4​
5describe('getQueryData', () => {
6 it('should be typed if key is tagged', () => {
7 const queryKey = ['key'] as DataTag<Array<string>, number>
8 const queryClient = new QueryClient()
9 const data = queryClient.getQueryData(queryKey)
10​
11 expectTypeOf(data).toEqualTypeOf<number | undefined>()
12 })
13​
14 it('should infer unknown if key is not tagged', () => {
15 const queryKey = ['key'] as const
16 const queryClient = new QueryClient()
17 const data = queryClient.getQueryData(queryKey)
18​
19 expectTypeOf(data).toEqualTypeOf<unknown>()
20 })
21​
22 it('should infer passed generic if passed', () => {
23 const queryKey = ['key'] as const
24 const queryClient = new QueryClient()
25 const data = queryClient.getQueryData<number>(queryKey)
26​
27 expectTypeOf(data).toEqualTypeOf<number | undefined>()
28 })
29​
30 it('should only allow Arrays to be passed', () => {
31 const queryKey = 'key'
32 const queryClient = new QueryClient()
33 // @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey'
34 return queryClient.getQueryData(queryKey)
35 })
36})
37​
38describe('setQueryData', () => {
39 it('updater should be typed if key is tagged', () => {
40 const queryKey = ['key'] as DataTag<Array<string>, number>
41 const queryClient = new QueryClient()
42 const data = queryClient.setQueryData(queryKey, (prev) => {
43 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
44 return prev
45 })
46 expectTypeOf(data).toEqualTypeOf<number | undefined>()
47 })
48​
49 it('value should be typed if key is tagged', () => {
50 const queryKey = ['key'] as DataTag<Array<string>, number>
51 const queryClient = new QueryClient()
52​
53 // @ts-expect-error value should be a number
54 queryClient.setQueryData(queryKey, '1')
55​
56 // @ts-expect-error value should be a number
57 queryClient.setQueryData(queryKey, () => '1')
58​
59 const data = queryClient.setQueryData(queryKey, 1)
60​
61 expectTypeOf(data).toEqualTypeOf<number | undefined>()
62 })
63​
64 it('should infer unknown for updater if key is not tagged', () => {
65 const queryKey = ['key'] as const
66 const queryClient = new QueryClient()
67 const data = queryClient.setQueryData(queryKey, (prev) => {
68 expectTypeOf(prev).toEqualTypeOf<unknown>()
69 return prev
70 })
71 expectTypeOf(data).toEqualTypeOf<unknown>()
72 })
73​
74 it('should infer unknown for value if key is not tagged', () => {
75 const queryKey = ['key'] as const
76 const queryClient = new QueryClient()
77 const data = queryClient.setQueryData(queryKey, 'foo')
78​
79 expectTypeOf(data).toEqualTypeOf<unknown>()
80 })
81​
82 it('should infer passed generic if passed', () => {
83 const queryKey = ['key'] as const
84 const queryClient = new QueryClient()
85 const data = queryClient.setQueryData<string>(queryKey, (prev) => {
86 expectTypeOf(prev).toEqualTypeOf<string | undefined>()
87 return prev
88 })
89 expectTypeOf(data).toEqualTypeOf<string | undefined>()
90 })
91​
92 it('should infer passed generic for value', () => {
93 const queryKey = ['key'] as const
94 const queryClient = new QueryClient()
95 const data = queryClient.setQueryData<string>(queryKey, 'foo')
96​
97 expectTypeOf(data).toEqualTypeOf<string | undefined>()
98 })
99})
100​
101describe('fetchInfiniteQuery', () => {
102 it('should allow passing pages', async () => {
103 const data = await new QueryClient().fetchInfiniteQuery({
104 queryKey: ['key'],
105 queryFn: () => Promise.resolve('string'),
106 getNextPageParam: () => 1,
107 initialPageParam: 1,
108 pages: 5,
109 })
110​
111 expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
112 })
113​
114 it('should not allow passing getNextPageParam without pages', () => {
115 new QueryClient().fetchInfiniteQuery({
116 queryKey: ['key'],
117 queryFn: () => Promise.resolve('string'),
118 initialPageParam: 1,
119 getNextPageParam: () => 1,
120 })
121 })
122​
123 it('should not allow passing pages without getNextPageParam', () => {
124 // @ts-expect-error Property 'getNextPageParam' is missing
125 new QueryClient().fetchInfiniteQuery({
126 queryKey: ['key'],
127 queryFn: () => Promise.resolve('string'),
128 initialPageParam: 1,
129 pages: 5,
130 })
131 })
132})