From 023bd6f38f06a20dfedcbb6bb5291811dc9f6ec9 Mon Sep 17 00:00:00 2001 From: Sergey Tatarintsev Date: Thu, 17 Nov 2016 17:09:03 +0100 Subject: [PATCH] test: add test for router --- lib/html-template.js | 2 +- lib/router.js | 3 +- test/html-template.js | 2 +- test/router.js | 149 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 3 deletions(-) create mode 100644 test/router.js diff --git a/lib/html-template.js b/lib/html-template.js index 660b697..5e4cc4f 100644 --- a/lib/html-template.js +++ b/lib/html-template.js @@ -35,4 +35,4 @@ function render(templateData) { ); } -module.exports = render; +exports.render = render; diff --git a/lib/router.js b/lib/router.js index 8c4eada..aaed1c5 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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(); @@ -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; diff --git a/test/html-template.js b/test/html-template.js index a156b9c..6f11546 100644 --- a/test/html-template.js +++ b/test/html-template.js @@ -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', () => { diff --git a/test/router.js b/test/router.js new file mode 100644 index 0000000..eba8639 --- /dev/null +++ b/test/router.js @@ -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 = '

It works!

'; + 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']) + ); + }); + }); +});