Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Fixes #5586
  • Loading branch information
jbudz committed Dec 9, 2015
1 parent eb821db commit 7f6a661
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
63 changes: 63 additions & 0 deletions src/ui/public/utils/__tests__/scanner.js
@@ -0,0 +1,63 @@
var Scanner = require('ui/utils/scanner');
var expect = require('expect.js');
var elasticsearch = require('elasticsearch-browser');
var sinon = require('sinon');

var es = new elasticsearch.Client({
host: 'http://localhost:9210',
});


describe('Scanner', function () {
describe('initialization', function () {
it('should throw errors if missing arguments on initialization', function () {
expect(() => new Scanner()).to.throwError();
expect(() => new Scanner(es)).to.throwError();
expect(() => new Scanner(es, {
index: 'foo',
type: 'bar'
})).not.to.throwError();
});
});

describe('scan', function () {
let search;
let scroll;
let scanner;
let mockSearch = {'_scroll_id':'abc','took':1,'timed_out':false,'_shards':{'total':1,'successful':1,'failed':0},'hits':{'total':2,'max_score':0.0,'hits':[]}}; // eslint-disable-line max-len
let mockScroll = {'took':1,'timed_out':false,'_shards':{'total':1,'successful':1,'failed':0},'hits':{'total':2,'max_score':0.0,'hits':['one', 'two']}}; // eslint-disable-line max-len

beforeEach(function () {
scanner = new Scanner(es, {
index: 'foo',
type: 'bar'
});
search = sinon.stub(scanner.client, 'search', (req, cb) => cb(null, mockSearch));
scroll = sinon.stub(scanner.client, 'scroll', (req, cb) => cb(null, mockScroll));
});

it('should search and then scroll for results', function () {
return scanner.scanAndMap('')
.then(function (error, response) {
expect(search.called).to.be(true);
expect(scroll.called).to.be(true);
});
});

it('should map results if a function is provided', function () {
return scanner.scanAndMap(null, null, function (hit) {
return hit.toUpperCase();
})
.then(function (response) {
expect(response.hits[0]).to.be('ONE');
expect(response.hits[1]).to.be('TWO');
});
});

afterEach(function () {
search.restore();
scroll.restore();
});
});

});
2 changes: 1 addition & 1 deletion src/ui/public/utils/scanner.js
@@ -1,6 +1,6 @@
const _ = require('lodash');

let Scanner = function (client, {index, type}) {
let Scanner = function (client, {index, type} = {}) {
if (!index) throw new Error('Expected index');
if (!type) throw new Error('Expected type');
if (!client) throw new Error('Expected client');
Expand Down

0 comments on commit 7f6a661

Please sign in to comment.