This repository was archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathactions.spec.ts
81 lines (66 loc) · 1.98 KB
/
actions.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { mock1 } from './__mocks__/state.mock';
import { actions } from '@/store/polls/actions';
import { PollsState, PollActionContext } from '@/store/polls/types';
import * as api from '@/lib/polls/api';
import { Vote } from '@/lib/polls/models';
let actionCxt: PollActionContext;
let commit: jest.Mock;
let state: PollsState;
jest.mock('@/lib/polls/api.ts');
describe('Polls actions', () => {
beforeEach(() => {
commit = jest.fn();
state = mock1();
actionCxt = {
state,
commit,
dispatch: jest.fn(),
getters: jest.fn(),
rootGetters: jest.fn(),
rootState: {}
};
});
describe('load', () => {
beforeEach(async () => {
await actions.load(actionCxt);
});
test('call api.loadPolls', () => {
expect(api.loadPolls).toHaveBeenCalledTimes(1);
});
test('commits "setPolls" with polls from api call', async () => {
expect(commit).toHaveBeenCalledTimes(1);
const commitCall = commit.mock.calls[0];
const polls = await api.loadPolls();
expect(commitCall[1]).toEqual(polls);
});
});
describe('vote', () => {
const choiceId = 0;
test('commits "vote"', () => {
actions.vote(actionCxt, { choiceId });
expect(commit).toHaveBeenCalledTimes(1);
const vote: Vote = commit.mock.calls[0][1];
expect(vote.choiceId).toBe(choiceId);
});
describe('when there is no vote', () => {
beforeEach(() => {
state.votes = [];
actions.vote(actionCxt, { choiceId });
});
test('vote ID is 1', () => {
const vote: Vote = commit.mock.calls[0][1];
expect(vote.id).toBe(1);
});
});
describe('when there is some votes', () => {
beforeEach(() => {
state.votes = [{ id: 1, choiceId: 1 }];
actions.vote(actionCxt, { choiceId });
});
test('vote ID is increment from votes length', () => {
const vote: Vote = commit.mock.calls[0][1];
expect(vote.id).toBe(2);
});
});
});
});