Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
test: add test for router
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Tatarintsev committed Nov 17, 2016
1 parent 0d27769 commit 023bd6f
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/html-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ function render(templateData) {
);
}

module.exports = render;
exports.render = render;
3 changes: 2 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function middleware(routeBuilder) {
log('this is a test page url');
const templateData = routeBuilder.getTemplateDataFromUrl(req.path, commonAssets);
log('template data', templateData);
res.send(htmlTemplate(templateData));
res.send(htmlTemplate.render(templateData));
}

next();
Expand All @@ -48,6 +48,7 @@ function readCommonAssets(webpackStats) {
webpackStats.toJson().assetsByChunkName,
value => /\.bundle.js$/.test(value)
);

return Object.keys(commonAssetsObj).reduce((result, item) => {
result.push(assetsRoot() + commonAssetsObj[item]);
return result;
Expand Down
2 changes: 1 addition & 1 deletion test/html-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('html template', () => {
jsList: [],
cssList: []
});
return jsdom.jsdom(htmlTemplate(templateData));
return jsdom.jsdom(htmlTemplate.render(templateData));
}

it('should have a test name as a title', () => {
Expand Down
149 changes: 149 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
'use strict';

const router = require('../lib/router');
const sinon = require('sinon');
const assert = require('chai').assert;

const RouteBuilder = require('../lib/route-builder');
const htmlTemplate = require('../lib/html-template');

describe('router', () => {
const sandbox = sinon.sandbox.create();

afterEach(() => {
sandbox.restore();
});

describe('testPathToChunkName', () => {
it('should replace extension with .bundle.js', () => {
assert.equal(
router.testPathToChunkName('file.js'),
'file.bundle.js'
);
});

it('should replace path delimeters with dashes', () => {
assert.equal(
router.testPathToChunkName('path/to/file.js'),
'path-to-file.bundle.js'
);
});

it('should urlencode the value', () => {
assert.equal(
router.testPathToChunkName('file with spaces.js'),
'file%20with%20spaces.bundle.js'
);
});
});

describe('testPathToChunkUrl', () => {
it('should return root-relative chunk url', () => {
assert.equal(
router.testPathToChunkUrl('file.js'),
'/assets/file.bundle.js'
);
});
});

describe('middleware', () => {
let routeBuilder;

function requestFor(path) {
return {
path: path
};
}

function response() {
return {
locals: {},
send: () => {}
};
}

function callMiddleware(params) {
params = params || {};
const req = params.req || requestFor('/');
const res = params.res || response();
const next = params.next || (() => {});

const middleware = router.middleware(routeBuilder);
middleware(req, res, next);
}

beforeEach(() => {
routeBuilder = sinon.createStubInstance(RouteBuilder);
});

it('should call `next` if url is unknown', () => {
const url = '/some/path';
routeBuilder.isUrlRegistered.withArgs(url).returns(false);
const next = sandbox.spy().named('next');

callMiddleware({
req: requestFor(url),
next: next
});

assert.calledOnce(next);
});

function setupSuccessResponse(url, html) {
html = html || '';

const data = {};

routeBuilder.isUrlRegistered
.withArgs(url)
.returns(true);
routeBuilder.getTemplateDataFromUrl
.withArgs(url)
.returns(data);
sandbox.stub(htmlTemplate, 'render')
.withArgs(data)
.returns(html);
}

it('should render html template if url matches', () => {
const url = '/some/path';
const html = '<h1>It works!</h1>';
const res = response();
sandbox.spy(res, 'send');
setupSuccessResponse(url, html);

callMiddleware({
req: requestFor(url),
res: res
});

assert.calledWith(res.send, html);
});

it('should pick webpacks assets', () => {
const url = '/some/path';
const res = response();

setupSuccessResponse(url);
res.locals.webpackStats = {
toJson: () => ({
assetsByChunkName: {
'test.bundle.js': 'path/to/test.bundle.js',
'common.js': 'path/to/common.js'
}
})
};

callMiddleware({
req: requestFor(url),
res: res
});

assert.calledWith(
routeBuilder.getTemplateDataFromUrl,
sinon.match.any,
sinon.match(['/assets/path/to/common.js'])
);
});
});
});

0 comments on commit 023bd6f

Please sign in to comment.