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

inject-query.test-d.ts

59 lines1.5 KB

1import { describe, expectTypeOf } from 'vitest'
2import { injectQuery } from '../inject-query'
3import { simpleFetcher } from './test-utils'
4import type { Signal } from '@angular/core'
5​
6describe('Discriminated union return type', () => {
7 test('data should be possibly undefined by default', () => {
8 const query = injectQuery(() => ({
9 queryKey: ['key'],
10 queryFn: simpleFetcher,
11 }))
12​
13 expectTypeOf(query.data).toEqualTypeOf<Signal<string | undefined>>()
14 })
15​
16 test('data should be defined when query is success', () => {
17 const query = injectQuery(() => ({
18 queryKey: ['key'],
19 queryFn: simpleFetcher,
20 }))
21​
22 if (query.isSuccess()) {
23 expectTypeOf(query.data).toEqualTypeOf<Signal<string>>()
24 }
25 })
26​
27 test('error should be null when query is success', () => {
28 const query = injectQuery(() => ({
29 queryKey: ['key'],
30 queryFn: simpleFetcher,
31 }))
32​
33 if (query.isSuccess()) {
34 expectTypeOf(query.error).toEqualTypeOf<Signal<null>>()
35 }
36 })
37​
38 test('data should be undefined when query is pending', () => {
39 const query = injectQuery(() => ({
40 queryKey: ['key'],
41 queryFn: simpleFetcher,
42 }))
43​
44 if (query.isPending()) {
45 expectTypeOf(query.data).toEqualTypeOf<Signal<undefined>>()
46 }
47 })
48​
49 test('error should be defined when query is error', () => {
50 const query = injectQuery(() => ({
51 queryKey: ['key'],
52 queryFn: simpleFetcher,
53 }))
54​
55 if (query.isError()) {
56 expectTypeOf(query.error).toEqualTypeOf<Signal<Error>>()
57 }
58 })
59})