|
| 1 | +import { reactive } from 'vue' |
| 2 | +import { useMutation } from '../useMutation' |
| 3 | +import { doNotExecute, Equal, Expect, successMutator } from './test-utils' |
| 4 | + |
| 5 | +describe('Discriminated union return type', () => { |
| 6 | + it('data should be possibly undefined by default', () => { |
| 7 | + doNotExecute(() => { |
| 8 | + const mutation = reactive( |
| 9 | + useMutation({ mutationFn: successMutator<string> }), |
| 10 | + ) |
| 11 | + |
| 12 | + const result: Expect<Equal<string | undefined, typeof mutation.data>> = |
| 13 | + true |
| 14 | + return result |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + it('data should be defined when mutation is success', () => { |
| 19 | + doNotExecute(() => { |
| 20 | + const mutation = reactive( |
| 21 | + useMutation({ mutationFn: successMutator<string> }), |
| 22 | + ) |
| 23 | + |
| 24 | + if (mutation.isSuccess) { |
| 25 | + const result: Expect<Equal<string, typeof mutation.data>> = true |
| 26 | + return result |
| 27 | + } |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + it('error should be null when mutation is success', () => { |
| 32 | + doNotExecute(() => { |
| 33 | + const mutation = reactive( |
| 34 | + useMutation({ mutationFn: successMutator<string> }), |
| 35 | + ) |
| 36 | + |
| 37 | + if (mutation.isSuccess) { |
| 38 | + const result: Expect<Equal<null, typeof mutation.error>> = true |
| 39 | + return result |
| 40 | + } |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('data should be undefined when mutation is loading', () => { |
| 45 | + doNotExecute(() => { |
| 46 | + const mutation = reactive( |
| 47 | + useMutation({ mutationFn: successMutator<string> }), |
| 48 | + ) |
| 49 | + |
| 50 | + if (mutation.isLoading) { |
| 51 | + const result: Expect<Equal<undefined, typeof mutation.data>> = true |
| 52 | + return result |
| 53 | + } |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + it('error should be defined when mutation is error', () => { |
| 58 | + doNotExecute(() => { |
| 59 | + const mutation = reactive( |
| 60 | + useMutation({ mutationFn: successMutator<string> }), |
| 61 | + ) |
| 62 | + |
| 63 | + if (mutation.isError) { |
| 64 | + const result: Expect<Equal<unknown, typeof mutation.error>> = true |
| 65 | + return result |
| 66 | + } |
| 67 | + }) |
| 68 | + }) |
| 69 | +}) |
0 commit comments