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

useQuery.test-d.tsx

98 lines3.1 KB

1import { describe, expectTypeOf, it } from 'vitest'
2import { queryOptions, useQuery } from '../index'
3​
4describe('initialData', () => {
5 describe('Config object overload', () => {
6 it('TData should always be defined when initialData is provided as an object', () => {
7 const { data } = useQuery(() => ({
8 queryKey: ['key'],
9 queryFn: () => ({ wow: true }),
10 initialData: { wow: true },
11 }))
12​
13 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
14 })
15​
16 it('TData should be defined when passed through queryOptions', () => {
17 const options = queryOptions({
18 queryKey: ['key'],
19 queryFn: () => ({ wow: true }),
20 initialData: { wow: true },
21 })
22 const { data } = useQuery(() => options)
23​
24 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
25 })
26​
27 it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
28 const { data } = useQuery(() => ({
29 queryKey: ['key'],
30 queryFn: () => ({ wow: true }),
31 initialData: () => ({ wow: true }),
32 }))
33​
34 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
35 })
36​
37 it('TData should have undefined in the union when initialData is NOT provided', () => {
38 const { data } = useQuery(() => ({
39 queryKey: ['key'],
40 queryFn: () => ({ wow: true }),
41 }))
42​
43 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
44 })
45​
46 it('TData should have undefined in the union when initialData is provided as a function which can return undefined', () => {
47 const { data } = useQuery(() => ({
48 queryKey: ['key'],
49 queryFn: () => ({ wow: true }),
50 initialData: () => undefined as { wow: boolean } | undefined,
51 }))
52​
53 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
54 })
55 })
56​
57 describe('Query key overload', () => {
58 it('TData should always be defined when initialData is provided', () => {
59 const { data } = useQuery(() => ({
60 queryKey: ['key'],
61 queryFn: () => ({ wow: true }),
62 initialData: { wow: true },
63 }))
64​
65 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
66 })
67​
68 it('TData should have undefined in the union when initialData is NOT provided', () => {
69 const { data } = useQuery(() => ({
70 queryKey: ['key'],
71 queryFn: () => ({ wow: true }),
72 }))
73​
74 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
75 })
76 })
77​
78 describe('Query key and func', () => {
79 it('TData should always be defined when initialData is provided', () => {
80 const { data } = useQuery(() => ({
81 queryKey: ['key'],
82 queryFn: () => ({ wow: true }),
83 initialData: { wow: true },
84 }))
85​
86 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
87 })
88​
89 it('TData should have undefined in the union when initialData is NOT provided', () => {
90 const { data } = useQuery(() => ({
91 queryKey: ['key'],
92 queryFn: () => ({ wow: true }),
93 }))
94​
95 expectTypeOf(data).toEqualTypeOf<{ wow: boolean } | undefined>()
96 })
97 })
98})