UNPKG

shaman-website-compiler

Version:

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

98 lines 5.03 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 css_content_handler_1 = require("./css-content.handler"); var compiler_context_spec_1 = require("../data/compiler.context.spec"); describe('CssContentHandler', 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 css', function (done) { (0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) { var subject = new css_content_handler_1.CssContentHandler(); var stub = sandbox.stub(subject.minify, 'minify'); 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 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 css_content_handler_1.CssContentHandler(); sandbox.stub(subject.minify, 'minify').returns('.css{}'); var file = new models_1.FileData("sample.css", "./sample.css"); file.text = '.css { }'; subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.css'); (0, chai_1.expect)(result.content).to.equal('.css { }'); 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 css_content_handler_1.CssContentHandler(); sandbox.stub(subject.minify, 'minify').returns({ styles: '.css{}' }); var file = new models_1.FileData("sample.css", "./sample.css"); subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.css'); (0, chai_1.expect)(result.content).to.equal('.css{}'); done(); }); }); }); it('processEvent should set routePath to *.min.css 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 css_content_handler_1.CssContentHandler(); sandbox.stub(subject.minify, 'minify').returns({ styles: '.css{}' }); var file = new models_1.FileData("sample.css", "./sample.css"); subject.processEvent(file).then(function (_) { var result = context.models.files.find('sample.css'); (0, chai_1.expect)(result.routePath).to.equal('sample.min.css'); 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 css_content_handler_1.CssContentHandler(); var stub = eventService.publish = sandbox.stub(); subject.processEvent(new models_1.FileData("sample.css", "./sample.css")).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.css', './sample.css'); file.available = true; context.models.files.add('sample.css', file); return context.saveChanges().then(function (_) { return (context); }); } //# sourceMappingURL=css-content.handler.spec.js.map