[util/registry] tests!

This commit is contained in:
Spencer Alger 2014-08-21 15:30:45 -07:00
parent 70d01c3881
commit 1a5b1ff967
4 changed files with 205 additions and 1 deletions

View file

@ -82,7 +82,8 @@
'specs/utils/diff_object',
'specs/utils/diff_time_picker_vals',
'specs/factories/events',
'specs/index_patterns/_flatten_search_response'
'specs/index_patterns/_flatten_search_response',
'specs/utils/registry/index'
], function (kibana, sinon) {
kibana.load(function () {
var xhr = sinon.useFakeXMLHttpRequest();

View file

@ -0,0 +1,140 @@
define(function (require) {
var _ = require('lodash');
var Registry = require('utils/registry/registry');
// this is generally a data-structure that Registry is good for managing
var users = [
{ name: 'John', id: 69, username: 'beast', group: 'admins' },
{ name: 'Anon', id: 0, username: 'shhhh', group: 'secret' },
{ name: 'Fern', id: 42, username: 'kitty', group: 'editor' },
{ name: 'Mary', id: 55, username: 'sheep', group: 'editor' }
];
// this is how we used to accomplish this, before Registry
users.byName = _.indexBy(users, 'name');
users.byUsername = _.indexBy(users, 'username');
users.byGroup = _.groupBy(users, 'group');
users.inIdOrder = _.sortBy(users, 'id');
// then things started becoming unruly... so Registry!
describe('Registry', function () {
describe('Basics', function () {
var reg;
beforeEach(function () {
reg = new Registry();
});
it('Extends Array', function () {
expect(reg).to.be.a(Array);
});
it('fails basic lodash check', function () {
expect(_.isArray(reg)).to.be(false);
});
it('clones to an object', function () {
expect(_.isPlainObject(_.clone(reg))).to.be(true);
expect(_.isArray(_.clone(reg))).to.be(false);
});
});
describe('Indexing', function () {
it('provides the initial set', function () {
var reg = new Registry({
initialSet: [1, 2, 3]
});
expect(reg).to.have.length(3);
reg.forEach(function (v, i) {
expect(v).to.eql(i + 1);
});
});
it('indexes the initial set', function () {
var reg = new Registry({
index: ['username'],
initialSet: users
});
expect(reg).to.have.property('byUsername');
expect(reg.byUsername).to.eql(users.byUsername);
});
it('updates indices after values are added', function () {
// split up the user list, and add it in chunks
var firstUser = users.slice(0, 1).pop();
var otherUsers = users.slice(1);
// start off with all but the first
var reg = new Registry({
group: ['group'],
order: ['id'],
initialSet: otherUsers
});
// add the first
reg.push(firstUser);
// end up with the same structure that is in the users fixture
expect(reg.byGroup).to.eql(users.byGroup);
expect(reg.inIdOrder).to.eql(users.inIdOrder);
});
it('updates indices after values are removed', function () {
// start off with all
var reg = new Registry({
group: ['group'],
order: ['id'],
initialSet: users
});
// remove the last
reg.pop();
var expectedCount = users.length - 1;
// indexed lists should be updated
expect(reg).to.have.length(expectedCount);
var sumOfGroups = _.reduce(reg.byGroup, function (note, group) {
return note + group.length;
}, 0);
expect(sumOfGroups).to.eql(expectedCount);
});
it('updates indices after values are re-ordered', function () {
var rawUsers = users.slice(0);
// collect and shuffle the ids available
var ids = [];
_.times(rawUsers.length, function (i) { ids.push(i); });
ids = _.shuffle(ids);
// move something here
var toI = ids.shift();
// from here
var fromI = ids.shift();
// do the move
var move = function (arr) { arr.splice(toI, 0, arr.splice(fromI, 1)[0]); };
var reg = new Registry({
index: ['username'],
initialSet: rawUsers
});
var index = reg.byUsername;
move(reg);
expect(reg.byUsername).to.eql(index);
expect(reg.byUsername).to.not.be(index);
});
});
require('./inflector')();
require('./path_getter')();
});
});

View file

@ -0,0 +1,30 @@
define(function (require) {
return function () {
var inflector = require('utils/registry/_inflector');
describe('Inflector', function () {
it('returns a function', function () {
var getter = inflector();
expect(getter).to.be.a('function');
});
describe('fn', function () {
it('prepends a prefix', function () {
var inflect = inflector('my');
expect(inflect('Family')).to.be('myFamily');
expect(inflect('family')).to.be('myFamily');
expect(inflect('fAmIlY')).to.be('myFamily');
});
it('adds both a prefix and suffix', function () {
var inflect = inflector('foo', 'Bar');
expect(inflect('box')).to.be('fooBoxBar');
expect(inflect('box.car.MAX')).to.be('fooBoxCarMaxBar');
expect(inflect('BaZzY')).to.be('fooBazzyBar');
});
});
});
};
});

View file

@ -0,0 +1,33 @@
define(function (require) {
return function () {
var pathGetter = require('utils/registry/_path_getter');
describe('Path Getter', function () {
it('returns a function', function () {
var getter = pathGetter('some.nonexistant.property');
expect(getter).to.be.a('function');
});
describe('... Getter', function () {
it('gets values from the object it is passed', function () {
var getter = pathGetter('some.nested.prop');
var val = getter({ some: { nested: { prop: 42 }}});
expect(val).to.be(42);
});
it('works just as well if we specify just a prop', function () {
var getter = pathGetter('name');
var val = getter({ name: 'Master Smitty Johnston III' });
expect(val).to.be('Master Smitty Johnston III');
});
it('returns undefined if the object does not exist', function () {
var getter = pathGetter('no.spelllllllling.writer');
var val = getter({ spelled: { right: 5 } });
expect(val).to.be(undefined);
});
});
});
};
});