Skip to content

Commit

Permalink
Tests for beforeAll hook
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Sep 18, 2015
1 parent af3e64e commit baea069
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
[*.{md,htmlt}]
trim_trailing_whitespace = false
insert_final_newline = false

[*.json]
indent_size = 2
6 changes: 6 additions & 0 deletions test/fixtures/serve/beforeAll-hook/compose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = function (req, res, next) {
req.param2 = 'overwritten';
next();
};
1 change: 1 addition & 0 deletions test/fixtures/serve/beforeAll-hook/index.htmlt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= request.param1 %>, <%= request.param2 %>
72 changes: 72 additions & 0 deletions test/spec/beforeAll-hook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

var express = require('express');
var http = require('http');
var serveSpa = require('../../lib/index.js');
var path = require('path');
var rp = require('request-promise');


describe('Regarding the beforeAll hook, Serve-SPA', function () {

var server;

before(function (done) {
var app = express();
serveSpa(app, path.join(__dirname, '../fixtures/serve/'), {

beforeAll: function (req, res, next) {

switch (req.path) {
case '/beforeAll-hook/own':
res.send('own');
return;
case '/beforeAll-hook/redirect':
res.redirect('http://localhost:4000/sub1/');
return;
}

req.param1 = 'original param1';
req.param2 = 'original param2';
next();

}

});
server = http.createServer(app);
server.listen(4000, function () { done(); });
});

after(function () {
server.close();
});


it('should call the beforeAll middleware first', function () {

return rp('http://localhost:4000/beforeAll-hook')
.then(function (body) {
expect(body).to.equal('original param1, overwritten');
});

});

it('should allow serving its own response', function () {

return rp('http://localhost:4000/beforeAll-hook/own')
.then(function (body) {
expect(body).to.equal('own');
});

});

it('should allow a redirect', function () {

return rp('http://localhost:4000/beforeAll-hook/redirect')
.then(function (body) {
expect(body).to.equal('sub1');
});

});

});
7 changes: 7 additions & 0 deletions test/spec/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe('Regarding its initialization, Serve-SPA', function () {
initServeSpa(app, '/absolute.path', { require: false });
}).to.throw('The require option must be if type function.');

expect(function () {
initServeSpa(app, '/absolute.path', { beforeAll: false });
}).to.throw('The beforeAll option must be if type function.');

expect(function () {
initServeSpa(app, path.join(__dirname, '../fixtures/invalid/compose-export/'));
}).to.throw('The following composing module does not export a function: ' + path.join(__dirname, '../fixtures/invalid/compose-export/compose.js'));
Expand Down Expand Up @@ -65,6 +69,9 @@ describe('Regarding its initialization, Serve-SPA', function () {
'/async-compose',
'/async-compose/compose.js',
'/async-compose/index.htmlt',
'/beforeAll-hook',
'/beforeAll-hook/compose.js',
'/beforeAll-hook/index.htmlt',
'/compose-with-router',
'/compose-with-router/compose.js',
'/compose-with-router/index.htmlt',
Expand Down

0 comments on commit baea069

Please sign in to comment.