Open History to see the last commit
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})