2013-05-26 21:17:46 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-09-06 20:07:25 +04:00
|
|
|
var testUtils = require('./testUtils'),
|
|
|
|
should = require('should'),
|
2013-06-25 15:43:15 +04:00
|
|
|
sinon = require('sinon'),
|
2013-09-06 20:07:25 +04:00
|
|
|
when = require('when'),
|
2013-07-11 02:45:13 +04:00
|
|
|
path = require('path'),
|
|
|
|
_ = require('underscore'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
2013-06-25 15:43:15 +04:00
|
|
|
Ghost = require('../../ghost');
|
2013-05-26 21:17:46 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
describe("Ghost API", function () {
|
2013-07-31 11:33:28 +04:00
|
|
|
var testTemplatePath = 'core/test/unit/fixtures/',
|
2013-08-02 21:51:33 +04:00
|
|
|
themeTemplatePath= 'core/test/unit/fixtures/theme',
|
2013-07-11 02:45:13 +04:00
|
|
|
ghost;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
ghost = new Ghost();
|
|
|
|
});
|
2013-05-26 21:17:46 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("is a singleton", function () {
|
|
|
|
var logStub = sinon.stub(console, "log"),
|
|
|
|
ghost1 = new Ghost(),
|
|
|
|
ghost2 = new Ghost();
|
2013-05-26 21:17:46 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.strictEqual(ghost1, ghost2);
|
|
|
|
logStub.restore();
|
|
|
|
});
|
2013-05-26 21:17:46 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("uses init() to initialize", function (done) {
|
2013-08-05 19:11:32 +04:00
|
|
|
var dataProviderInitMock = sinon.stub(ghost.dataProvider, "init", function () {
|
|
|
|
return when.resolve();
|
|
|
|
});
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.not.exist(ghost.settings());
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.init().then(function () {
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(ghost.settings());
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-08-05 19:11:32 +04:00
|
|
|
dataProviderInitMock.called.should.equal(true);
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-08-05 19:11:32 +04:00
|
|
|
dataProviderInitMock.restore();
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-08-05 19:11:32 +04:00
|
|
|
}, done);
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("can register filters with specific priority", function () {
|
2013-07-11 02:45:13 +04:00
|
|
|
var filterName = 'test',
|
2013-06-25 15:43:15 +04:00
|
|
|
filterPriority = 9,
|
|
|
|
testFilterHandler = sinon.spy();
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.registerFilter(filterName, filterPriority, testFilterHandler);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(ghost.filterCallbacks[filterName]);
|
|
|
|
should.exist(ghost.filterCallbacks[filterName][filterPriority]);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.filterCallbacks[filterName][filterPriority].should.include(testFilterHandler);
|
|
|
|
});
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("can register filters with default priority", function () {
|
2013-07-11 02:45:13 +04:00
|
|
|
var filterName = 'test',
|
2013-06-25 15:43:15 +04:00
|
|
|
defaultPriority = 5,
|
|
|
|
testFilterHandler = sinon.spy();
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.registerFilter(filterName, testFilterHandler);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(ghost.filterCallbacks[filterName]);
|
|
|
|
should.exist(ghost.filterCallbacks[filterName][defaultPriority]);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.filterCallbacks[filterName][defaultPriority].should.include(testFilterHandler);
|
|
|
|
});
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("executes filters in priority order", function (done) {
|
2013-07-11 02:45:13 +04:00
|
|
|
var filterName = 'testpriority',
|
2013-06-25 15:43:15 +04:00
|
|
|
testFilterHandler1 = sinon.spy(),
|
|
|
|
testFilterHandler2 = sinon.spy(),
|
|
|
|
testFilterHandler3 = sinon.spy();
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.registerFilter(filterName, 0, testFilterHandler1);
|
|
|
|
ghost.registerFilter(filterName, 2, testFilterHandler2);
|
|
|
|
ghost.registerFilter(filterName, 9, testFilterHandler3);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.doFilter(filterName, null, function () {
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
testFilterHandler1.calledBefore(testFilterHandler2).should.equal(true);
|
|
|
|
testFilterHandler2.calledBefore(testFilterHandler3).should.equal(true);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
testFilterHandler3.called.should.equal(true);
|
2013-06-09 20:16:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-06-09 20:16:25 +04:00
|
|
|
});
|
2013-05-26 21:17:46 +04:00
|
|
|
});
|
2013-07-11 02:45:13 +04:00
|
|
|
|
|
|
|
it("can compile a template", function (done) {
|
|
|
|
var template = path.join(process.cwd(), testTemplatePath, 'test.hbs');
|
|
|
|
|
|
|
|
should.exist(ghost.compileTemplate, 'Template Compiler exists');
|
|
|
|
|
|
|
|
ghost.compileTemplate(template).then(function (templateFn) {
|
|
|
|
should.exist(templateFn);
|
|
|
|
_.isFunction(templateFn).should.equal(true);
|
|
|
|
|
|
|
|
templateFn().should.equal('<h1>HelloWorld</h1>');
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-07-11 02:45:13 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it("loads templates for helpers", function (done) {
|
2013-08-02 21:51:33 +04:00
|
|
|
var compileSpy = sinon.spy(ghost, 'compileTemplate'),
|
|
|
|
pathsStub;
|
2013-07-11 02:45:13 +04:00
|
|
|
|
|
|
|
should.exist(ghost.loadTemplate, 'load template function exists');
|
|
|
|
|
|
|
|
// In order for the test to work, need to replace the path to the template
|
2013-08-02 21:51:33 +04:00
|
|
|
pathsStub = sinon.stub(ghost, "paths", function () {
|
|
|
|
return {
|
|
|
|
// Forcing the theme path to be the same
|
|
|
|
activeTheme: path.join(process.cwd(), testTemplatePath),
|
|
|
|
helperTemplates: path.join(process.cwd(), testTemplatePath)
|
|
|
|
};
|
2013-07-11 02:45:13 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
ghost.loadTemplate('test').then(function (templateFn) {
|
2013-08-02 21:51:33 +04:00
|
|
|
compileSpy.restore();
|
|
|
|
pathsStub.restore();
|
|
|
|
|
2013-07-11 02:45:13 +04:00
|
|
|
// test that compileTemplate was called with the expected path
|
|
|
|
compileSpy.calledOnce.should.equal(true);
|
|
|
|
compileSpy.calledWith(path.join(process.cwd(), testTemplatePath, 'test.hbs')).should.equal(true);
|
|
|
|
|
|
|
|
should.exist(templateFn);
|
|
|
|
_.isFunction(templateFn).should.equal(true);
|
|
|
|
|
|
|
|
templateFn().should.equal('<h1>HelloWorld</h1>');
|
|
|
|
|
2013-09-06 20:07:25 +04:00
|
|
|
|
2013-08-02 21:51:33 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("loads templates from themes first", function (done) {
|
|
|
|
var compileSpy = sinon.spy(ghost, 'compileTemplate'),
|
|
|
|
pathsStub;
|
|
|
|
|
|
|
|
should.exist(ghost.loadTemplate, 'load template function exists');
|
|
|
|
|
|
|
|
// In order for the test to work, need to replace the path to the template
|
|
|
|
pathsStub = sinon.stub(ghost, "paths", function () {
|
|
|
|
return {
|
|
|
|
activeTheme: path.join(process.cwd(), themeTemplatePath),
|
|
|
|
helperTemplates: path.join(process.cwd(), testTemplatePath)
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
ghost.loadTemplate('test').then(function (templateFn) {
|
|
|
|
// test that compileTemplate was called with the expected path
|
|
|
|
compileSpy.calledOnce.should.equal(true);
|
|
|
|
compileSpy.calledWith(path.join(process.cwd(), themeTemplatePath, 'partials', 'test.hbs')).should.equal(true);
|
|
|
|
|
|
|
|
should.exist(templateFn);
|
|
|
|
_.isFunction(templateFn).should.equal(true);
|
|
|
|
|
|
|
|
templateFn().should.equal('<h1>HelloWorld Themed</h1>');
|
|
|
|
|
2013-07-11 02:45:13 +04:00
|
|
|
compileSpy.restore();
|
2013-08-02 21:51:33 +04:00
|
|
|
pathsStub.restore();
|
2013-07-11 02:45:13 +04:00
|
|
|
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-07-11 02:45:13 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|