shaman-website-compiler
Version:
Compile raw HTML, CSS and Javascript into the smallest possible, SEO friendly website.
195 lines • 9.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
require("mocha");
var chai_1 = require("chai");
var sinon = require("sinon");
var _fsx = require("fs-extra");
var ts_auto_mock_1 = require("ts-auto-mock");
var app_composition_1 = require("./composition/app.composition");
var models_1 = require("./models");
var compiler_context_spec_1 = require("./data/compiler.context.spec");
var event_service_1 = require("./services/event.service");
var website_1 = require("./website");
describe('WebsiteRouter', function () {
var sandbox;
var config;
var compiler;
var server;
beforeEach(function () {
app_composition_1.IoC.snapshot();
sandbox = sinon.createSandbox();
config = new models_1.WebsiteConfig();
compiler = (0, ts_auto_mock_1.createMock)();
server = (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.WebsiteCompiler).toConstantValue(compiler);
app_composition_1.IoC.bind(app_composition_1.TYPES.WebsiteServer).toConstantValue(server);
app_composition_1.IoC.bind(app_composition_1.TYPES.CompilerEvents).toConstantValue(new event_service_1.EventService());
app_composition_1.IoC.bind(app_composition_1.TYPES.Logger).toConstantValue((0, ts_auto_mock_1.createMock)());
sandbox.stub(_fsx, 'ensureFile').returns(Promise.resolve());
});
afterEach(function () {
app_composition_1.IoC.restore();
sandbox.restore();
});
it('Website should not output files if config.output = false', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = false;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var outputStub = sandbox.stub(_fsx, 'outputFile').returns(Promise.resolve());
var subject = new website_1.Website();
subject.build().then(function (_) {
(0, chai_1.expect)(outputStub.called).not.to.be.true;
done();
});
});
});
it('Website should output files', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = './output';
config.serve = false;
var route = new models_1.Route("index.html", "<html></html>", "html");
compiler.compile = sandbox.stub().returns(Promise.resolve([route]));
var outputStub = sandbox.stub(_fsx, 'outputFile').returns(Promise.resolve());
var subject = new website_1.Website();
subject.build().then(function (_) {
(0, chai_1.expect)(outputStub.called).to.be.true;
done();
});
});
});
it('Website should output asset files', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestAssetFileToContext).then(function (_) {
config.output = './output';
config.serve = false;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var copyStub = sandbox.stub(_fsx, 'copy').returns(Promise.resolve());
var subject = new website_1.Website();
subject.build().then(function (_) {
(0, chai_1.expect)(copyStub.called).to.be.true;
done();
});
});
});
it('Website should not start server if config.serve = false', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = false;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var outputStub = server.start = sandbox.stub();
var subject = new website_1.Website();
subject.build().then(function (_) {
(0, chai_1.expect)(outputStub.called).not.to.be.true;
done();
});
});
});
it('Website should not start server if server is already started', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = true;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
server.listening = true;
var outputStub = server.start = sandbox.stub();
var subject = new website_1.Website();
sandbox.stub(subject, 'gaze');
subject.build().then(function (_) {
(0, chai_1.expect)(outputStub.called).not.to.be.true;
done();
});
});
});
it('Website should start server', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = true;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var outputStub = server.start = sandbox.stub();
var subject = new website_1.Website();
sandbox.stub(subject, 'gaze');
subject.build().then(function (_) {
(0, chai_1.expect)(outputStub.called).to.be.true;
done();
});
});
});
it('Website should not watch for file changes if config.serve = false', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = false;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var subject = new website_1.Website();
var gazeStub = sandbox.stub(subject, 'gaze');
subject.build().then(function (_) {
(0, chai_1.expect)(gazeStub.called).not.to.be.true;
done();
});
});
});
it('Website should not add listener to watcher if gaze returns error', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(function (_) {
config.output = null;
config.serve = true;
compiler.compile = sandbox.stub().returns(Promise.resolve([]));
var subject = new website_1.Website();
var watcherStub = sandbox.stub();
sandbox.stub(subject, 'gaze').yields(new Error("test error"), watcherStub);
subject.build().then(function (_) {
(0, chai_1.expect)(watcherStub.called).not.to.be.true;
done();
});
});
});
it('Website should not add listener to watcher if gaze returns error', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (_) {
config.output = null;
config.serve = true;
var route = new models_1.Route("sample.html", "<html></html>", "html");
compiler.compile = sandbox.stub().returns(Promise.resolve([route]));
var subject = new website_1.Website();
var watcher = { on: sandbox.stub().yields("sample.html") };
sandbox.stub(subject, 'gaze').yields(null, watcher);
var fileChangedStub = sandbox.stub(subject, 'fileChanged');
subject.build().then(function (_) {
(0, chai_1.expect)(fileChangedStub.called).to.be.true;
done();
});
});
});
it('Website should set file.available = false', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) {
config.root = "/home";
config.output = null;
var subject = new website_1.Website();
subject.fileChanged("/home/sample.html");
(0, chai_1.expect)(context.models.files.find('sample.html').available).to.be.false;
done();
});
});
it('Website should update server routes', function (done) {
(0, compiler_context_spec_1.CreateDataContext)(app_composition_1.IoC).then(addTestFileToContext).then(function (context) {
config.root = "/home";
config.output = null;
var subject = new website_1.Website();
var updateStub = server.updateRoutes = sandbox.stub();
subject.fileChanged("/home/sample.html").then(function (_) {
(0, chai_1.expect)(updateStub.called).to.be.true;
done();
});
});
});
});
function addTestAssetFileToContext(context) {
var file = new models_1.FileData('sample.png', './sample.png');
file.available = true;
context.models.assets.add('sample.png', file);
return context.saveChanges().then(function (_) { return (context); });
}
function addTestFileToContext(context) {
var file = new models_1.FileData('sample.html', '/home/sample.html');
file.available = true;
context.models.files.add('sample.html', file);
return context.saveChanges().then(function (_) { return (context); });
}
//# sourceMappingURL=website.spec.js.map