File size: 2,294 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import { describe, expectTypeOf, it } from 'vitest'
import { reactive } from 'vue-demi'
import { sleep } from '@tanstack/query-test-utils'
import { useMutation } from '../useMutation'
describe('Discriminated union return type', () => {
it('data should be possibly undefined by default', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
expectTypeOf(mutation.data).toEqualTypeOf<string | undefined>()
})
it('data should be defined when mutation is success', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
if (mutation.isSuccess) {
expectTypeOf(mutation.data).toEqualTypeOf<string>()
}
})
it('error should be null when mutation is success', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
if (mutation.isSuccess) {
expectTypeOf(mutation.error).toEqualTypeOf<null>()
}
})
it('data should be undefined when mutation is pending', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
if (mutation.isPending) {
expectTypeOf(mutation.data).toEqualTypeOf<undefined>()
}
})
it('error should be defined when mutation is error', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
if (mutation.isError) {
expectTypeOf(mutation.error).toEqualTypeOf<Error>()
}
})
it('should narrow variables', () => {
const mutation = reactive(
useMutation({
mutationFn: (params: string) => sleep(0).then(() => params),
}),
)
if (mutation.isIdle) {
expectTypeOf(mutation.variables).toEqualTypeOf<undefined>()
return
}
if (mutation.isPending) {
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
return
}
if (mutation.isSuccess) {
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
return
}
expectTypeOf(mutation.variables).toEqualTypeOf<string>()
})
})
|