Skip to content

Commit

Permalink
adding additional vis unit tests (elastic#14031)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored and chrisronline committed Nov 20, 2017
1 parent 22d606f commit b6d7e30
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/ui/public/vis/__tests__/response_handlers/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ngMock from 'ng_mock';
import expect from 'expect.js';
import { BasicResponseHandlerProvider } from 'ui/vis/response_handlers/basic';
import { VisProvider } from 'ui/vis';
import fixtures from 'fixtures/fake_hierarchical_data';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';

const rowAgg = [
{ id: 'agg_1', type: 'avg', schema: 'metric', params: { field: 'bytes' } },
{ id: 'agg_2', type: 'terms', schema: 'split', params: { field: 'extension', rows: true } },
{ id: 'agg_3', type: 'terms', schema: 'segment', params: { field: 'machine.os' } },
{ id: 'agg_4', type: 'terms', schema: 'segment', params: { field: 'geo.src' } }
];


describe('Basic Response Handler', function () {
let basicResponseHandler;
let indexPattern;
let Vis;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
basicResponseHandler = Private(BasicResponseHandlerProvider).handler;
Vis = Private(VisProvider);
indexPattern = Private(FixturesStubbedLogstashIndexPatternProvider);
}));

it('calls hierarchical converter if isHierarchical is set to true', () => {
const vis = new Vis(indexPattern, {
type: 'pie',
aggs: rowAgg
});
basicResponseHandler(vis, fixtures.threeTermBuckets).then(data => {
expect(data).to.not.be.an('undefined');
expect(data.rows[0].slices).to.not.be.an('undefined');
expect(data.rows[0].series).to.be.an('undefined');
});
});

it('returns empty object if conversion failed', () => {
basicResponseHandler({}, {}).then(data => {
expect(data).to.not.be.an('undefined');
expect(data.rows).to.equal([]);
});
});

it('returns converted data', () => {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: rowAgg.slice(0, 3)
});
basicResponseHandler(vis, fixtures.threeTermBuckets).then(data => {
expect(data).to.not.be.an('undefined');
expect(data.rows[0].slices).to.be.an('undefined');
expect(data.rows[0].series).to.not.be.an('undefined');
});
});

});
41 changes: 41 additions & 0 deletions src/ui/public/vis/__tests__/vis_types/base_vis_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import ngMock from 'ng_mock';
import expect from 'expect.js';
import { VisTypeProvider } from 'ui/vis/vis_types/base_vis_type';

describe('Base Vis Type', function () {
let BaseVisType;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
BaseVisType = Private(VisTypeProvider);
}));

describe('initialization', () => {
it('should throw if mandatory properties are missing', () => {
expect(() => {
new BaseVisType({});
}).to.throwError('vis_type must define its name');

expect(() => {
new BaseVisType({ name: 'test' });
}).to.throwError('vis_type must define its title');

expect(() => {
new BaseVisType({ name: 'test', title: 'test' });
}).to.throwError('vis_type must define its description');

expect(() => {
new BaseVisType({ name: 'test', title: 'test', description: 'test' });
}).to.throwError('vis_type must define its icon or image');

expect(() => {
new BaseVisType({ name: 'test', title: 'test', description: 'test', icon: 'test' });
}).to.throwError('vis_type must define visualization controller');

expect(() => {
new BaseVisType({ name: 'test', title: 'test', description: 'test', icon: 'test', visualization: {} });
}).to.not.throwError();
});
});

});
57 changes: 57 additions & 0 deletions src/ui/public/vis/__tests__/vis_types/react_vis_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import ngMock from 'ng_mock';
import expect from 'expect.js';
import { ReactVisTypeProvider } from 'ui/vis/vis_types/react_vis_type';

describe('React Vis Type', function () {
let ReactVisType;

const visConfig = {
name: 'test',
title: 'test',
description: 'test',
icon: 'test',
visConfig: { component: 'test' },
type: { visConfig: { component: 'test' } }
};

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
ReactVisType = Private(ReactVisTypeProvider);
}));

