rename the HistoryFactory to PersistedLog, change API slightly, update tests

This commit is contained in:
Joe Fleming 2014-08-06 17:51:38 -07:00
parent 224691dccc
commit 2facccfefc
4 changed files with 77 additions and 74 deletions

View file

@ -1,32 +0,0 @@
define(function (require) {
var modules = require('modules');
var _ = require('lodash');
modules.get('kibana/history')
.factory('history', function ($window, storage, config) {
function History(name, length) {
this.name = name;
this.maxLength = length || config.get('history:limit');
this.items = storage.get(this.name) || [];
}
History.prototype.add = function (val) {
if (!val) {
return this.items;
}
var history = _.pull(this.items.slice(0), val);
history.unshift(val);
this.items = history.slice(0, this.maxLength);
storage.set(this.name, this.items);
return this.items;
};
History.prototype.get = function () {
return this.items;
};
return History;
});
});

View file

@ -0,0 +1,48 @@
define(function (require) {
var modules = require('modules');
var _ = require('lodash');
modules.get('kibana/persisted_log')
.factory('PersistedLog', function ($window, storage) {
function PersistedLog(name, options) {
options = options || {};
this.name = name;
this.maxLength = options.maxLength || 0;
this.filterDuplicates = options.filterDuplicates || false;
this.items = storage.get(this.name) || [];
}
PersistedLog.prototype.add = function (val) {
if (val == null) {
return this.items;
}
var stack = this.items;
// remove any matching items from the stack if option is set
if (this.filterDuplicates) {
stack = _.pull(this.items, val);
stack = _.filter(stack, function (item) {
return !_.isEqual(item, val);
});
}
stack.unshift(val);
// if maxLength is set, truncate the stack
if (this.maxLength > 0) {
stack = stack.slice(0, this.maxLength);
}
// persist the stack
storage.set(this.name, stack);
return this.items = stack;
};
PersistedLog.prototype.get = function () {
return this.items;
};
return PersistedLog;
});
});

View file

@ -70,7 +70,7 @@
'specs/filters/moment',
'specs/filters/start_from',
'specs/services/storage',
'specs/services/history',
'specs/services/persisted_log',
'specs/utils/datemath',
'specs/utils/interval',
'specs/utils/versionmath',

View file

@ -3,7 +3,7 @@ define(function (require) {
var storage;
var config;
var history;
var PersistedLog;
var historyName = 'testHistory';
var historyLimit = 10;
@ -13,10 +13,10 @@ define(function (require) {
{ first: 'bruce', last: 'wayne' }
];
require('components/history/history');
require('components/persisted_log/persisted_log');
function init() {
module('kibana/history', function ($provide) {
module('kibana/persisted_log', function ($provide) {
// mock storage service
$provide.service('storage', function () {
this.get = sinon.stub();
@ -24,47 +24,41 @@ define(function (require) {
this.remove = sinon.spy();
this.clear = sinon.spy();
});
// mock config service
$provide.service('config', function () {
this.get = sinon.stub().returns(historyLimit);
});
});
inject(function ($injector) {
storage = $injector.get('storage');
config = $injector.get('config');
history = $injector.get('history');
PersistedLog = $injector.get('PersistedLog');
});
}
describe('HistoryFactory', function () {
describe.only('PersistedLog', function () {
beforeEach(function () {
init();
});
describe('expected API', function () {
it('has expected methods', function () {
var h = new history(historyName);
var log = new PersistedLog(historyName);
expect(h.add).to.be.a('function');
expect(h.get).to.be.a('function');
expect(log.add).to.be.a('function');
expect(log.get).to.be.a('function');
});
});
describe('internal functionality', function () {
it('reads from storage', function () {
var h = new history(historyName);
var log = new PersistedLog(historyName);
expect(storage.get.calledOnce).to.be(true);
expect(storage.get.calledWith(historyName)).to.be(true);
});
it('writes to storage', function () {
var h = new history(historyName);
var log = new PersistedLog(historyName);
var newItem = { first: 'diana', last: 'prince' };
var data = h.add(newItem);
var data = log.add(newItem);
expect(storage.set.calledOnce).to.be(true);
expect(data).to.eql([newItem]);
@ -74,26 +68,26 @@ define(function (require) {
describe('persisting data', function () {
it('fetches records from storage', function () {
storage.get.returns(payload);
var h = new history(historyName);
var log = new PersistedLog(historyName);
var items = h.get();
var items = log.get();
expect(items.length).to.equal(3);
expect(items).to.eql(payload);
});
it('prepends new records', function () {
storage.get.returns(payload);
var h = new history(historyName);
storage.get.returns(payload.slice(0));
var log = new PersistedLog(historyName);
var newItem = { first: 'selina', last: 'kyle' };
var items = h.add(newItem);
expect(items.length).to.equal(4);
var items = log.add(newItem);
expect(items.length).to.equal(payload.length + 1);
expect(items[0]).to.eql(newItem);
});
});
describe('stack limit', function () {
it('should observe the stack limit config', function () {
describe('stack options', function () {
it('should observe the maxLength option', function () {
var bulkData = [];
for (var i = 0; i < historyLimit; i++) {
@ -101,27 +95,20 @@ define(function (require) {
}
storage.get.returns(bulkData);
var h = new history(historyName);
h.add(['new array 1']);
var items = h.add(['new array 2']);
var log = new PersistedLog(historyName, { maxLength: historyLimit });
log.add(['new array 1']);
var items = log.add(['new array 2']);
expect(items.length).to.equal(historyLimit);
});
it('should observe the passed stack limit', function () {
var customHistoryLimit = 15;
var bulkData = [];
it('should observe the filterDuplicates option', function () {
storage.get.returns(payload.slice(0));
var log = new PersistedLog(historyName, { filterDuplicates: true });
var newItem = payload[1];
for (var i = 0; i < customHistoryLimit; i++) {
bulkData.push('record ' + i);
}
storage.get.returns(bulkData);
var h = new history(historyName, customHistoryLimit);
h.add('new string 1');
var items = h.add('new string 2');
expect(items.length).to.equal(customHistoryLimit);
var items = log.add(newItem);
expect(items.length).to.equal(payload.length);
});
});
});