mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-29 14:35:18 -04:00
Add tests.
This commit is contained in:
parent
3882f1483b
commit
88854ad451
23 changed files with 171 additions and 38 deletions
133
test/tests/unit/ext/title.js
Normal file
133
test/tests/unit/ext/title.js
Normal file
|
@ -0,0 +1,133 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
var ID = 'ext/title';
|
||||
var DEPS = ['_', 'core/event', 'core/settings'];
|
||||
|
||||
describe('module \'' + ID + '\'', function () {
|
||||
|
||||
before(function () {
|
||||
|
||||
this.definition = modulejs._private.definitions[ID];
|
||||
|
||||
this.xEvent = {
|
||||
sub: sinon.stub()
|
||||
};
|
||||
this.xSettings = {title: {
|
||||
enabled: true
|
||||
}};
|
||||
|
||||
this.applyFn = function () {
|
||||
|
||||
this.xEvent.sub.reset();
|
||||
|
||||
return this.definition.fn(_, this.xEvent, this.xSettings);
|
||||
};
|
||||
|
||||
this.titleBak = document.title;
|
||||
this.dummyTitle = util.uniqId();
|
||||
});
|
||||
|
||||
after(function () {
|
||||
|
||||
document.title = this.titleBak;
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
|
||||
document.title = this.dummyTitle;
|
||||
});
|
||||
|
||||
describe('definition', function () {
|
||||
|
||||
it('is defined', function () {
|
||||
|
||||
assert.isPlainObject(this.definition);
|
||||
});
|
||||
|
||||
it('has correct id', function () {
|
||||
|
||||
assert.strictEqual(this.definition.id, ID);
|
||||
});
|
||||
|
||||
it('requires correct', function () {
|
||||
|
||||
assert.deepEqual(this.definition.deps, DEPS);
|
||||
});
|
||||
|
||||
it('args for each request', function () {
|
||||
|
||||
assert.strictEqual(this.definition.deps.length, this.definition.fn.length);
|
||||
});
|
||||
|
||||
it('has no instance', function () {
|
||||
|
||||
assert.notProperty(modulejs._private.instances, ID);
|
||||
});
|
||||
|
||||
it('inits without errors', function () {
|
||||
|
||||
this.applyFn();
|
||||
});
|
||||
});
|
||||
|
||||
describe('application', function () {
|
||||
|
||||
it('returns undefined', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isUndefined(instance);
|
||||
});
|
||||
|
||||
it('subscribes to location.changed', function () {
|
||||
|
||||
this.xSettings.title.enabled = true;
|
||||
|
||||
this.applyFn();
|
||||
assert.isTrue(this.xEvent.sub.calledOnce);
|
||||
assert.strictEqual(this.xEvent.sub.lastCall.args[0], 'location.changed');
|
||||
});
|
||||
|
||||
it('does not subscribe to events if disabled', function () {
|
||||
|
||||
this.xSettings.title.enabled = false;
|
||||
|
||||
this.applyFn();
|
||||
assert.isFalse(this.xEvent.sub.called);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sets title on location.changed', function () {
|
||||
|
||||
_.each([
|
||||
[''],
|
||||
['a', 'a'],
|
||||
['a', 'b', 'b - a > b'],
|
||||
['a', 'b', 'c', 'c - a > b > c']
|
||||
], function (data) {
|
||||
|
||||
var labels = data.slice(0, -1);
|
||||
var exp = data.slice(-1)[0];
|
||||
|
||||
it(String(labels) + ' => \'' + exp + '\'', function () {
|
||||
|
||||
this.xSettings.title.enabled = true;
|
||||
|
||||
this.applyFn();
|
||||
|
||||
var fn = this.xEvent.sub.lastCall.args[1];
|
||||
var crumb = _.map(labels, function (x) { return {label: x}; });
|
||||
var item = {
|
||||
getCrumb: sinon.stub().returns(crumb)
|
||||
};
|
||||
|
||||
fn(item);
|
||||
|
||||
assert.isTrue(item.getCrumb.calledOnce);
|
||||
assert.strictEqual(document.title, exp);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}());
|
Loading…
Add table
Add a link
Reference in a new issue