Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { injectQuery, queryOptions } from '..'
3import { simpleFetcher } from './test-utils'
4import type { Signal } from '@angular/core'
5β
6describe('initialData', () => {
7 describe('Config object overload', () => {
8 it('TData should always be defined when initialData is provided as an object', () => {
9 const { data } = injectQuery(() => ({
10 queryKey: ['key'],
11 queryFn: () => ({ wow: true }),
12 initialData: { wow: true },
13 }))
14β
15 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
16 })
17β
18 it('TData should be defined when passed through queryOptions', () => {
19 const options = () =>
20 queryOptions({
21 queryKey: ['key'],
22 queryFn: () => {
23 return {
24 wow: true,
25 }
26 },
27 initialData: {
28 wow: true,
29 },
30 })
31 const { data } = injectQuery(options)
32β
33 expectTypeOf(data).toEqualTypeOf<Signal<{ 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 = injectQuery(() => ({
43 ...options,
44 select: (data) => data > 1,
45 }))
46β
47 expectTypeOf(query.data).toEqualTypeOf<Signal<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 } = injectQuery(() => ({
52 queryKey: ['key'],
53 queryFn: () => {
54 return {
55 wow: true,
56 }
57 },
58 initialData: () => ({
59 wow: true,
60 }),
61 }))
62β
63 expectTypeOf(data).toEqualTypeOf<Signal<{ wow: boolean }>>()
64 })
65β
66 it('TData should have undefined in the union when initialData is NOT provided', () => {
67 const { data } = injectQuery(() => ({
68 queryKey: ['key'],
69 queryFn: () => {
70 return {
71 wow: true,
72 }
73 },
74 }))
75β
76 expectTypeOf(data).toEqualTypeOf<Signal<{ 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 } = injectQuery(() => ({
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<Signal<{ 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 query = injectQuery(() => ({
95 queryKey: ['key'],
96 queryFn: () => {
97 return {
98 wow: true,
99 }
100 },
101 initialData: () => undefined as { wow: boolean } | undefined,
102 }))
103β
104 if (query.isSuccess()) {
105 expectTypeOf(query.data).toEqualTypeOf<Signal<{ wow: boolean }>>()
106 }
107 })
108 })
109β
110 describe('structuralSharing', () => {
111 it('should restrict to same types', () => {
112 injectQuery(() => ({
113 queryKey: ['key'],
114 queryFn: () => 5,
115 structuralSharing: (_oldData, newData) => {
116 return newData
117 },
118 }))
119 })
120 })
121})
122β
123describe('Discriminated union return type', () => {
124 test('data should be possibly undefined by default', () => {
125 const query = injectQuery(() => ({
126 queryKey: ['key'],
127 queryFn: simpleFetcher,
128 }))
129β
130 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
131 })
132β
133 test('data should be defined when query is success', () => {
134 const query = injectQuery(() => ({
135 queryKey: ['key'],
136 queryFn: simpleFetcher,
137 }))
138β
139 if (query.isSuccess()) {
140 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
141 }
142 })
143β
144 test('error should be null when query is success', () => {
145 const query = injectQuery(() => ({
146 queryKey: ['key'],
147 queryFn: simpleFetcher,
148 }))
149β
150 if (query.isSuccess()) {
151 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
152 }
153 })
154β
155 test('data should be undefined when query is pending', () => {
156 const query = injectQuery(() => ({
157 queryKey: ['key'],
158 queryFn: simpleFetcher,
159 }))
160β
161 if (query.isPending()) {
162 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
163 }
164 })
165β
166 test('error should be defined when query is error', () => {
167 const query = injectQuery(() => ({
168 queryKey: ['key'],
169 queryFn: simpleFetcher,
170 }))
171β
172 if (query.isError()) {
173 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
174 }
175 })
176})