h5ai/test/tests/unit/view/sidebar.js
2015-04-22 19:34:35 +02:00

127 lines
3.4 KiB
JavaScript

(function () {
'use strict';
var ID = 'view/sidebar';
var DEPS = ['$', 'core/resource', 'core/store'];
describe('module "' + ID + '"', function () {
before(function () {
this.definition = modulejs._private.definitions[ID];
this.xResource = {
image: sinon.stub().throws('invalid image request')
};
this.xResource.image.withArgs('back').returns(util.uniqPath('-back.png'));
this.xResource.image.withArgs('sidebar').returns(util.uniqPath('-sidebar.png'));
this.xStore = {
get: sinon.stub().returns(false),
put: sinon.stub()
};
this.xStore.get.returns(false);
this.applyFn = function () {
this.xResource.image.reset();
this.xStore.get.reset();
this.xStore.put.reset();
return this.definition.fn($, this.xResource, this.xStore);
};
});
after(function () {
util.restoreHtml();
});
beforeEach(function () {
util.restoreHtml();
$('<div id="toolbar"/>').appendTo('body');
$('<div id="sidebar"/>').appendTo('body');
});
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.instance = this.applyFn();
});
});
describe('application', function () {
it('returns undefined', function () {
var instance = this.applyFn();
assert.isUndefined(instance);
});
it('adds HTML #sidebar-toggle', function () {
this.applyFn();
assert.lengthOf($('#toolbar > #sidebar-toggle'), 1);
});
it('toggle works', function () {
this.applyFn();
assert.lengthOf($('#sidebar:visible'), 0);
this.xStore.get.returns(false).reset();
this.xStore.put.reset();
$('#sidebar-toggle').trigger('click');
assert.isTrue(this.xStore.get.calledOnce);
assert.strictEqual(this.xStore.get.lastCall.args[0], 'sidebarIsVisible');
assert.isTrue(this.xStore.put.calledOnce);
assert.strictEqual(this.xStore.put.lastCall.args[0], 'sidebarIsVisible');
assert.isTrue(this.xStore.put.lastCall.args[1]);
assert.lengthOf($('#sidebar:visible'), 1);
this.xStore.get.returns(true).reset();
this.xStore.put.reset();
$('#sidebar-toggle').trigger('click');
assert.isTrue(this.xStore.get.calledOnce);
assert.strictEqual(this.xStore.get.lastCall.args[0], 'sidebarIsVisible');
assert.isTrue(this.xStore.put.calledOnce);
assert.strictEqual(this.xStore.put.lastCall.args[0], 'sidebarIsVisible');
assert.isFalse(this.xStore.put.lastCall.args[1]);
assert.lengthOf($('#sidebar:visible'), 0);
});
});
});
}());