shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
103 lines • 5.55 kB
JavaScript
;
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 process_model_handler_1 = require("./process-model.handler");
var compiler_context_spec_1 = require("../data/compiler.context.spec");
describe('ProcessModelHandler', function () {
var sandbox;
var config;
var eventService;
var logger;
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)();
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.QueryAdapter).toConstantValue(queryAdapter);
});
afterEach(function () {
app_composition_1.IoC.restore();
sandbox.restore();
});
it('processEvent should do nothing if file is not html', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
var subject = new process_model_handler_1.ProcessModelHandler();
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 content in database', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) {
var subject = new process_model_handler_1.ProcessModelHandler();
var file = new models_1.FileData("sample.html", "./sample.html");
var bundle = new models_1.Bundle({ name: 'a', type: 'js', files: ['a.js'], path: 'a.js' });
file.model = { shaman: { bundles: [bundle], query: [] } };
subject.processEvent(file).then(function (_) {
(0, chai_1.expect)(context.models.bundles.filter(function (b) { return !!b; }).length).to.equal(1);
done();
});
});
});
it('processEvent should set bundle.extension to *.min.js when 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 process_model_handler_1.ProcessModelHandler();
var file = new models_1.FileData("sample.html", "./sample.html");
var bundle = new models_1.Bundle({ name: 'a', type: 'js', files: ['a.js'], path: 'a.js' });
file.model = { shaman: { bundles: [bundle], query: [] } };
subject.processEvent(file).then(function (_) {
var bundle = context.models.bundles.find('a');
(0, chai_1.expect)(bundle.extension).to.equal('min.js');
done();
});
});
});
it('processEvent should update content in database', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) {
var subject = new process_model_handler_1.ProcessModelHandler();
queryAdapter.run = sandbox.stub().returns(Promise.resolve({ success: true }));
var file = new models_1.FileData("sample.html", "./sample.html");
var query = { name: 'q1', path: '' };
file.model = { shaman: { bundles: [], query: [query] } };
subject.processEvent(file).then(function (_) {
var result = context.models.files.find("sample.html");
(0, chai_1.expect)(result.query.q1.success).to.be.true;
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 (_) {
var subject = new process_model_handler_1.ProcessModelHandler();
queryAdapter.run = sandbox.stub().returns(Promise.resolve({ success: true }));
var file = new models_1.FileData("sample.html", "./sample.html");
file.model = { shaman: { bundles: [], query: [] } };
var stub = eventService.publish = sandbox.stub();
subject.processEvent(file).then(function (_) {
(0, chai_1.expect)(stub.getCall(0).args[0]).to.equal('file-model-processed');
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=process-model.handler.spec.js.map