describe('initialization', () => {
it('should throw if component is not set', () => {
expect(() => {
new ReactVisType({});
}).to.throwError();
});

it('creates react controller', () => {
const visType = new ReactVisType(visConfig);
expect(visType.visualization).to.not.be.an('undefined');
});
});

describe('controller render method', () => {
let vis;
beforeEach(() => {
const visType = new ReactVisType(visConfig);
const Vis = visType.visualization;

vis = new Vis(window.document.body, {});
});

it('rejects if data is not provided', () => {
vis.render().then(() => {
expect('promise was not rejected').to.equal(false);
}).catch(() => {});
});

it('renders the component', () => {
expect(() => {
vis.render({});
}).to.not.throwError();
});

});
});
90 changes: 90 additions & 0 deletions src/ui/public/vis/__tests__/vis_types/vislib_vis_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import ngMock from 'ng_mock';
import expect from 'expect.js';
import { VislibVisTypeProvider } from 'ui/vis/vis_types/vislib_vis_type';

describe('Vislib Vis Type', function () {
let VislibVisType;

const visConfig = {
name: 'test',
title: 'test',
description: 'test',
icon: 'test',
visConfig: { component: 'test' },
type: { visConfig: { component: 'test' } }
};

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
VislibVisType = Private(VislibVisTypeProvider);
}));

describe('initialization', () => {
it('should set the basic response handler if not set', () => {
const visType = new VislibVisType(visConfig);
expect(visType.responseHandler).to.equal('basic');
});

it('should not change response handler if its already set', () => {
visConfig.responseHandler = 'none';
const visType = new VislibVisType(visConfig);
expect(visType.responseHandler).to.equal('none');
});

it('creates vislib controller', () => {
visConfig.responseHandler = 'none';
const visType = new VislibVisType(visConfig);
expect(visType.visualization).to.not.be.undefined;
});
});

describe('controller', function () {
it('constructor sets vis and element properties', () => {
visConfig.responseHandler = 'none';
const visType = new VislibVisType(visConfig);
const Vis = visType.visualization;
const vis = new Vis(window.document.body, {});
expect(vis.el).to.not.be.undefined;
expect(vis.vis).to.not.be.undefined;
});
});

describe('render method', () => {
let vis;
beforeEach(() => {
visConfig.responseHandler = 'none';
const visType = new VislibVisType(visConfig);
const Vis = visType.visualization;
vis = new Vis(window.document.body, { params: {} });
});

it('rejects if response is not provided', () => {
vis.render().then(() => {
expect('promise was not rejected').to.equal(false);
}).catch(() => {});
});

it('creates new vislib vis', () => {
vis.render({});
expect(vis.vis.vislibVis).to.not.be.undefined;
});

});

describe('destroy method', () => {
let vis;
beforeEach(() => {
visConfig.responseHandler = 'none';
const visType = new VislibVisType(visConfig);
const Vis = visType.visualization;
vis = new Vis(window.document.body, { params: {} });
});

it('destroys vislib vis', () => {
vis.render({}).then(() => {
vis.destroy();
expect(vis.vis.vislibVis).to.be.undefined;
});
});
});
});
23 changes: 23 additions & 0 deletions src/ui/public/vis/editors/default/__tests__/nesting_indicator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';

describe('nestingIndicator directive', () => {
let element;
let $rootScope;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject((_$rootScope_, _$compile_) => {
$rootScope = _$rootScope_;

$rootScope.list = ['test'];
$rootScope.item = 'test';
element = _$compile_('<nesting-indicator item="item" list="list">')($rootScope);
}));

it('should update background color on list change', () => {
$rootScope.list.push('test2');
$rootScope.$digest();
expect(element.find('span').length).to.equal(1);
});

});
1 change: 1 addition & 0 deletions src/ui/public/vis/vis_types/vislib_vis_type.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function VislibVisTypeProvider(Private) {
this.vis.vislibVis.off('brush', this.vis.API.events.brush);
this.vis.vislibVis.off('click', this.vis.API.events.filter);
this.vis.vislibVis.destroy();
delete this.vis.vislibVis;
}
}
}
Expand Down

0 comments on commit b6d7e30

Please sign in to comment.