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