Skip to content

Commit

Permalink
Add locals object to response
Browse files Browse the repository at this point in the history
Re-implementation of #135
  • Loading branch information
Eugene Fidelin committed Feb 15, 2018
1 parent f18f00a commit baa05e5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -109,6 +109,7 @@ Where options is an object hash with any of the following values:

option | description | default value
------ | ----------- | -------------
`locals` | object that contains `response` local variables | `{}`
`eventEmitter` | event emitter used by `response` object | `mockEventEmitter`
`writableStream` | writable stream used by `response` object | `mockWritableStream`
`req` | Request object being responded to | null
Expand Down
12 changes: 1 addition & 11 deletions lib/express/mock-express.js
Expand Up @@ -8,18 +8,8 @@ var application = require('./mock-application');
var request = require('./mock-request');
var response = require('../mockResponse');


function createExpressResponse(options) {
var res = response.createResponse(options);

// http://expressjs.com/en/api.html#res.locals
res.locals = options && options.locals || {};

return res;
}

var expressResponse = {
createResponse: createExpressResponse
createResponse: response.createResponse
};

function createApplication() {
Expand Down
3 changes: 3 additions & 0 deletions lib/mockResponse.js
Expand Up @@ -66,6 +66,9 @@ function createResponse(options) {
mockResponse.statusCode = 200;
mockResponse.statusMessage = 'OK';

// http://expressjs.com/en/api.html#res.locals
mockResponse.locals = options.locals || {};

mockResponse.cookie = function(name, value, opt) {

mockResponse.cookies[name] = {
Expand Down
11 changes: 0 additions & 11 deletions test/lib/mockExpressResponse.spec.js
Expand Up @@ -2,12 +2,10 @@

var chai = require('chai');
var expect = chai.expect;
var sinon = require('sinon');
var sinonChai = require('sinon-chai');
chai.use(sinonChai);

var mockExpressResponse = require('../../lib/express/mock-express').response;
var mockRequest = require('../../lib/mockRequest');

describe('mockExpressResponse', function() {

Expand All @@ -26,14 +24,5 @@ describe('mockExpressResponse', function() {
expect(response).to.be.an('object');
});

it('should have a .locals property object', function() {
expect(response.locals).to.be.an('object');
});

it('should allow you to pass options.locals', function() {
var res = mockExpressResponse.createResponse({ locals: { test: true } });
expect(res.locals.test).to.equal(true);
});

});
});
7 changes: 5 additions & 2 deletions test/lib/mockResponse.spec.js
Expand Up @@ -19,7 +19,9 @@ describe('mockResponse', function() {
var response;

before(function() {
response = mockResponse.createResponse();
response = mockResponse.createResponse({
locals: {a: 'b'}
});
});

it('should return an object', function() {
Expand Down Expand Up @@ -133,7 +135,7 @@ describe('mockResponse', function() {
expect(response.prependListener).to.be.a('function');
});

it('should expose heler methods', function() {
it('should expose helper methods', function() {
expect(response).to.have.property('_isEndCalled');
expect(response._isEndCalled).to.be.a('function');

Expand Down Expand Up @@ -168,6 +170,7 @@ describe('mockResponse', function() {
it('shoud initialize with default options', function() {
expect(response.statusCode).to.equal(200);
expect(response.cookies).to.deep.equal({});
expect(response.locals).to.deep.equal({a: 'b'});
});

});
Expand Down

0 comments on commit baa05e5

Please sign in to comment.