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

inject-mutation.test-d.ts

71 lines2.1 KB

1import { describe, expectTypeOf } from 'vitest'
2import { injectMutation } from '..'
3import { successMutator } 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 mutation = injectMutation(() => ({
9 mutationFn: successMutator<string>,
10 }))
11​
12 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string | undefined>>()
13 })
14​
15 test('data should be defined when mutation is success', () => {
16 const mutation = injectMutation(() => ({
17 mutationFn: successMutator<string>,
18 }))
19​
20 if (mutation.isSuccess()) {
21 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string>>()
22 }
23 })
24​
25 test('error should be null when mutation is success', () => {
26 const mutation = injectMutation(() => ({
27 mutationFn: successMutator<string>,
28 }))
29​
30 if (mutation.isSuccess()) {
31 expectTypeOf(mutation.error).toEqualTypeOf<Signal<null>>()
32 }
33 })
34​
35 test('data should be undefined when mutation is pending', () => {
36 const mutation = injectMutation(() => ({
37 mutationFn: successMutator<string>,
38 }))
39​
40 if (mutation.isPending()) {
41 expectTypeOf(mutation.data).toEqualTypeOf<Signal<undefined>>()
42 }
43 })
44​
45 test('error should be defined when mutation is error', () => {
46 const mutation = injectMutation(() => ({
47 mutationFn: successMutator<string>,
48 }))
49​
50 if (mutation.isError()) {
51 expectTypeOf(mutation.error).toEqualTypeOf<Signal<Error>>()
52 }
53 })
54​
55 test('should narrow variables', () => {
56 const mutation = injectMutation(() => ({
57 mutationFn: successMutator<string>,
58 }))
59​
60 if (mutation.isIdle()) {
61 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<undefined>>()
62 }
63 if (mutation.isPending()) {
64 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
65 }
66 if (mutation.isSuccess()) {
67 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
68 }
69 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string | undefined>>()
70 })
71})