mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-24 03:57:08 -04:00
Clean code.
This commit is contained in:
parent
c396800dfa
commit
882bf8ae11
17 changed files with 202 additions and 148 deletions
|
@ -22,8 +22,8 @@
|
|||
"mocha": true,
|
||||
|
||||
"globals": {
|
||||
"$": false,
|
||||
"_": false,
|
||||
"$": false,
|
||||
"assert": false,
|
||||
"chai": false,
|
||||
"jQuery": false,
|
||||
|
|
|
@ -59,25 +59,34 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.sub()', function () {
|
||||
|
||||
it('.sub() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.sub);
|
||||
});
|
||||
});
|
||||
|
||||
it('.unsub() is function', function () {
|
||||
describe('.unsub()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.unsub);
|
||||
});
|
||||
});
|
||||
|
||||
it('.pub() is function', function () {
|
||||
describe('.pub()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.pub);
|
||||
});
|
||||
});
|
||||
|
||||
describe('works', function () {
|
||||
|
||||
it('works', function () {
|
||||
|
||||
|
|
|
@ -59,33 +59,24 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.setDefaultMetric()', function () {
|
||||
|
||||
it('.setDefaultMetric() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.setDefaultMetric);
|
||||
});
|
||||
});
|
||||
|
||||
it('.formatSize() is function', function () {
|
||||
describe('.formatSize()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.formatSize);
|
||||
});
|
||||
|
||||
it('.setDefaultDateFormat() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.setDefaultDateFormat);
|
||||
});
|
||||
|
||||
it('.formatDate() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.formatDate);
|
||||
});
|
||||
|
||||
it('.formatSize() defaults to decimal metric', function () {
|
||||
it('defaults to decimal metric', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.formatSize(1024), '1 KB');
|
||||
|
@ -95,7 +86,7 @@ describe('module "' + ID + '"', function () {
|
|||
assert.strictEqual(instance.formatSize(1024), '1 KB');
|
||||
});
|
||||
|
||||
describe('.formatSize() with decimal metric', function () {
|
||||
describe('decimal metric', function () {
|
||||
|
||||
_.each([
|
||||
[0, '0 B'],
|
||||
|
@ -130,7 +121,7 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('.formatSize() with binary metric', function () {
|
||||
describe('binary metric', function () {
|
||||
|
||||
_.each([
|
||||
[0, '0 B'],
|
||||
|
@ -165,8 +156,26 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('.formatDate() with default format', function () {
|
||||
describe('.setDefaultDateFormat()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.setDefaultDateFormat);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.formatDate()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.formatDate);
|
||||
});
|
||||
|
||||
it('default format', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.formatDate(0), '');
|
||||
|
@ -187,8 +196,6 @@ describe('module "' + ID + '"', function () {
|
|||
assert.strictEqual(instance.formatDate(1400000000000), '18 14 20');
|
||||
});
|
||||
|
||||
describe('.formatDate()', function () {
|
||||
|
||||
_.each([
|
||||
[0, 'YYYY-MM-DD HH:mm:ss', ''],
|
||||
[1000, 'YYYY-MM-DD HH:mm:ss', '1970-01-01 01:00:01'],
|
||||
|
@ -210,6 +217,5 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}());
|
||||
|
|
|
@ -73,8 +73,6 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
|
||||
describe('.forceEncoding()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
@ -150,6 +148,5 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
}());
|
||||
|
|
|
@ -77,15 +77,15 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.set()', function () {
|
||||
|
||||
it('.set() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.ok(_.isFunction(instance.set));
|
||||
});
|
||||
|
||||
it('.set() works', function () {
|
||||
it('works', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ describe('module "' + ID + '"', function () {
|
|||
b: 'myTheme/b.jpg'
|
||||
}
|
||||
};
|
||||
this.xSettings = {appHref: '/some/app/href/'};
|
||||
this.xSettings = {appHref: util.uniqPath('/appHref/')};
|
||||
this.applyFn = function () {
|
||||
|
||||
return this.definition.fn(_, this.xConfig, this.xSettings);
|
||||
|
@ -66,21 +66,15 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.image()', function () {
|
||||
|
||||
it('.image() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.image);
|
||||
});
|
||||
|
||||
it('.icon() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.icon);
|
||||
});
|
||||
|
||||
it('.image() works', function () {
|
||||
it('works', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
var ui = this.xSettings.appHref + 'client/images/ui/';
|
||||
|
@ -90,8 +84,17 @@ describe('module "' + ID + '"', function () {
|
|||
assert.strictEqual(instance.image(''), ui + '.svg');
|
||||
assert.strictEqual(instance.image('a'), ui + 'a.svg');
|
||||
});
|
||||
});
|
||||
|
||||
it('.icon() works', function () {
|
||||
describe('.icon()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.icon);
|
||||
});
|
||||
|
||||
it('works', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
var themes = this.xSettings.appHref + 'client/images/themes/';
|
||||
|
|
|
@ -98,21 +98,27 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.backend', function () {
|
||||
|
||||
it('.backend is set correct', function () {
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.backend, this.xConfig.setup.BACKEND);
|
||||
});
|
||||
});
|
||||
|
||||
it('.name is set correct', function () {
|
||||
describe('.name', function () {
|
||||
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.name, this.xConfig.setup.SERVER_NAME);
|
||||
});
|
||||
});
|
||||
|
||||
it('.version is set correct', function () {
|
||||
describe('.version', function () {
|
||||
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.version, this.xConfig.setup.SERVER_VERSION);
|
||||
|
|
|
@ -73,7 +73,7 @@ describe('module "' + ID + '"', function () {
|
|||
|
||||
describe('publics', function () {
|
||||
|
||||
it('are extended from options', function () {
|
||||
it('extended from options', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.someOptions, this.xConfig.options.someOptions);
|
||||
|
@ -81,20 +81,29 @@ describe('module "' + ID + '"', function () {
|
|||
assert.strictEqual(instance.more, this.xConfig.options.more);
|
||||
assert.strictEqual(_.keys(instance).length, _.keys(this.xConfig.options).length + 3);
|
||||
});
|
||||
});
|
||||
|
||||
it('.appHref is set correct', function () {
|
||||
describe('.appHref', function () {
|
||||
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.appHref, this.xConfig.setup.APP_HREF);
|
||||
});
|
||||
});
|
||||
|
||||
it('.rootHref is set correct', function () {
|
||||
describe('.rootHref', function () {
|
||||
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.rootHref, this.xConfig.setup.ROOT_HREF);
|
||||
});
|
||||
});
|
||||
|
||||
it('.currentHref is set correct', function () {
|
||||
describe('.currentHref', function () {
|
||||
|
||||
it('set correct', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.currentHref, this.xConfig.setup.CURRENT_HREF);
|
||||
|
|
|
@ -71,27 +71,32 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.put()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.put);
|
||||
});
|
||||
});
|
||||
|
||||
describe('.get()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.get);
|
||||
});
|
||||
});
|
||||
|
||||
describe('works', function () {
|
||||
|
||||
it('works', function () {
|
||||
|
||||
var key1 = 'test1';
|
||||
var value1 = '1234';
|
||||
var key2 = 'test2';
|
||||
var value2 = '5678';
|
||||
|
||||
it('.put() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.put);
|
||||
});
|
||||
|
||||
it('.get() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.get);
|
||||
});
|
||||
|
||||
it('works', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
|
||||
assert.isNull(window.localStorage.getItem(this.storeKey));
|
||||
|
|
|
@ -64,15 +64,15 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.getType()', function () {
|
||||
|
||||
it('.getType() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.getType);
|
||||
});
|
||||
|
||||
it('.getType() works', function () {
|
||||
it('works', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.strictEqual(instance.getType('file.a'), 'a');
|
||||
|
|
|
@ -59,20 +59,14 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.regularCmpFn()', function () {
|
||||
|
||||
it('.regularCmpFn() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.regularCmpFn);
|
||||
});
|
||||
|
||||
it('.naturalCmpFn() is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.naturalCmpFn);
|
||||
});
|
||||
|
||||
_.each([
|
||||
[0, 0, 0],
|
||||
[1, 0, 1],
|
||||
|
@ -93,6 +87,15 @@ describe('module "' + ID + '"', function () {
|
|||
assert.strictEqual(instance.regularCmpFn(arg1, arg2), exp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('.naturalCmpFn()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.naturalCmpFn);
|
||||
});
|
||||
|
||||
_.each([
|
||||
[0, 0, 0],
|
||||
|
|
|
@ -64,15 +64,18 @@ describe('module "' + ID + '"', function () {
|
|||
});
|
||||
});
|
||||
|
||||
describe('publics', function () {
|
||||
describe('.get()', function () {
|
||||
|
||||
it('.get() is function', function () {
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.get);
|
||||
});
|
||||
});
|
||||
|
||||
it('.remove() is function', function () {
|
||||
describe('.remove()', function () {
|
||||
|
||||
it('is function', function () {
|
||||
|
||||
var instance = this.applyFn();
|
||||
assert.isFunction(instance.remove);
|
||||
|
|
|
@ -34,8 +34,6 @@ describe('premisses', function () {
|
|||
|
||||
assert.isFunction(util.uniqId);
|
||||
|
||||
var uid1 = parseInt(util.uniqObj().uniqId.replace(/\D/g, ''), 10);
|
||||
|
||||
assert.lengthOfKeys(util.uniqObj(), 1);
|
||||
assert.isTrue(util.isUniqId(util.uniqObj().uniqId));
|
||||
assert.notEqual(util.uniqObj(), util.uniqObj());
|
||||
|
@ -44,9 +42,17 @@ describe('premisses', function () {
|
|||
assert.notEqual(util.uniqObj().uniqId, util.uniqObj().uniqId);
|
||||
assert.notDeepEqual(util.uniqObj().uniqId, util.uniqObj().uniqId);
|
||||
assert.notStrictEqual(util.uniqObj().uniqId, util.uniqObj().uniqId);
|
||||
});
|
||||
|
||||
var uid2 = parseInt(util.uniqObj().uniqId.replace(/\D/g, ''), 10);
|
||||
assert.strictEqual(uid2, uid1 + 15);
|
||||
it('util.uniqPath() works', function () {
|
||||
|
||||
assert.isFunction(util.uniqPath);
|
||||
|
||||
assert.notEqual(util.uniqPath(), util.uniqPath());
|
||||
assert.notDeepEqual(util.uniqPath(), util.uniqPath());
|
||||
assert.notStrictEqual(util.uniqPath(), util.uniqPath());
|
||||
assert.strictEqual(util.uniqPath('abc').substr(-3), 'abc');
|
||||
assert.strictEqual(util.uniqPath('xyz/').substr(-4), 'xyz/');
|
||||
});
|
||||
|
||||
it('assert.isPlainObject() works', function () {
|
||||
|
|
|
@ -12,7 +12,7 @@ describe('module "' + ID + '"', function () {
|
|||
|
||||
this.xSettings = util.uniqObj();
|
||||
this.xResource = {
|
||||
icon: sinon.stub().returns('/some/path/' + util.uniqId() + '-icon.png')
|
||||
icon: sinon.stub().returns(util.uniqPath('-icon.png'))
|
||||
};
|
||||
this.xFormat = {
|
||||
formatSize: sinon.stub().returns(util.uniqId()),
|
||||
|
|
|
@ -13,8 +13,8 @@ describe('module "' + ID + '"', function () {
|
|||
this.xResource = {
|
||||
image: sinon.stub().throws('invalid image request')
|
||||
};
|
||||
this.xResource.image.withArgs('back').returns('/some/path/' + util.uniqId() + '-back.png');
|
||||
this.xResource.image.withArgs('sidebar').returns('/some/path/' + util.uniqId() + '-sidebar.png');
|
||||
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()
|
||||
|
|
|
@ -14,7 +14,7 @@ describe('module "' + ID + '"', function () {
|
|||
view: {}
|
||||
};
|
||||
this.xResource = {
|
||||
image: sinon.stub().returns('/some/path/' + util.uniqId() + '.png')
|
||||
image: sinon.stub().returns(util.uniqPath('-image.png'))
|
||||
};
|
||||
this.xStore = {
|
||||
get: sinon.stub(),
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
var PREFIX = 'UQ';
|
||||
var SUFFIX = 'ID';
|
||||
var PREFIX = 'UNIQ-';
|
||||
var SUFFIX = '-ID';
|
||||
var LENGTH = 4;
|
||||
var RE = new RegExp('^' + PREFIX + '\\d{' + LENGTH + '}' + SUFFIX + '$');
|
||||
|
||||
|
@ -14,19 +14,26 @@ function uniqId() {
|
|||
return PREFIX + ('00000000' + counter).substr(-LENGTH) + SUFFIX;
|
||||
}
|
||||
|
||||
function uniqObj() {
|
||||
|
||||
return {uniqId: uniqId()};
|
||||
}
|
||||
|
||||
function isUniqId(uid) {
|
||||
|
||||
return RE.test(uid);
|
||||
}
|
||||
|
||||
function uniqObj() {
|
||||
|
||||
return {uniqId: uniqId()};
|
||||
}
|
||||
|
||||
function uniqPath(suffix) {
|
||||
|
||||
return '/some/path/' + uniqId() + (suffix ? suffix : '');
|
||||
}
|
||||
|
||||
|
||||
window.util = window.util || {};
|
||||
window.util.uniqId = uniqId;
|
||||
window.util.uniqObj = uniqObj;
|
||||
window.util.isUniqId = isUniqId;
|
||||
window.util.uniqObj = uniqObj;
|
||||
window.util.uniqPath = uniqPath;
|
||||
|
||||
}());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue