-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.js
56 lines (50 loc) · 2.14 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
import mockStore from '../../utils/asyncActionsUtils';
import createTypes from '../../creators/createTypes';
import withStatusHandling from '.';
const MockService = {
fetchSomething: () => new Promise(resolve => resolve({ ok: true, data: 42 })),
fetchFailureNotFound: () => new Promise(resolve => resolve({ ok: false, problem: 'CLIENT_ERROR', status: 404 })),
fetchFailureExpiredToken: () => new Promise(resolve => resolve({ ok: false, problem: 'CLIENT_ERROR', status: 422 }))
};
const actions = createTypes(
['FETCH', 'FETCH_SUCCESS', 'FETCH_FAILURE', 'NOT_FOUND', 'EXPIRED_TOKEN'],
'@TEST'
);
const customThunkAction = serviceCall => ({
type: actions.FETCH,
target: 'aTarget',
service: serviceCall,
injections: [withStatusHandling({ 404: dispatch => dispatch({ type: actions.NOT_FOUND }) })]
});
describe('withStatusHandling', () => {
it('Handles correctly status codes', async () => {
const store = mockStore({});
await store.dispatch(customThunkAction(MockService.fetchFailureNotFound));
const actionsDispatched = store.getActions();
expect(actionsDispatched).toEqual([
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.NOT_FOUND },
{ type: actions.FETCH_FAILURE, target: 'aTarget', payload: 'CLIENT_ERROR' }
]);
});
it('If not encounters a status code handler, it dispatches FAILURE', async () => {
const store = mockStore({});
await store.dispatch(customThunkAction(MockService.fetchFailureExpiredToken));
const actionsDispatched = store.getActions();
expect(actionsDispatched).toEqual([
{ type: actions.FETCH, target: 'aTarget' },
{ type: actions.FETCH_FAILURE, target: 'aTarget', payload: 'CLIENT_ERROR' }
]);
});
it('Does not dispatch a FAILURE if a handler returns false', async () => {
const store = mockStore({});
await store.dispatch({
type: actions.FETCH,
target: 'aTarget',
service: MockService.fetchFailureExpiredToken,
injections: [withStatusHandling({ 422: () => false })]
});
const actionsDispatched = store.getActions();
expect(actionsDispatched).toEqual([{ type: actions.FETCH, target: 'aTarget' }]);
});
});