forked from PlatziDev/redux-catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
61 lines (59 loc) · 1.87 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
const middleware = require('./index');
const assert = require('assert');
const store = {
getState: () => 'state',
dispatch: () => 'dispatch',
};
describe('ErrorMiddleware', () => {
describe('success', () => {
it('should pass the action through successfully', async () => {
const next = action => action;
const action = {
type: 'FAKE_ACTION',
};
const errorHandler = (error, getState, action, dispatch) => {};
const result = await middleware(errorHandler)(store)(next)(action);
assert.deepEqual(result, { type: 'FAKE_ACTION' });
});
});
describe('catch synchronous error', () => {
it('should catch error from a synchronous dispatch', async () => {
// SYNC ERROR
const next = action => {
throw new Error('error');
};
const action = {
type: 'FAKE_ACTION',
};
const errorHandler = (error, getState, action, dispatch) => {
assert.equal(error.message, 'error');
assert.equal(getState(), 'state');
assert.deepEqual(action, {
type: 'FAKE_ACTION',
});
assert.equal(dispatch(), 'dispatch');
};
const result = await middleware(errorHandler)(store)(next)(action);
});
});
describe('catch asynchronous error', () => {
it('should catch error from an asynchronous dispatch', async () => {
// ASYNC ERROR
const next = async action => {
throw new Error('error');
};
const action = {
type: 'FAKE_ACTION',
};
const errorHandler = (error, getState, action, dispatch) => {
assert.equal(error.message, 'error');
assert.equal(getState(), 'state');
assert.deepEqual(action, {
type: 'FAKE_ACTION',
});
assert.equal(dispatch(), 'dispatch');
};
const result = await middleware(errorHandler)(store)(next)(action);
});
});
});