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() }) 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() } }) 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() } }) 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() } }) 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() } }) it('should narrow variables', () => { const mutation = reactive( useMutation({ mutationFn: (params: string) => sleep(0).then(() => params), }), ) if (mutation.isIdle) { expectTypeOf(mutation.variables).toEqualTypeOf() return } if (mutation.isPending) { expectTypeOf(mutation.variables).toEqualTypeOf() return } if (mutation.isSuccess) { expectTypeOf(mutation.variables).toEqualTypeOf() return } expectTypeOf(mutation.variables).toEqualTypeOf() }) })