|
1 | 1 | import { DefaultError, QueryClient } from '@tanstack/query-core'; |
| 2 | +import { reaction } from 'mobx'; |
2 | 3 | import { describe, expect, it, vi } from 'vitest'; |
3 | 4 |
|
4 | 5 | import { MobxMutation } from './mobx-mutation'; |
@@ -93,4 +94,70 @@ describe('MobxMutation', () => { |
93 | 94 | variables: undefined, |
94 | 95 | }); |
95 | 96 | }); |
| 97 | + |
| 98 | + it('should change mutation status (success)', async () => { |
| 99 | + const mobxMutation = new MobxMutationMock({ |
| 100 | + mutationKey: ['test'], |
| 101 | + mutationFn: async () => { |
| 102 | + return 'OK'; |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + const statuses: (typeof mobxMutation)['result']['status'][] = []; |
| 107 | + |
| 108 | + reaction( |
| 109 | + () => mobxMutation.result.status, |
| 110 | + (status) => { |
| 111 | + statuses.push(status); |
| 112 | + }, |
| 113 | + { |
| 114 | + fireImmediately: true, |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + await mobxMutation.mutate(); |
| 119 | + |
| 120 | + expect(statuses).toStrictEqual(['idle', 'pending', 'success']); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should change mutation status (failure)', async () => { |
| 124 | + const mobxMutation = new MobxMutationMock({ |
| 125 | + mutationKey: ['test'], |
| 126 | + mutationFn: async () => { |
| 127 | + throw new Error('BAD'); |
| 128 | + }, |
| 129 | + }); |
| 130 | + |
| 131 | + const statuses: (typeof mobxMutation)['result']['status'][] = []; |
| 132 | + |
| 133 | + reaction( |
| 134 | + () => mobxMutation.result.status, |
| 135 | + (status) => { |
| 136 | + statuses.push(status); |
| 137 | + }, |
| 138 | + { |
| 139 | + fireImmediately: true, |
| 140 | + }, |
| 141 | + ); |
| 142 | + |
| 143 | + try { |
| 144 | + await mobxMutation.mutate(); |
| 145 | + // eslint-disable-next-line no-empty |
| 146 | + } catch {} |
| 147 | + |
| 148 | + expect(statuses).toStrictEqual(['idle', 'pending', 'error']); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should throw exception', async () => { |
| 152 | + const mobxMutation = new MobxMutationMock({ |
| 153 | + mutationKey: ['test'], |
| 154 | + mutationFn: async () => { |
| 155 | + throw new Error('BAD'); |
| 156 | + }, |
| 157 | + }); |
| 158 | + |
| 159 | + expect(async () => { |
| 160 | + await mobxMutation.mutate(); |
| 161 | + }).rejects.toThrowError('BAD'); |
| 162 | + }); |
96 | 163 | }); |
0 commit comments