-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
asyncActions.spec.js
71 lines (51 loc) · 1.79 KB
/
asyncActions.spec.js
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
import { RestApi } from '@magento/peregrine';
import { dispatch, getState } from '../../../store';
import actions from '../actions';
import { getCountries } from '../asyncActions';
jest.mock('../../../store');
const thunkArgs = [dispatch, getState];
const { request } = RestApi.Magento2;
afterEach(() => {
dispatch.mockClear();
request.mockClear();
});
test('getCountries() to return a thunk', () => {
expect(getCountries()).toBeInstanceOf(Function);
});
test('getCountries thunk returns undefined', async () => {
const thunk = getCountries();
await expect(thunk(...thunkArgs)).resolves.toBeUndefined();
});
test('getCountries thunk does nothing if data is present', async () => {
getState.mockImplementationOnce(() => ({
directory: {
countries: []
}
}));
await getCountries()(...thunkArgs);
expect(request).not.toHaveBeenCalled();
});
test('getCountries thunk requests API data', async () => {
await getCountries()(...thunkArgs);
expect(request).toHaveBeenCalled();
});
test('getCountries thunk dispatches actions on success', async () => {
const response = 'FOO';
getState.mockImplementationOnce(() => ({
directory: {}
}));
request.mockResolvedValueOnce(response);
await getCountries()(...thunkArgs);
expect(dispatch).toHaveBeenCalledWith(actions.getCountries(response));
expect(dispatch).toHaveBeenCalledTimes(1);
});
test('getCountries thunk dispatches actions on failure', async () => {
const error = new Error('BAR');
getState.mockImplementationOnce(() => ({
directory: {}
}));
request.mockRejectedValueOnce(error);
await getCountries()(...thunkArgs);
expect(dispatch).toHaveBeenCalledWith(actions.getCountries(error));
expect(dispatch).toHaveBeenCalledTimes(1);
});