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