UNPKG

1.15 kBJavaScriptView Raw
1import { expect } from 'chai';
2import sinon from 'sinon';
3
4import redirect from '../../../src/middleware/redirect';
5
6describe('redirect', () => {
7 let res;
8 let req;
9 let next;
10
11 beforeEach(() => {
12 req = {};
13 res = {
14 writeHead: sinon.spy(),
15 end: sinon.spy(),
16 };
17 next = {
18 request: sinon.spy(),
19 error: sinon.spy(),
20 };
21 });
22
23 it('should not call next request', () => {
24 const app = redirect('/foo')(next);
25 app.request(req, res);
26 expect(next.request).to.be.not.called;
27 expect(res.end).to.be.calledOnce;
28 });
29
30 it('should set the status code and url', () => {
31 const app = redirect('/foo')(next);
32 app.request(req, res);
33 expect(res.writeHead).to.be.calledWithMatch(302, {
34 Location: '/foo',
35 });
36 });
37
38 it('should set the status code and url for functions', () => {
39 const app = redirect(() => '/foo')(next);
40 app.request(req, res);
41 expect(res.writeHead).to.be.calledWithMatch(302, {
42 Location: '/foo',
43 });
44 });
45
46 it('should fail for invalid parameters', () => {
47 expect(() => {
48 redirect(false);
49 }).to.throw(TypeError);
50 });
51});