Open History to see the last commit
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})