Clean code. Add tests.

This commit is contained in:
Lars Jung 2015-04-24 18:03:10 +02:00
parent d591dbc0cd
commit 908c49c584
4 changed files with 210 additions and 14 deletions

View file

@ -7,7 +7,11 @@ modulejs.define('model/item', ['_', 'core/event', 'core/location', 'core/server'
function startsWith(sequence, part) { function startsWith(sequence, part) {
return sequence.slice && part.length && sequence.slice(0, part.length) === part; if (!sequence || !sequence.substr || !part || !part.length) {
return false;
}
return sequence.substr(0, part.length) === part;
} }
function createLabel(sequence) { function createLabel(sequence) {

View file

@ -3,7 +3,7 @@
var ID = 'core/server'; var ID = 'core/server';
var DEPS = ['_', '$', 'config', 'core/location']; var DEPS = ['_', '$', 'config', 'core/location'];
var $submitSnap; var $submitEl;
describe('module \'' + ID + '\'', function () { describe('module \'' + ID + '\'', function () {
@ -31,7 +31,7 @@ describe('module \'' + ID + '\'', function () {
this.xAjax = sinon.stub($, 'ajax').returns(this.xAjaxResult); this.xAjax = sinon.stub($, 'ajax').returns(this.xAjaxResult);
this.xSubmit = sinon.stub($.fn, 'submit', function () { this.xSubmit = sinon.stub($.fn, 'submit', function () {
$submitSnap = this; $submitEl = this;
return this; return this;
}); });
@ -43,7 +43,7 @@ describe('module \'' + ID + '\'', function () {
this.xAjaxResult.always.reset(); this.xAjaxResult.always.reset();
this.xAjax.reset(); this.xAjax.reset();
this.xSubmit.reset(); this.xSubmit.reset();
$submitSnap = undefined; $submitEl = undefined;
return this.definition.fn(_, $, this.xConfig, this.xLocation); return this.definition.fn(_, $, this.xConfig, this.xLocation);
}; };
@ -263,7 +263,7 @@ describe('module \'' + ID + '\'', function () {
assert.isUndefined(res); assert.isUndefined(res);
assert.isFalse(this.xSubmit.called); assert.isFalse(this.xSubmit.called);
assert.isUndefined($submitSnap); assert.isUndefined($submitEl);
}); });
it('works', function () { it('works', function () {
@ -282,13 +282,13 @@ describe('module \'' + ID + '\'', function () {
assert.isTrue(this.xSubmit.calledOnce); assert.isTrue(this.xSubmit.calledOnce);
assert.lengthOf($submitSnap, 1); assert.lengthOf($submitEl, 1);
assert.strictEqual($submitSnap.get(0).tagName.toLowerCase(), 'form'); assert.strictEqual($submitEl.get(0).tagName.toLowerCase(), 'form');
assert.strictEqual($submitSnap.attr('method'), 'post'); assert.strictEqual($submitEl.attr('method'), 'post');
assert.strictEqual($submitSnap.attr('style'), 'display:none;'); assert.strictEqual($submitEl.attr('style'), 'display:none;');
assert.strictEqual($submitSnap.attr('action'), this.xAbsHref); assert.strictEqual($submitEl.attr('action'), this.xAbsHref);
var $children = $submitSnap.children(); var $children = $submitEl.children();
assert.lengthOf($children, 2); assert.lengthOf($children, 2);

View file

@ -10,11 +10,19 @@ describe('module \'' + ID + '\'', function () {
this.definition = modulejs._private.definitions[ID]; this.definition = modulejs._private.definitions[ID];
this.xTypes = util.uniqObj(); this.xRootName = util.uniqId();
this.xTypes = {
getType: sinon.stub().returns(util.uniqId())
};
this.xEvent = util.uniqObj(); this.xEvent = util.uniqObj();
this.xSettings = util.uniqObj(); this.xSettings = {
rootHref: util.uniqPath('/' + this.xRootName + '/')
};
this.xServer = util.uniqObj(); this.xServer = util.uniqObj();
this.xLocation = util.uniqObj(); this.xLocation = {
forceEncoding: sinon.stub().returnsArg(0),
getDomain: sinon.stub().returns(util.uniqId())
};
this.applyFn = function () { this.applyFn = function () {
return this.definition.fn(_, this.xEvent, this.xLocation, this.xServer, this.xSettings, this.xTypes); return this.definition.fn(_, this.xEvent, this.xLocation, this.xServer, this.xSettings, this.xTypes);
@ -71,6 +79,189 @@ describe('module \'' + ID + '\'', function () {
var instance = this.applyFn(); var instance = this.applyFn();
assert.isFunction(instance.get); assert.isFunction(instance.get);
}); });
it('returns null with no argument', function () {
var instance = this.applyFn();
assert.isNull(instance.get());
});
it('returns null for no string argument', function () {
var instance = this.applyFn();
assert.isNull(instance.get(1));
});
it('returns null for href not starting with rootHref', function () {
var instance = this.applyFn();
assert.isNull(instance.get('/a/'));
});
describe('for rootHref', function () {
beforeEach(function () {
var instance = this.applyFn();
this.item = instance.get(this.xSettings.rootHref);
});
it('returns object', function () {
assert.isObject(this.item);
});
it('sets href correct', function () {
assert.strictEqual(this.item.absHref, this.xSettings.rootHref);
});
it('sets type correct', function () {
assert.strictEqual(this.item.type, this.xTypes.getType(this.absHref));
});
it('sets label correct', function () {
assert.strictEqual(this.item.label, this.xRootName);
});
it('sets time to null', function () {
assert.isNull(this.item.time);
});
it('sets size to null', function () {
assert.isNull(this.item.size);
});
it('sets parent to null', function () {
assert.isNull(this.item.parent);
});
it('sets isManaged to null', function () {
assert.isNull(this.item.isManaged);
});
it('sets content correct', function () {
assert.isPlainObject(this.item.content);
assert.lengthOfKeys(this.item.content, 0);
});
});
describe('for folder href other than rootHref', function () {
beforeEach(function () {
var instance = this.applyFn();
this.item = instance.get(this.xSettings.rootHref + 'a/');
});
it('returns object', function () {
assert.isObject(this.item);
});
it('sets href correct', function () {
assert.strictEqual(this.item.absHref, this.xSettings.rootHref + 'a/');
});
it('sets type correct', function () {
assert.strictEqual(this.item.type, this.xTypes.getType(this.absHref));
});
it('sets label correct', function () {
assert.strictEqual(this.item.label, 'a');
});
it('sets time to null', function () {
assert.isNull(this.item.time);
});
it('sets size to null', function () {
assert.isNull(this.item.size);
});
it('sets parent to object', function () {
assert.isObject(this.item.parent);
});
it('sets isManaged to null', function () {
assert.isNull(this.item.isManaged);
});
it('sets content correct', function () {
assert.isPlainObject(this.item.content);
assert.lengthOfKeys(this.item.content, 0);
});
});
describe('for file href other than rootHref', function () {
beforeEach(function () {
var instance = this.applyFn();
this.item = instance.get(this.xSettings.rootHref + 'a');
});
it('returns object', function () {
assert.isObject(this.item);
});
it('sets href correct', function () {
assert.strictEqual(this.item.absHref, this.xSettings.rootHref + 'a');
});
it('sets type correct', function () {
assert.strictEqual(this.item.type, this.xTypes.getType(this.absHref));
});
it('sets label correct', function () {
assert.strictEqual(this.item.label, 'a');
});
it('sets time to null', function () {
assert.isNull(this.item.time);
});
it('sets size to null', function () {
assert.isNull(this.item.size);
});
it('sets parent to object', function () {
assert.isObject(this.item.parent);
});
it('sets isManaged to null', function () {
assert.isNull(this.item.isManaged);
});
it('sets content correct', function () {
assert.isPlainObject(this.item.content);
assert.lengthOfKeys(this.item.content, 0);
});
});
}); });
describe('.remove()', function () { describe('.remove()', function () {

View file

@ -87,6 +87,7 @@ describe('premisses', function () {
assert.throws(function () { assert.isPlainObject(new Date()); }); assert.throws(function () { assert.isPlainObject(new Date()); });
assert.throws(function () { assert.isPlainObject(/a/); }); assert.throws(function () { assert.isPlainObject(/a/); });
assert.throws(function () { assert.isPlainObject(function () {}); }); assert.throws(function () { assert.isPlainObject(function () {}); });
assert.throws(function () { assert.isPlainObject(new function A() {}); });
}); });
it('assert.lengthOfKeys() works', function () { it('assert.lengthOfKeys() works', function () {