shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
90 lines • 4.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("mocha");
var sinon = require("sinon");
var fsx = require("fs-extra");
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 add_model_handler_1 = require("./add-model.handler");
var compiler_context_spec_1 = require("../data/compiler.context.spec");
describe('AddModelHandler', function () {
var sandbox;
var eventService;
var logger;
beforeEach(function () {
app_composition_1.IoC.snapshot();
sandbox = sinon.createSandbox();
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(new models_1.WebsiteConfig());
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 html', function () {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
var stub = sandbox.stub(fsx, 'readJSON');
var subject = new add_model_handler_1.AddModelHandler();
subject.processEvent(new models_1.FileData("sample.js", "./sample.js")).then(function (_) {
(0, chai_1.expect)(stub.called).to.be.false;
});
});
});
it('processEvent should call raise "file-model-added" event', function () {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) {
sandbox.stub(fsx, 'readJSON').returns(Promise.resolve({}));
var stub = eventService.publish = sandbox.stub();
var subject = new add_model_handler_1.AddModelHandler();
subject.processEvent(new models_1.FileData("sample.html", "./sample.html")).then(function (_) {
(0, chai_1.expect)(stub.getCall(0).args[0]).to.equal('file-model-added');
});
});
});
it('processEvent should add shaman model details, if not provided', function () {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) {
sandbox.stub(fsx, 'readJSON').returns(Promise.resolve({}));
var stub = eventService.publish = sandbox.stub();
var subject = new add_model_handler_1.AddModelHandler();
subject.processEvent(new models_1.FileData("sample.html", "./sample.html")).then(function (_) {
var file = stub.getCall(0).args[1];
(0, chai_1.expect)(file.model.shaman.bundles.length).to.equal(0);
});
});
});
it('processEvent should warn user when file model does not exist', function () {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) {
sandbox.stub(fsx, 'readJSON').returns(Promise.reject());
var stub = logger.log = sandbox.stub();
var subject = new add_model_handler_1.AddModelHandler();
subject.processEvent(new models_1.FileData("sample.html", "./sample.html")).then(function (_) {
var logLevel = stub.getCall(0).args[1];
(0, chai_1.expect)(logLevel).to.equal(1);
});
});
});
it('processEvent should add shaman model details from file contents', function () {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) {
var model = new models_1.FileModelConfig();
model.bundles = [new models_1.Bundle()];
sandbox.stub(fsx, 'readJSON').returns(Promise.resolve({ shaman: model }));
var stub = eventService.publish = sandbox.stub();
var subject = new add_model_handler_1.AddModelHandler();
subject.processEvent(new models_1.FileData("sample.html", "./sample.html")).then(function (_) {
var file = stub.getCall(0).args[1];
(0, chai_1.expect)(file.model.shaman.bundles.length).to.equal(1);
});
});
});
});
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=add-model.handler.spec.js.map