Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { sleep } from '@tanstack/query-test-utils'
3import { injectMutation } from '..'
4import type { Signal } from '@angular/core'
5β
6describe('injectMutation', () => {
7 describe('Discriminated union return type', () => {
8 it('data should be possibly undefined by default', () => {
9 const mutation = injectMutation(() => ({
10 mutationFn: () => sleep(0).then(() => 'string'),
11 }))
12β
13 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string | undefined>>()
14 })
15β
16 it('data should be defined when mutation is success', () => {
17 const mutation = injectMutation(() => ({
18 mutationFn: () => sleep(0).then(() => 'string'),
19 }))
20β
21 if (mutation.isSuccess()) {
22 expectTypeOf(mutation.data).toEqualTypeOf<Signal<string>>()
23 }
24 })
25β
26 it('error should be null when mutation is success', () => {
27 const mutation = injectMutation(() => ({
28 mutationFn: () => sleep(0).then(() => 'string'),
29 }))
30β
31 if (mutation.isSuccess()) {
32 expectTypeOf(mutation.error).toEqualTypeOf<Signal<null>>()
33 }
34 })
35β
36 it('data should be undefined when mutation is pending', () => {
37 const mutation = injectMutation(() => ({
38 mutationFn: () => sleep(0).then(() => 'string'),
39 }))
40β
41 if (mutation.isPending()) {
42 expectTypeOf(mutation.data).toEqualTypeOf<Signal<undefined>>()
43 }
44 })
45β
46 it('error should be defined when mutation is error', () => {
47 const mutation = injectMutation(() => ({
48 mutationFn: () => sleep(0).then(() => 'string'),
49 }))
50β
51 if (mutation.isError()) {
52 expectTypeOf(mutation.error).toEqualTypeOf<Signal<Error>>()
53 }
54 })
55β
56 it('should narrow variables', () => {
57 const mutation = injectMutation(() => ({
58 mutationFn: (_variables: string) => sleep(0).then(() => 'string'),
59 }))
60β
61 if (mutation.isIdle()) {
62 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<undefined>>()
63 }
64 if (mutation.isPending()) {
65 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
66 }
67 if (mutation.isSuccess()) {
68 expectTypeOf(mutation.variables).toEqualTypeOf<Signal<string>>()
69 }
70 expectTypeOf(mutation.variables).toEqualTypeOf<
71 Signal<string | undefined>
72 >()
73 })
74 })
75})