This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
117 lines (106 loc) · 3.58 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const sinon = require('sinon');
const {expect} = require('chai');
const {action, setupAction} = require('./index');
let fakeNext;
describe('index.js', () => {
describe('action', () => {
beforeEach(() => {
fakeNext = err => console.log(err);
});
it('should return 500 in case of error', async () => {
// setup
fakeNext = sinon.fake();
const error = new Error('any error');
// exercise
await action(async () => {
throw error;
})({}, {}, fakeNext);
// verify
expect(fakeNext.lastArg).to.be.equal(error);
});
it('should return 400 validation error', async () => {
// setup
const fakeJson = sinon.fake();
const fakeRes = {
status: sinon.fake.returns({json: fakeJson})
};
const fakeValidationErrors = [{id: 'error1'}];
const fakeReq = {_validationErrors: fakeValidationErrors};
// exercise
await action(async () => {
})(fakeReq, fakeRes, fakeNext);
// verify
expect(fakeRes.status.lastArg).to.be.equal(400);
expect(fakeJson.lastArg).to.be.deep.equal({
"data": fakeValidationErrors,
"message": "Bad Request"
});
});
it('should return 404 if return is null', async () => {
// setup
const fakeJson = sinon.fake();
const fakeRes = {
status: sinon.fake.returns({json: fakeJson})
};
const fakeReq = {};
// exercise
await action(async () => {
return null
})(fakeReq, fakeRes, fakeNext);
// verify
expect(fakeRes.status.lastArg).to.be.equal(404);
expect(fakeJson.lastArg).to.be.deep.equal({
"message": "Not found"
});
});
it('should return 200 if data is valid', async () => {
// setup
const fakeJson = sinon.fake();
const fakeRes = {
status: sinon.fake.returns({json: fakeJson})
};
const fakeReq = {};
// exercise
await action(async () => {
return {id: 1, name: 'bulbasaur'}
})(fakeReq, fakeRes, fakeNext);
// verify
expect(fakeRes.status.lastArg).to.be.equal(200);
expect(fakeJson.lastArg).to.be.deep.equal({
id: 1,
name: "bulbasaur",
});
});
});
describe('setupAction', () => {
beforeEach(() => {
fakeNext = err => console.log(err);
});
it('it should enable newrelic', async () => {
setupAction({
config: {
package: {},
newrelic: {enabled: true},
}
});
let localNewrelic = null;
await action(async ({newrelic}) => {
localNewrelic = newrelic;
})({route: '/xpto'}, {}, ()=>{});
expect(localNewrelic).to.exist;
});
it('it should enable raven', async () => {
setupAction({
config: {
package: {},
sentry: {enabled: true},
}
});
let localRaven = null;
await action(async ({raven}) => {
localRaven = raven;
})({}, {}, ()=>{});
expect(localRaven).to.exist;
});
});
});