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

createQuery.test-d.ts

99 lines3.2 KB

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