-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.js
80 lines (74 loc) · 2.65 KB
/
test.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
72
73
74
75
76
77
78
79
80
import mockStore from '../../utils/asyncActionsUtils';
import createTypes from '../../creators/createTypes';
import withStatusHandling from '../withStatusHandling';
import withFailure from '.';
const MockService = {
fetchSomething: () => new Promise(resolve => resolve({ ok: true, data: 42 })),
fetchFailureNotFound: () => new Promise(resolve => resolve({ ok: false, problem: 'CLIENT_ERROR', status: 404 }))
};
const actions = createTypes(
['FETCH', 'FETCH_SUCCESS', 'FETCH_FAILURE', 'OTHER_ACTION', 'NOT_FOUND'],
'@TEST'
);
describe('withFailure', () => {
it('Handles correctly failure behavior', async () => {
const store = mockStore({});
await store.dispatch({
type: actions.FETCH,
target: 'aTarget',
service: MockService.fetchSomething,
injections: [withFailure(dispatch => dispatch({ type: actions.OTHER_ACTION }))]
});
await store.dispatch({
type: actions.FETCH,
target: 'aTarget',
service: MockService.fetchFailureNotFound,
injections: [withFailure(dispatch => dispatch({ type: actions.OTHER_ACTION }))]
});
const actionsDispatched = store.getActions();
// Does not dispatch FAILURE action
expect(actionsDispatched).toEqual([
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.FETCH_SUCCESS, target: 'aTarget', payload: 42 },
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.OTHER_ACTION }
]);
});
it('Does not execute depending on the status handler result', async () => {
const store = mockStore({});
// Does execute Failure handler
await store.dispatch({
type: actions.FETCH,
target: 'aTarget',
service: MockService.fetchFailureNotFound,
injections: [
withStatusHandling({ 404: dispatch => dispatch({ type: actions.NOT_FOUND }) }),
withFailure(dispatch => dispatch({ type: actions.OTHER_ACTION }))
]
});
// Does not execute Failure handler
await store.dispatch({
type: actions.FETCH,
target: 'aTarget',
service: MockService.fetchFailureNotFound,
injections: [
withStatusHandling({
404: dispatch => {
dispatch({ type: actions.NOT_FOUND });
return false;
}
}),
withFailure(dispatch => dispatch({ type: actions.OTHER_ACTION }))
]
});
const actionsDispatched = store.getActions();
// Does not dispatch FAILURE action
expect(actionsDispatched).toEqual([
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.NOT_FOUND },
{ type: actions.OTHER_ACTION },
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.NOT_FOUND }
]);
});
});