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

useQuery.test-d.tsx

101 lines3.3 KB

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