Skip to content

Commit e3cee9c

Browse files
committed
test: tests for MobxMutation
1 parent b31364a commit e3cee9c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/mobx-mutation.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DefaultError, QueryClient } from '@tanstack/query-core';
2+
import { reaction } from 'mobx';
23
import { describe, expect, it, vi } from 'vitest';
34

45
import { MobxMutation } from './mobx-mutation';
@@ -93,4 +94,70 @@ describe('MobxMutation', () => {
9394
variables: undefined,
9495
});
9596
});
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+
});
96163
});

0 commit comments

Comments
 (0)