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