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

useQuery.test-d.tsx

150 lines4.0 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { useQuery } from '../useQuery'
3import { queryOptions } from '../queryOptions'
4import type { OmitKeyof } from '..'
5import type { UseQueryOptions } from '../types'
6​
7describe('initialData', () => {
8 describe('Config object overload', () => {
9 it('TData should always be defined when initialData is provided as an object', () => {
10 const { data } = useQuery({
11 queryKey: ['key'],
12 queryFn: () => ({ wow: true }),
13 initialData: { wow: true },
14 })
15​
16 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
17 })
18​
19 it('TData should be defined when passed through queryOptions', () => {
20 const options = queryOptions({
21 queryKey: ['key'],
22 queryFn: () => {
23 return {
24 wow: true,
25 }
26 },
27 initialData: {
28 wow: true,
29 },
30 })
31 const { data } = useQuery(options)
32​
33 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
34 })
35​
36 it('should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
37 const options = queryOptions({
38 queryKey: ['key'],
39 queryFn: () => Promise.resolve(1),
40 })
41​
42 const query = useQuery({
43 ...options,
44 select: (data) => data > 1,
45 })
46​
47 expectTypeOf(query.data).toEqualTypeOf<boolean | undefined>()
48 })
49​
50 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
51 const { data } = useQuery({
52 queryKey: ['key'],
53 queryFn: () => {
54 return {
55 wow: true,
56 }
57 },
58 initialData: () => ({
59 wow: true,
60 }),
61 })
62​
63 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
64 })
65​
66 it('TData should have undefined in the union when initialData is NOT provided', () => {
67 const { data } = useQuery({
68 queryKey: ['key'],
69 queryFn: () => {
70 return {
71 wow: true,
72 }
73 },
74 })
75​
76 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
77 })
78​
79 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
80 const { data } = useQuery({
81 queryKey: ['key'],
82 queryFn: () => {
83 return {
84 wow: true,
85 }
86 },
87 initialData: () => undefined as { wow: boolean } | undefined,
88 })
89​
90 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
91 })
92​
93 it('TData should be narrowed after an isSuccess check when initialData is provided as a function which can return undefined', () => {
94 const { data, isSuccess } = useQuery({
95 queryKey: ['key'],
96 queryFn: () => {
97 return {
98 wow: true,
99 }
100 },
101 initialData: () => undefined as { wow: boolean } | undefined,
102 })
103​
104 if (isSuccess) {
105 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
106 }
107 })
108​
109 it('data should not have undefined when initialData is provided', () => {
110 const { data } = useQuery({
111 queryKey: ['query-key'],
112 initialData: 42,
113 })
114​
115 expectTypeOf(data).toEqualTypeOf<number>()
116 })
117 })
118​
119 describe('custom hook', () => {
120 it('should allow custom hooks using UseQueryOptions', () => {
121 type Data = string
122​
123 const useCustomQuery = (
124 options?: OmitKeyof<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
125 ) => {
126 return useQuery({
127 ...options,
128 queryKey: ['todos-key'],
129 queryFn: () => Promise.resolve('data'),
130 })
131 }
132​
133 const { data } = useCustomQuery()
134​
135 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
136 })
137 })
138​
139 describe('structuralSharing', () => {
140 it('should restrict to same types', () => {
141 useQuery({
142 queryKey: ['key'],
143 queryFn: () => 5,
144 structuralSharing: (_oldData, newData) => {
145 return newData
146 },
147 })
148 })
149 })
150})