Skip to content

Commit 71510ad

Browse files
committed
test(applyMiddleware): added coverage
1 parent 2cac815 commit 71510ad

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@
6464
"cz-conventional-changelog": "^1.2.0",
6565
"eslint": "^3.7.1",
6666
"eslint-plugin-ava": "^4.0.0",
67+
"fetch-mock": "^5.5.0",
6768
"husky": "^0.11.9",
6869
"nyc": "^9.0.1",
6970
"rimraf": "^2.5.4",
71+
"sinon": "^1.17.6",
7072
"validate-commit-msg": "^2.8.2"
7173
},
7274
"ava": {
@@ -85,6 +87,19 @@
8587
],
8688
"babel": "inherit"
8789
},
90+
"nyc": {
91+
"include": [
92+
"src/**/*.js"
93+
],
94+
"exclude": [
95+
"src/**/*spec.js"
96+
],
97+
"reporter": [
98+
"html",
99+
"text",
100+
"text-summary"
101+
]
102+
},
88103
"config": {
89104
"commitizen": {
90105
"path": "./node_modules/cz-conventional-changelog"

src/error.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function APIError(code, message) {
66
Object.setPrototypeOf(APIError.prototype, Error.prototype);
77
APIError.prototype.name = 'APIError';
88
APIError.prototype.toString = function toString() {
9-
return this.code + ' - ' + this.message;
9+
return this.message ? this.code + ' - ' + this.message : String(this.code);
1010
};
1111

1212
export default APIError;

test/applyMiddlewate.spec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import test from 'ava';
2+
import sinon from 'sinon';
3+
import applyMiddleware from '../src/applyMiddleware';
4+
5+
test('applyTo is required', t => {
6+
t.throws(() => applyMiddleware());
7+
});
8+
9+
test('applyTo is not a function', t => {
10+
const applyTo = 'oops';
11+
t.throws(() => applyMiddleware(applyTo));
12+
});
13+
14+
test('no middleware returns default params', t => {
15+
const applyTo = a => a;
16+
const spyApplyTo = sinon.spy(applyTo);
17+
18+
t.deepEqual(applyMiddleware(spyApplyTo), {});
19+
t.true(spyApplyTo.calledOnce);
20+
t.true(spyApplyTo.calledWithExactly({}));
21+
});
22+
23+
test('no functional middleware returns default params', t => {
24+
const applyTo = sinon.spy(a => a);
25+
const middlewares = [
26+
'test', undefined, {}, [], 123, null, NaN, /^/
27+
];
28+
29+
t.deepEqual(applyMiddleware(applyTo, middlewares), {});
30+
t.true(applyTo.calledOnce);
31+
t.true(applyTo.calledWithExactly({}));
32+
});
33+
34+
test('one middleware', t => {
35+
const applyTo = sinon.spy(a => a);
36+
const middleware = sinon.spy(a => a);
37+
38+
const options = { options: 'options' };
39+
const params = { params: 'params' };
40+
const resourceId = 'resourceId';
41+
const method = 'method';
42+
43+
t.deepEqual(applyMiddleware(applyTo, [ middleware ], options, params, resourceId, method), params);
44+
t.true(applyTo.calledOnce);
45+
t.true(applyTo.calledWithExactly(params, options, params, resourceId, method));
46+
t.true(middleware.calledOnce);
47+
48+
const middlewareSpyCall = middleware.getCall(0);
49+
t.true(middlewareSpyCall.args.length === 1);
50+
t.true(middlewareSpyCall.args[0] instanceof Function);
51+
t.falsy(middlewareSpyCall.thisValue);
52+
t.true(middlewareSpyCall.args[0].prototype === undefined);
53+
});
54+
55+
test('multiple middlewares', t => {
56+
const applyTo = sinon.spy(a => a);
57+
const middleware1 = sinon.spy(a => a);
58+
const middleware2 = sinon.spy(a => a);
59+
60+
const options = { options: 'options' };
61+
const params = { params: 'params' };
62+
const resourceId = 'resourceId';
63+
const method = 'method';
64+
65+
t.deepEqual(applyMiddleware(applyTo, [ middleware1, middleware2 ], options, params, resourceId, method), params);
66+
t.true(applyTo.calledOnce);
67+
t.true(applyTo.calledWithExactly(params, options, params, resourceId, method, options, params, resourceId, method));
68+
t.true(middleware1.calledOnce);
69+
t.true(middleware2.calledOnce);
70+
71+
const middleware1SpyCall = middleware1.getCall(0);
72+
t.true(middleware1SpyCall.args.length === 1);
73+
t.true(middleware1SpyCall.args[0] instanceof Function);
74+
t.falsy(middleware1SpyCall.thisValue);
75+
t.true(middleware1SpyCall.args[0].prototype === undefined);
76+
77+
const middleware2SpyCall = middleware2.getCall(0);
78+
t.true(middleware2SpyCall.args.length === 1);
79+
t.true(middleware2SpyCall.args[0] instanceof Function);
80+
t.falsy(middleware2SpyCall.thisValue);
81+
t.true(middleware2SpyCall.args[0].prototype === undefined);
82+
});

test/error.spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import APIError from '../src/error';
33

44
test.beforeEach(t => {
55
t.context.error = new APIError(404, 'Not Found');
6+
t.context.errorNoMessage = new APIError(404);
67
});
78

89
test('instance of Error', t => {
@@ -14,5 +15,6 @@ test('instance of APIError', t => {
1415
});
1516

1617
test('to string', t => {
17-
t.is(t.context.error.toString(), '404 - Not Found')
18+
t.is(t.context.error.toString(), '404 - Not Found', 'has message');
19+
t.is(t.context.errorNoMessage.toString(), '404', 'No message');
1820
});

0 commit comments

Comments
 (0)