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

query-options.test-d.ts

142 lines3.8 KB

1import { assertType, describe, expectTypeOf, test } from 'vitest'
2import { QueryClient, dataTagSymbol, injectQuery, queryOptions } from '..'
3import type { Signal } from '@angular/core'
4​
5describe('queryOptions', () => {
6 test('should not allow excess properties', () => {
7 expectTypeOf(queryOptions).parameter(0).not.toHaveProperty('stallTime')
8 })
9​
10 test('should infer types for callbacks', () => {
11 queryOptions({
12 queryKey: ['key'],
13 queryFn: () => Promise.resolve(5),
14 staleTime: 1000,
15 select: (data) => {
16 expectTypeOf(data).toEqualTypeOf<number>()
17 },
18 })
19 })
20​
21 test('should allow undefined response in initialData', () => {
22 const options = (id: string | null) =>
23 queryOptions({
24 queryKey: ['todo', id],
25 queryFn: () =>
26 Promise.resolve({
27 id: '1',
28 title: 'Do Laundry',
29 }),
30 initialData: () =>
31 !id
32 ? undefined
33 : {
34 id,
35 title: 'Initial Data',
36 },
37 })
38​
39 expectTypeOf(options(null).initialData).returns.toEqualTypeOf<
40 { id: string; title: string } | undefined
41 >()
42 })
43})
44​
45test('should work when passed to injectQuery', () => {
46 const options = queryOptions({
47 queryKey: ['key'],
48 queryFn: () => Promise.resolve(5),
49 })
50​
51 const { data } = injectQuery(() => options)
52 expectTypeOf(data).toEqualTypeOf<Signal<number | undefined>>()
53})
54​
55test('should work when passed to fetchQuery', () => {
56 const options = queryOptions({
57 queryKey: ['key'],
58 queryFn: () => Promise.resolve(5),
59 })
60​
61 const data = new QueryClient().fetchQuery(options)
62 assertType<Promise<number>>(data)
63})
64​
65test('should tag the queryKey with the result type of the QueryFn', () => {
66 const { queryKey } = queryOptions({
67 queryKey: ['key'],
68 queryFn: () => Promise.resolve(5),
69 })
70 assertType<number>(queryKey[dataTagSymbol])
71})
72​
73test('should tag the queryKey even if no promise is returned', () => {
74 const { queryKey } = queryOptions({
75 queryKey: ['key'],
76 queryFn: () => 5,
77 })
78 assertType<number>(queryKey[dataTagSymbol])
79})
80​
81test('should tag the queryKey with unknown if there is no queryFn', () => {
82 const { queryKey } = queryOptions({
83 queryKey: ['key'],
84 })
85​
86 assertType<unknown>(queryKey[dataTagSymbol])
87})
88​
89test('should tag the queryKey with the result type of the QueryFn if select is used', () => {
90 const { queryKey } = queryOptions({
91 queryKey: ['key'],
92 queryFn: () => Promise.resolve(5),
93 select: (data) => data.toString(),
94 })
95​
96 assertType<number>(queryKey[dataTagSymbol])
97})
98​
99test('should return the proper type when passed to getQueryData', () => {
100 const { queryKey } = queryOptions({
101 queryKey: ['key'],
102 queryFn: () => Promise.resolve(5),
103 })
104​
105 const queryClient = new QueryClient()
106 const data = queryClient.getQueryData(queryKey)
107​
108 expectTypeOf(data).toEqualTypeOf<number | undefined>()
109})
110​
111test('should properly type updaterFn when passed to setQueryData', () => {
112 const { queryKey } = queryOptions({
113 queryKey: ['key'],
114 queryFn: () => Promise.resolve(5),
115 })
116​
117 const queryClient = new QueryClient()
118 const data = queryClient.setQueryData(queryKey, (prev) => {
119 expectTypeOf(prev).toEqualTypeOf<number | undefined>()
120 return prev
121 })
122​
123 expectTypeOf(data).toEqualTypeOf<number | undefined>()
124})
125​
126test('should properly type value when passed to setQueryData', () => {
127 const { queryKey } = queryOptions({
128 queryKey: ['key'],
129 queryFn: () => Promise.resolve(5),
130 })
131​
132 const queryClient = new QueryClient()
133​
134 // @ts-expect-error value should be a number
135 queryClient.setQueryData(queryKey, '5')
136 // @ts-expect-error value should be a number
137 queryClient.setQueryData(queryKey, () => '5')
138​
139 const data = queryClient.setQueryData(queryKey, 5)
140​
141 expectTypeOf(data).toEqualTypeOf<number | undefined>()
142})