Skip to content

Commit e09f130

Browse files
committed
test: formMachine 단위 테스트 작업중
1 parent 685d61b commit e09f130

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import {
2+
type Mock,
3+
afterEach,
4+
beforeEach,
5+
describe,
6+
expect,
7+
it,
8+
vi,
9+
} from 'vitest';
10+
import { createActor } from 'xstate';
11+
12+
import { api as userApi } from '~/entities/user';
13+
import { formMachine } from './form.machine';
14+
15+
vi.mock('~/entities/user', () => ({
16+
api: {
17+
getUserInfo: vi.fn(),
18+
},
19+
}));
20+
21+
describe('formMachine 테스트', () => {
22+
beforeEach(() => {
23+
vi.resetAllMocks();
24+
});
25+
26+
afterEach(() => {
27+
vi.resetAllMocks();
28+
});
29+
30+
it('초기에는 Empty Buy Form 상태이다.', () => {
31+
const actor = createActor(formMachine);
32+
33+
actor.start();
34+
35+
expect(actor.getSnapshot().value).toBe('Empty Buy Form');
36+
expect(actor.getSnapshot().context.tradeType).toBe('매수');
37+
expect(actor.getSnapshot().context.orderType).toBe('지정가');
38+
expect(actor.getSnapshot().context.price).toBe(undefined);
39+
expect(actor.getSnapshot().context.quantity).toBe(undefined);
40+
expect(actor.getSnapshot().context.message).toBe('');
41+
expect(actor.getSnapshot().context.deposit).toBe(undefined);
42+
expect(actor.getSnapshot().context.holdings).toBe(undefined);
43+
});
44+
45+
it('사용자의 자산을 로드한다.', async () => {
46+
const mockResponse = {
47+
json: vi.fn().mockResolvedValue({
48+
data: {
49+
cash: 100000,
50+
wallets: [
51+
{ ticker: 'BTC', size: 1.5 },
52+
{ ticker: 'ETH', size: 10 },
53+
],
54+
},
55+
}),
56+
};
57+
58+
(userApi.getUserInfo as Mock).mockResolvedValue(mockResponse);
59+
60+
const actor = createActor(formMachine);
61+
62+
actor.start();
63+
});
64+
65+
it('SWITCH_TRADE_TYPE 이벤트가 발생하면 tradeType이 변경된다.', () => {
66+
const actor = createActor(formMachine);
67+
68+
actor.start();
69+
70+
expect(actor.getSnapshot().value).toBe('Empty Buy Form');
71+
72+
actor.send({ type: 'SWITCH_TRADE_TYPE', tradeType: '매수' });
73+
74+
expect(actor.getSnapshot().value).toBe('Empty Sell Form');
75+
76+
actor.send({ type: 'SWITCH_TRADE_TYPE', tradeType: '매도' });
77+
78+
expect(actor.getSnapshot().value).toBe('Empty Buy Form');
79+
});
80+
81+
// it('SWITCH_ORDER_TYPE 이벤트가 발생하면 orderType이 변경된다.', () => {
82+
// const actor = createActor(formMachine);
83+
84+
// actor.start();
85+
86+
// expect(actor.getSnapshot().value).toBe('Empty Buy Form');
87+
88+
// actor.send({ type: 'SWITCH_ORDER_TYPE', orderType: '지정가' });
89+
90+
// expect(actor.getSnapshot().value).toBe('Empty Buy Form');
91+
// expect(actor.getSnapshot().context.orderType).toBe('지정가');
92+
93+
// actor.send({ type: 'SWITCH_ORDER_TYPE', orderType: '시장가' });
94+
95+
// expect(actor.getSnapshot().value).toBe('Empty Buy Form');
96+
// expect(actor.getSnapshot().context.orderType).toBe('시장가');
97+
// });
98+
});

0 commit comments

Comments
 (0)