forked from ngokevin/redux-raven-middleware
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.es6
93 lines (76 loc) · 2.88 KB
/
test.es6
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
82
83
84
85
86
87
88
89
90
91
92
93
import assert from 'assert';
import sinon from 'sinon';
const stubStore = {
getState: () => ({test: 'test'})
};
const mockNextHandler = action => {
if (action.error) {
throw new Error('Test Error');
}
};
describe('createRavenMiddleware', () => {
const createRavenMiddleware = require('./index');
const Raven = require('raven-js');
const ravenSpy = sinon.spy(Raven, 'captureException');
beforeEach(() => {
sinon.stub(Raven, 'config', () => {
return {install: () => {}}
});
});
afterEach(() => {
Raven.config.restore();
ravenSpy.reset();
});
it('returns middleware', () => {
assert.equal(createRavenMiddleware.constructor, Function);
});
it('middleware returns nextHandler', () => {
const nextHandler = createRavenMiddleware('abc')(stubStore);
assert.equal(nextHandler.constructor, Function);
});
it('nextHandler returns actionHandler', () => {
const actionHandler = createRavenMiddleware('abc')(stubStore)();
assert.equal(actionHandler.constructor, Function);
assert.equal(actionHandler.length, 1);
});
it('configures Raven', done => {
Raven.config.restore();
sinon.stub(Raven, 'config', (dsn, cfg) => {
assert.equal(dsn, 'abc');
assert.deepEqual(cfg, {tags: {test: 'test'}});
done();
return {install: () => {}};
});
createRavenMiddleware('abc', {tags: {test: 'test'}})
});
it('reports error', () => {
createRavenMiddleware('abc')(stubStore)(mockNextHandler)({error: true});
assert.equal(ravenSpy.args[0][0].constructor, Error);
assert.equal(ravenSpy.args[0][0].message, 'Test Error');
assert.deepEqual(ravenSpy.args[0][1].extra.action, {error: true});
assert.deepEqual(ravenSpy.args[0][1].extra.state, {test: 'test'});
});
it('reports error with transformed state and action', () => {
const actionTransformer = action => action.error
const stateTransformer = state => state.test
const cfg = {}
const opts = { actionTransformer, stateTransformer }
createRavenMiddleware('abc', cfg, opts)(stubStore)(mockNextHandler)({error: true});
assert.equal(ravenSpy.args[0][0].constructor, Error);
assert.equal(ravenSpy.args[0][0].message, 'Test Error');
assert.equal(ravenSpy.args[0][1].extra.action, true);
assert.equal(ravenSpy.args[0][1].extra.state, 'test');
});
it('logs via custom logger', () => {
const logger = sinon.spy()
const cfg = {}
const opts = { logger }
createRavenMiddleware('abc', cfg, opts)(stubStore)(mockNextHandler)({error: true});
assert.equal(ravenSpy.args[0][0].constructor, Error);
assert.equal(ravenSpy.args[0][0].message, 'Test Error');
assert.deepEqual(ravenSpy.args[0][1].extra.action, {error: true});
assert.deepEqual(ravenSpy.args[0][1].extra.state, {test: 'test'});
assert(logger.calledOnce, 'logs one message')
assert.equal(logger.args[0][0], ravenSpy.args[0][0])
});
});