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