2013-05-26 21:17:46 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-06-25 15:43:15 +04:00
|
|
|
var should = require('should'),
|
|
|
|
when = require('when'),
|
|
|
|
sinon = require('sinon'),
|
|
|
|
Ghost = require('../../ghost');
|
2013-05-26 21:17:46 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
describe("Ghost API", function () {
|
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) {
|
|
|
|
var ghost = new Ghost(),
|
|
|
|
fakeDataProvider = {
|
|
|
|
init: function () {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
dataProviderInitSpy = sinon.spy(fakeDataProvider, "init"),
|
|
|
|
oldDataProvider = ghost.dataProvider;
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.dataProvider = fakeDataProvider;
|
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-06-25 15:43:15 +04:00
|
|
|
dataProviderInitSpy.called.should.equal(true);
|
2013-05-30 18:01:29 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
ghost.dataProvider = oldDataProvider;
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
|
|
|
}).then(null, 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 () {
|
|
|
|
var ghost = new Ghost(),
|
|
|
|
filterName = 'test',
|
|
|
|
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 () {
|
|
|
|
var ghost = new Ghost(),
|
|
|
|
filterName = 'test',
|
|
|
|
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) {
|
|
|
|
var ghost = new Ghost(),
|
|
|
|
filterName = 'testpriority',
|
|
|
|
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-06-25 15:43:15 +04:00
|
|
|
});
|