UNPKG

shaman-website-compiler

Version:

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

107 lines 5.69 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 javascript_content_handler_1 = require("./javascript-content.handler"); var compiler_context_spec_1 = require("../data/compiler.context.spec"); describe('JavascriptContentHandler', function () { var sandbox; var config; var eventService; var logger; 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)(); 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); }); afterEach(function () { app_composition_1.IoC.restore(); sandbox.restore(); }); it('processEvent should do nothing if file is not js', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) { var subject = new javascript_content_handler_1.JavascriptContentHandler(); var stub = sandbox.stub(subject, 'minify'); subject.processEvent(new models_1.FileData("sample.css", "./sample.css")).then(function (_) { (0, chai_1.expect)(stub.called).to.be.false; done(); }); }); }); it('processEvent should not minify file if config.production = false', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = false; var subject = new javascript_content_handler_1.JavascriptContentHandler(); sandbox.stub(subject, 'minify').returns({ code: 'var $={}' }); var file = new models_1.FileData("sample.js", "./sample.js"); file.text = 'var $ = { }'; subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.js'); (0, chai_1.expect)(result.content).to.equal('var $ = { }'); done(); }); }); }); it('processEvent should handle minification errors', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = true; var subject = new javascript_content_handler_1.JavascriptContentHandler(); sandbox.stub(subject, 'minify').returns({ error: new Error("test") }); var file = new models_1.FileData("sample.js", "./sample.js"); subject.processEvent(file).catch(function (_) { return done(); }); }); }); it('processEvent should minify file if config.production = true', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = true; var subject = new javascript_content_handler_1.JavascriptContentHandler(); sandbox.stub(subject, 'minify').returns({ code: 'var $={}' }); var file = new models_1.FileData("sample.js", "./sample.js"); subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.js'); (0, chai_1.expect)(result.content).to.equal('var $={}'); done(); }); }); }); it('processEvent should set routePath to *.min.js if config.production = true', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) { config.production = true; var subject = new javascript_content_handler_1.JavascriptContentHandler(); sandbox.stub(subject, 'minify').returns({ code: 'var $={}' }); var file = new models_1.FileData("sample.js", "./sample.js"); subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.js'); (0, chai_1.expect)(result.routePath).to.equal('sample.min.js'); done(); }); }); }); it('processEvent should call raise "file-contents-added" event', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) { config.production = false; var subject = new javascript_content_handler_1.JavascriptContentHandler(); var stub = eventService.publish = sandbox.stub(); subject.processEvent(new models_1.FileData("sample.js", "./sample.js")).then(function (_) { (0, chai_1.expect)(stub.getCall(0).args[0]).to.equal('file-available'); done(); }); }); }); }); function addTestFileToContext(context) { var file = new models_1.FileData('sample.js', './sample.js'); file.available = true; context.models.files.add('sample.js', file); return context.saveChanges().then(function (_) { return (context); }); } //# sourceMappingURL=javascript-content.handler.spec.js.map