UNPKG

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