[courier] added tests for requesQueue, DocSource, and SearchSource
This commit is contained in:
parent
bf556ba2b4
commit
49ab884404
3 changed files with 210 additions and 0 deletions
92
src/ui/public/courier/__tests__/requestQueue.js
Normal file
92
src/ui/public/courier/__tests__/requestQueue.js
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
import ngMock from 'ngMock';
|
||||||
|
import expect from 'expect.js';
|
||||||
|
import sinon from 'auto-release-sinon';
|
||||||
|
|
||||||
|
import RequestQueueProv from '../_request_queue';
|
||||||
|
import SearchStrategyProv from '../fetch/strategy/search';
|
||||||
|
import DocStrategyProv from '../fetch/strategy/doc';
|
||||||
|
|
||||||
|
describe('Courier Request Queue', function () {
|
||||||
|
let docStrategy;
|
||||||
|
let requestQueue;
|
||||||
|
let searchStrategy;
|
||||||
|
|
||||||
|
beforeEach(ngMock.module('kibana'));
|
||||||
|
beforeEach(ngMock.inject(function (Private) {
|
||||||
|
docStrategy = Private(DocStrategyProv);
|
||||||
|
requestQueue = Private(RequestQueueProv);
|
||||||
|
searchStrategy = Private(SearchStrategyProv);
|
||||||
|
}));
|
||||||
|
|
||||||
|
class MockReq {
|
||||||
|
constructor(strategy, startable = true) {
|
||||||
|
this.strategy = strategy;
|
||||||
|
this.source = {};
|
||||||
|
this.canStart = sinon.stub().returns(startable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('#getStartable(strategy)', function () {
|
||||||
|
it('only returns requests that match one of the passed strategies', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy),
|
||||||
|
new MockReq(searchStrategy),
|
||||||
|
new MockReq(searchStrategy),
|
||||||
|
new MockReq(searchStrategy)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.getStartable(docStrategy)).to.have.length(1);
|
||||||
|
expect(requestQueue.getStartable(searchStrategy)).to.have.length(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns all requests when no strategy passed', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy),
|
||||||
|
new MockReq(searchStrategy)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.getStartable()).to.have.length(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns only startable requests', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy, true),
|
||||||
|
new MockReq(searchStrategy, false)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.getStartable()).to.have.length(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#get(strategy)', function () {
|
||||||
|
it('only returns requests that match one of the passed strategies', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy),
|
||||||
|
new MockReq(searchStrategy),
|
||||||
|
new MockReq(searchStrategy),
|
||||||
|
new MockReq(searchStrategy)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.get(docStrategy)).to.have.length(1);
|
||||||
|
expect(requestQueue.get(searchStrategy)).to.have.length(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns all requests when no strategy passed', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy),
|
||||||
|
new MockReq(searchStrategy)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.get()).to.have.length(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns startable and not-startable requests', function () {
|
||||||
|
requestQueue.push(
|
||||||
|
new MockReq(docStrategy, true),
|
||||||
|
new MockReq(searchStrategy, false)
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(requestQueue.get()).to.have.length(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
59
src/ui/public/courier/data_source/__tests__/DocSource.js
Normal file
59
src/ui/public/courier/data_source/__tests__/DocSource.js
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import ngMock from 'ngMock';
|
||||||
|
import expect from 'expect.js';
|
||||||
|
import sinon from 'auto-release-sinon';
|
||||||
|
|
||||||
|
import RequestQueueProv from '../../_request_queue';
|
||||||
|
import DocSourceProv from '../doc_source';
|
||||||
|
|
||||||
|
describe('DocSource', function () {
|
||||||
|
require('testUtils/noDigestPromises').activateForSuite();
|
||||||
|
|
||||||
|
let requestQueue;
|
||||||
|
let DocSource;
|
||||||
|
|
||||||
|
beforeEach(ngMock.module('kibana'));
|
||||||
|
beforeEach(ngMock.inject(function (Private) {
|
||||||
|
requestQueue = Private(RequestQueueProv);
|
||||||
|
DocSource = Private(DocSourceProv);
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('#onUpdate()', function () {
|
||||||
|
it('adds a request to the requestQueue', function () {
|
||||||
|
const source = new DocSource();
|
||||||
|
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
source.onUpdate();
|
||||||
|
expect(requestQueue).to.have.length(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a promise that is resolved with the results', function () {
|
||||||
|
const source = new DocSource();
|
||||||
|
const fakeResults = {};
|
||||||
|
|
||||||
|
const promise = source.onUpdate().then((results) => {
|
||||||
|
expect(results).to.be(fakeResults);
|
||||||
|
});
|
||||||
|
|
||||||
|
requestQueue[0].defer.resolve(fakeResults);
|
||||||
|
return promise;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#destroy()', function () {
|
||||||
|
it('aborts all startable requests', function () {
|
||||||
|
const source = new DocSource();
|
||||||
|
source.onUpdate();
|
||||||
|
sinon.stub(requestQueue[0], 'canStart').returns(true);
|
||||||
|
source.destroy();
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('aborts all non-startable requests', function () {
|
||||||
|
const source = new DocSource();
|
||||||
|
source.onUpdate();
|
||||||
|
sinon.stub(requestQueue[0], 'canStart').returns(false);
|
||||||
|
source.destroy();
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
59
src/ui/public/courier/data_source/__tests__/SearchSource.js
Normal file
59
src/ui/public/courier/data_source/__tests__/SearchSource.js
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
import ngMock from 'ngMock';
|
||||||
|
import expect from 'expect.js';
|
||||||
|
import sinon from 'auto-release-sinon';
|
||||||
|
|
||||||
|
import RequestQueueProv from '../../_request_queue';
|
||||||
|
import SearchSourceProv from '../search_source';
|
||||||
|
|
||||||
|
describe('SearchSource', function () {
|
||||||
|
require('testUtils/noDigestPromises').activateForSuite();
|
||||||
|
|
||||||
|
let requestQueue;
|
||||||
|
let SearchSource;
|
||||||
|
|
||||||
|
beforeEach(ngMock.module('kibana'));
|
||||||
|
beforeEach(ngMock.inject(function (Private) {
|
||||||
|
requestQueue = Private(RequestQueueProv);
|
||||||
|
SearchSource = Private(SearchSourceProv);
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('#onResults()', function () {
|
||||||
|
it('adds a request to the requestQueue', function () {
|
||||||
|
const source = new SearchSource();
|
||||||
|
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
source.onResults();
|
||||||
|
expect(requestQueue).to.have.length(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns a promise that is resolved with the results', function () {
|
||||||
|
const source = new SearchSource();
|
||||||
|
const fakeResults = {};
|
||||||
|
|
||||||
|
const promise = source.onResults().then((results) => {
|
||||||
|
expect(results).to.be(fakeResults);
|
||||||
|
});
|
||||||
|
|
||||||
|
requestQueue[0].defer.resolve(fakeResults);
|
||||||
|
return promise;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#destroy()', function () {
|
||||||
|
it('aborts all startable requests', function () {
|
||||||
|
const source = new SearchSource();
|
||||||
|
source.onResults();
|
||||||
|
sinon.stub(requestQueue[0], 'canStart').returns(true);
|
||||||
|
source.destroy();
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('aborts all non-startable requests', function () {
|
||||||
|
const source = new SearchSource();
|
||||||
|
source.onResults();
|
||||||
|
sinon.stub(requestQueue[0], 'canStart').returns(false);
|
||||||
|
source.destroy();
|
||||||
|
expect(requestQueue).to.have.length(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue