UNPKG

shaman-website-compiler

Version:

Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.

145 lines 7.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("mocha"); var sinon = require("sinon"); var chai_1 = require("chai"); var app_composition_1 = require("../composition/app.composition"); var models_1 = require("../models"); var ts_auto_mock_1 = require("ts-auto-mock"); var html_content_handler_1 = require("./html-content.handler"); var compiler_context_spec_1 = require("../data/compiler.context.spec"); describe('HtmlContentHandler', function () { var sandbox; var config; var eventService; var logger; var handlebarsService; var queryAdapter; beforeEach(function () { app_composition_1.IoC.snapshot(); sandbox = sinon.createSandbox(); config = new models_1.WebsiteConfig(); eventService = (0, ts_auto_mock_1.createMock)(); logger = (0, ts_auto_mock_1.createMock)(); handlebarsService = (0, ts_auto_mock_1.createMock)(); queryAdapter = (0, ts_auto_mock_1.createMock)(); app_composition_1.IoC.bind(app_composition_1.TYPES.WebsiteConfig).toConstantValue(config); app_composition_1.IoC.bind(app_composition_1.TYPES.CompilerEvents).toConstantValue(eventService); app_composition_1.IoC.bind(app_composition_1.TYPES.Logger).toConstantValue(logger); app_composition_1.IoC.bind(app_composition_1.TYPES.HandlebarsService).toConstantValue(handlebarsService); app_composition_1.IoC.bind(app_composition_1.TYPES.QueryAdapter).toConstantValue(queryAdapter); }); afterEach(function () { app_composition_1.IoC.restore(); sandbox.restore(); }); it('processEvent should do nothing if file is not css', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) { var subject = new html_content_handler_1.HtmlContentHandler(); var stub = eventService.publish = sandbox.stub(); subject.processEvent(new models_1.FileData("sample.js", "./sample.js")).then(function (_) { (0, chai_1.expect)(stub.called).to.be.false; done(); }); }); }); it('processEvent should update file contents', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { var subject = new html_content_handler_1.HtmlContentHandler(); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); var file = new models_1.FileData('sample.html', './sample.html'); file.model = { shaman: {} }; subject.processEvent(file).then(function (_) { var file = context.models.files.find("sample.html"); (0, chai_1.expect)(file.content).to.equal('<html>\r\n</html>'); done(); }); }); }); it('processEvent should minify file contents', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = true; var subject = new html_content_handler_1.HtmlContentHandler(); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); sandbox.stub(subject, 'minify').returns('<html></html>'); var file = new models_1.FileData('sample.html', './sample.html'); file.model = { shaman: {} }; subject.processEvent(file).then(function (_) { var file = context.models.files.find("sample.html"); (0, chai_1.expect)(file.content).to.equal('<html></html>'); done(); }); }); }); it('processEvent should call raise "file-available" event', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) { config.production = true; var subject = new html_content_handler_1.HtmlContentHandler(); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); sandbox.stub(subject, 'minify').returns('<html></html>'); var file = new models_1.FileData('sample.html', './sample.html'); file.model = { shaman: {} }; var stub = eventService.publish = sandbox.stub(); subject.processEvent(file).then(function (_) { (0, chai_1.expect)(stub.getCall(0).args[0]).to.equal('file-available'); done(); }); }); }); it('processEvent should warn user if dynamic file has no queries', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { var subject = new html_content_handler_1.HtmlContentHandler(); var stub = logger.log = sandbox.stub(); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); var file = new models_1.FileData('sample.html', './sample.html'); file.model = { shaman: { dynamic: { path: '', name: '' }, query: [] } }; subject.processEvent(file).then(function (_) { var logLevel = stub.getCall(0).args[1]; (0, chai_1.expect)(logLevel).to.equal(1); done(); }); }); }); it('processEvent should warn user if dynamic query returns empty', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { var subject = new html_content_handler_1.HtmlContentHandler(); var stub = logger.log = sandbox.stub(); queryAdapter.run = sandbox.stub().returns(Promise.resolve([])); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); var file = new models_1.FileData('sample.html', './sample.html'); var query = new models_1.QueryModel(); query.dynamic = true; file.model = { shaman: { dynamic: { path: '', name: '' }, query: [query] } }; subject.processEvent(file).then(function (_) { var logLevel = stub.getCall(0).args[1]; (0, chai_1.expect)(logLevel).to.equal(1); done(); }); }); }); it('processEvent should minify dynamic routes', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = true; var subject = new html_content_handler_1.HtmlContentHandler(); queryAdapter.run = sandbox.stub().returns(Promise.resolve([{ title: 'a.html' }])); handlebarsService.renderTemplate = sandbox.stub().returns('<html>\r\n</html>'); sandbox.stub(subject, 'minify').returns('<html></html>'); var file = new models_1.FileData('sample.html', './sample.html'); var query = new models_1.QueryModel(); query.dynamic = true; file.model = { shaman: { dynamic: { path: '', name: 'title' }, query: [query] } }; subject.processEvent(file).then(function (_) { var route = context.models.dynamicRoutes.find('a.html'); (0, chai_1.expect)(route.content).to.equal('<html></html>'); done(); }); }); }); }); function addTestFileToContext(context) { var file = new models_1.FileData('sample.html', './sample.html'); file.available = true; context.models.files.add('sample.html', file); return context.saveChanges().then(function (_) { return (context); }); } //# sourceMappingURL=html-content.handler.spec.js.map