mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
add history service, with tests
This commit is contained in:
parent
e2a605c69e
commit
85a39e2196
2 changed files with 127 additions and 0 deletions
32
src/kibana/components/history/history.js
Normal file
32
src/kibana/components/history/history.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
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;
|
||||
});
|
||||
});
|
95
test/unit/specs/services/history.js
Normal file
95
test/unit/specs/services/history.js
Normal file
|
@ -0,0 +1,95 @@
|
|||
define(function (require) {
|
||||
var sinon = require('sinon/sinon');
|
||||
|
||||
var storage;
|
||||
var config;
|
||||
var history;
|
||||
|
||||
var historyName = 'testHistory';
|
||||
var historyLimit = 10;
|
||||
var payload = [
|
||||
{ first: 'clark', last: 'kent' },
|
||||
{ first: 'peter', last: 'parker' },
|
||||
{ first: 'bruce', last: 'wayne' }
|
||||
];
|
||||
|
||||
require('components/history/history');
|
||||
|
||||
function init() {
|
||||
module('kibana/history', function ($provide) {
|
||||
// mock storage service
|
||||
$provide.service('storage', function () {
|
||||
this.get = sinon.stub();
|
||||
this.set = sinon.stub();
|
||||
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');
|
||||
});
|
||||
}
|
||||
|
||||
describe.only('HistoryFactory', function () {
|
||||
beforeEach(function () {
|
||||
init();
|
||||
});
|
||||
|
||||
describe('expected API', function () {
|
||||
it('has expected methods', function () {
|
||||
var h = new history(historyName);
|
||||
|
||||
expect(h.add).to.be.a('function');
|
||||
expect(h.get).to.be.a('function');
|
||||
});
|
||||
});
|
||||
|
||||
describe('internal functionality', function () {
|
||||
it('reads from storage', function () {
|
||||
var h = new history(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 newItem = { first: 'diana', last: 'prince' };
|
||||
|
||||
var data = h.add(newItem);
|
||||
|
||||
expect(storage.set.calledOnce).to.be(true);
|
||||
expect(data).to.eql([newItem]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('persisting data', function () {
|
||||
it('fetches records from storage', function () {
|
||||
storage.get.returns(payload);
|
||||
var h = new history(historyName);
|
||||
|
||||
var items = h.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);
|
||||
var newItem = { first: 'selina', last: 'kyle' };
|
||||
|
||||
var items = h.add(newItem);
|
||||
expect(items.length).to.equal(4);
|
||||
expect(items[0]).to.eql(newItem);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue