UNPKG

1.84 kBJavaScriptView Raw
1import { expect } from 'chai';
2import sinon from 'sinon';
3
4import renderMiddleware from '../../../src/middleware/render';
5
6describe('render', () => {
7 let next;
8 let store;
9 let result;
10 let createStore;
11 let render;
12 let error;
13 let request;
14 let req;
15 let res;
16 let err;
17 let app;
18
19 beforeEach(() => {
20 next = {
21 request: sinon.spy(),
22 error: sinon.spy(),
23 };
24 store = { };
25 result = { };
26 createStore = sinon.spy(() => store);
27 render = sinon.spy(() => result);
28 request = sinon.spy();
29 error = sinon.spy();
30 req = { };
31 res = { };
32 err = new Error();
33 app = renderMiddleware({ createStore, render, request, error })(next);
34 });
35
36 describe('with a request', () => {
37 it('it should call request', () => {
38 app.request(req, res);
39 expect(request).to.have.been.calledWith(req, res, store);
40 });
41
42 it('should call render', (done) => {
43 app.request(req, res).then(() => {
44 expect(render).to.have.been.calledWith(req, res, store);
45 done();
46 });
47 });
48
49 it('it should call next request', (done) => {
50 app.request(req, res).then(() => {
51 expect(next.request).to.have.been.calledWith(req, res);
52 done();
53 });
54 });
55 });
56
57 describe('with an error', () => {
58 it('it should call error', () => {
59 app.error(req, res, err);
60 expect(error).to.have.been.calledWith(req, res, err, store);
61 });
62
63 it('it should call next request', (done) => {
64 app.error(err, req, res).then(() => {
65 expect(next.request).to.have.been.calledWith(req, res);
66 done();
67 });
68 });
69 });
70
71 it('should attach result returned from render to req', () => {
72 app.request(req, res).then(() => {
73 expect(req.render).to.equal(result);
74 expect(req.body).to.equal(result.markup);
75 });
76 });
77});