UNPKG

1.55 kBJavaScriptView Raw
1import { expect } from 'chai';
2import sinon from 'sinon';
3
4import status from '../../../src/middleware/status';
5
6describe('status', () => {
7 let res;
8 let req;
9 let next;
10
11 beforeEach(() => {
12 req = {};
13 res = {};
14 next = {
15 request: sinon.spy(),
16 error: sinon.spy(),
17 };
18 });
19
20 it('should call next request', (done) => {
21 const app = status()(next);
22 app.request(req, res).then(() => {
23 expect(next.request).to.be.calledWith(req, res);
24 done();
25 });
26 });
27
28 it('should set res.statusCode', (done) => {
29 const app = status(200)(next);
30 app.request(req, res).then(() => {
31 expect(res.statusCode).to.be.equal(200);
32 done();
33 });
34 });
35
36 it('should call function argument', (done) => {
37 const handler = sinon.spy(() => 200);
38 const app = status(handler)(next);
39 app.request(req, res).then(() => {
40 expect(handler).to.be.calledWith(req);
41 expect(res.statusCode).to.be.equal(200);
42 done();
43 });
44 });
45
46 it('should not set res.statusCode if value is falsy', (done) => {
47 const app = status(false)(next);
48 app.request(req, res).then(() => {
49 expect(res.statusCode).to.be.be.undefined;
50 done();
51 });
52 });
53
54 it('should call next error', (done) => {
55 const err = new Error();
56 const handler = () => {
57 throw err;
58 };
59 const app = status(handler)(next);
60 app.request(req, res).then(() => {
61 expect(next.error).to.be.calledWith(err, req, res);
62 expect(res.statusCode).to.be.be.undefined;
63 done();
64 });
65 });
66});