Open History to see the last commit
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β
110 describe('custom hook', () => {
111 it('should allow custom hooks using UseQueryOptions', () => {
112 type Data = string
113β
114 const useCustomQuery = (
115 options?: OmitKeyof<UseQueryOptions<Data>, 'queryKey' | 'queryFn'>,
116 ) => {
117 return useQuery({
118 ...options,
119 queryKey: ['todos-key'],
120 queryFn: () => Promise.resolve('data'),
121 })
122 }
123β
124 const { data } = useCustomQuery()
125β
126 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
127 })
128 })
129β
130 describe('structuralSharing', () => {
131 it('should restrict to same types', () => {
132 useQuery({
133 queryKey: ['key'],
134 queryFn: () => 5,
135 structuralSharing: (_oldData, newData) => {
136 return newData
137 },
138 })
139 })
140 })
141})