UNPKG

@spalger/kibana

Version:

Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic

117 lines (92 loc) 3.11 kB
var sinon = require('sinon'); var expect = require('expect.js'); var ngMock = require('ngMock'); var storage; var config; var PersistedLog; var historyName = 'testHistory'; var historyLimit = 10; var payload = [ { first: 'clark', last: 'kent' }, { first: 'peter', last: 'parker' }, { first: 'bruce', last: 'wayne' } ]; require('ui/persisted_log'); function init() { ngMock.module('kibana/persisted_log', function ($provide) { // mock storage service $provide.service('localStorage', function () { this.get = sinon.stub(); this.set = sinon.stub(); this.remove = sinon.spy(); this.clear = sinon.spy(); }); }); ngMock.inject(function ($injector) { storage = $injector.get('localStorage'); PersistedLog = $injector.get('PersistedLog'); }); } describe('PersistedLog', function () { beforeEach(function () { init(); }); describe('expected API', function () { it('has expected methods', function () { var log = new PersistedLog(historyName); expect(log.add).to.be.a('function'); expect(log.get).to.be.a('function'); }); }); describe('internal functionality', function () { it('reads from storage', function () { 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 log = new PersistedLog(historyName); var newItem = { first: 'diana', last: 'prince' }; var data = log.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 log = new PersistedLog(historyName); var items = log.get(); expect(items.length).to.equal(3); expect(items).to.eql(payload); }); it('prepends new records', function () { storage.get.returns(payload.slice(0)); var log = new PersistedLog(historyName); var newItem = { first: 'selina', last: 'kyle' }; var items = log.add(newItem); expect(items.length).to.equal(payload.length + 1); expect(items[0]).to.eql(newItem); }); }); describe('stack options', function () { it('should observe the maxLength option', function () { var bulkData = []; for (var i = 0; i < historyLimit; i++) { bulkData.push(['record ' + i]); } storage.get.returns(bulkData); 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 filterDuplicates option', function () { storage.get.returns(payload.slice(0)); var log = new PersistedLog(historyName, { filterDuplicates: true }); var newItem = payload[1]; var items = log.add(newItem); expect(items.length).to.equal(payload.length); }); }); });