Add tests, speed up tilemap tests

This commit is contained in:
Rashid Khan 2015-04-30 15:47:24 -07:00
parent 59888b9834
commit 1d35ea8d31
5 changed files with 352 additions and 140 deletions

View file

@ -3,11 +3,12 @@ define(function (require) {
return function () {
return function ($state) {
if (!_.isObject($state)) throw new Error ('pushFilters requires a state object');
return function (filter, negate, index) {
// Hierarchical and tabular data set their aggConfigResult parameter
// differently because of how the point is rewritten between the two. So
// we need to check if the point.orig is set, if not use try the point.aggConfigResult
var filters = _.clone($state.filters);
var filters = _.clone($state.filters || []);
var pendingFilter = { meta: { negate: negate, index: index }};
_.extend(pendingFilter, filter);
filters.push(pendingFilter);

View file

@ -0,0 +1,62 @@
define(function (require) {
describe('Filter Bar Directive', function () {
describe('mapGeoBoundingBox()', function () {
var sinon = require('test_utils/auto_release_sinon');
var mapGeoBoundingBox, $rootScope, indexPattern, getIndexPatternStub;
beforeEach(module('kibana'));
beforeEach(function () {
getIndexPatternStub = sinon.stub();
module('kibana/courier', function ($provide) {
$provide.service('courier', function () {
var courier = { indexPatterns: { get: getIndexPatternStub } };
return courier;
});
});
});
beforeEach(inject(function (Private, _$rootScope_, Promise) {
mapGeoBoundingBox = Private(require('components/filter_bar/lib/mapGeoBoundingBox'));
$rootScope = _$rootScope_;
indexPattern = Private(require('fixtures/stubbed_logstash_index_pattern'));
getIndexPatternStub.returns(Promise.resolve(indexPattern));
}));
it('should return the key and value for matching filters with bounds', function (done) {
var filter = {
meta: {
index: 'logstash-*'
},
geo_bounding_box: {
point: { // field name
top_left: {
lat: 5,
lon: 10
},
bottom_right: {
lat: 15,
lon: 20
}
}
}
};
mapGeoBoundingBox(filter).then(function (result) {
expect(result).to.have.property('key', 'point');
expect(result).to.have.property('value', '{"lat":5,"lon":10} to {"lat":15,"lon":20}');
done();
});
$rootScope.$apply();
});
it('should return undefined for none matching', function (done) {
var filter = { meta: { index: 'logstash-*' }, query: { query_string: { query: 'foo:bar' } } };
mapGeoBoundingBox(filter).catch(function (result) {
expect(result).to.be(filter);
done();
});
$rootScope.$apply();
});
});
});
});

View file

@ -0,0 +1,68 @@
define(function (require) {
describe('Filter Bar pushFilter()', function () {
var _ = require('lodash');
var pushFilterFn;
beforeEach(module('kibana'));
beforeEach(inject(function (Private, $injector) {
pushFilterFn = Private(require('components/filter_bar/push_filter'));
}));
it('is a function that returns a function', function () {
expect(pushFilterFn).to.be.a(Function);
expect(pushFilterFn({})).to.be.a(Function);
});
it('throws an error if passed something besides an object', function () {
expect(pushFilterFn).withArgs(true).to.throwError();
});
describe('pushFilter($state)()', function () {
var $state;
var pushFilter;
var filter;
beforeEach(inject(function (Private, $injector) {
$state = {filters:[]};
pushFilter = pushFilterFn($state);
filter = {query: {query_string: {query: ''}}};
}));
it('should create the filters property it needed', function () {
var altState = {};
pushFilterFn(altState)(filter);
expect(altState.filters).to.be.an(Array);
});
it('should replace the filters property instead of modifying it', function () {
// If we push directly instead of using pushFilter a $watch('filters') does not trigger
var oldFilters;
oldFilters = $state.filters;
$state.filters.push(filter);
expect($state.filters).to.equal(oldFilters); // Same object
oldFilters = $state.filters;
pushFilter(filter);
expect($state.filters).to.not.equal(oldFilters); // New object!
});
it('should add meta data to the filter', function () {
pushFilter(filter, true, 'myIndex');
expect($state.filters[0].meta).to.be.an(Object);
expect($state.filters[0].meta.negate).to.be(true);
expect($state.filters[0].meta.index).to.be('myIndex');
pushFilter(filter, false, 'myIndex');
expect($state.filters[1].meta.negate).to.be(false);
});
});
});
});

View file

@ -9,91 +9,118 @@ define(function (require) {
angular.module('DispatchClass', ['kibana']);
describe('VisLib Dispatch Class Test Suite', function () {
var vis;
beforeEach(function () {
module('AreaChartFactory');
});
beforeEach(function () {
inject(function (Private) {
vis = Private(require('vislib_fixtures/_vis_fixture'))();
require('css!components/vislib/styles/main');
vis.on('brush', _.noop);
vis.render(data);
});
});
afterEach(function () {
function destroyVis(vis) {
$(vis.el).remove();
vis = null;
});
}
describe('addEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var addEvent = chart.events.addEvent;
expect(_.isFunction(addEvent('click', _.noop))).to.be(true);
describe('Stock event handlers', function () {
var vis;
beforeEach(function () {
module('AreaChartFactory');
});
beforeEach(function () {
inject(function (Private) {
vis = Private(require('vislib_fixtures/_vis_fixture'))();
require('css!components/vislib/styles/main');
vis.on('brush', _.noop);
vis.render(data);
});
});
afterEach(function () {
destroyVis(vis);
});
describe('addEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var addEvent = chart.events.addEvent;
expect(_.isFunction(addEvent('click', _.noop))).to.be(true);
});
});
});
describe('addHoverEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var hover = chart.events.addHoverEvent;
expect(_.isFunction(hover)).to.be(true);
});
});
it('should attach a hover event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.hover)).to.be(true);
});
});
});
describe('addClickEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var click = chart.events.addClickEvent;
expect(_.isFunction(click)).to.be(true);
});
});
it('should attach a click event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.click)).to.be(true);
});
});
});
describe('addBrushEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var brush = chart.events.addBrushEvent;
expect(_.isFunction(brush)).to.be(true);
});
});
it('should attach a brush event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.brush)).to.be(true);
});
});
});
describe('addMousePointer method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var pointer = chart.events.addMousePointer;
expect(_.isFunction(pointer)).to.be(true);
});
});
});
});
describe('addHoverEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var hover = chart.events.addHoverEvent;
describe('Custom event handlers', function () {
it('should attach whatever gets passed on vis.on() to dispatch', function (done) {
var vis;
var chart;
module('AreaChartFactory');
inject(function (Private) {
vis = Private(require('vislib_fixtures/_vis_fixture'))();
vis.on('someEvent', _.noop);
vis.render(data);
expect(_.isFunction(hover)).to.be(true);
});
});
vis.handler.charts.forEach(function (chart) {
expect(chart.events.dispatch.someEvent).to.be.a(Function);
});
it('should attach a hover event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.hover)).to.be(true);
});
});
});
describe('addClickEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var click = chart.events.addClickEvent;
expect(_.isFunction(click)).to.be(true);
});
});
it('should attach a click event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.click)).to.be(true);
});
});
});
describe('addBrushEvent method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var brush = chart.events.addBrushEvent;
expect(_.isFunction(brush)).to.be(true);
});
});
it('should attach a brush event', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.events.dispatch.brush)).to.be(true);
});
});
});
describe('addMousePointer method', function () {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
var pointer = chart.events.addMousePointer;
expect(_.isFunction(pointer)).to.be(true);
destroyVis(vis);
done();
});
});
});

View file

@ -11,90 +11,144 @@ define(function (require) {
require('vislib_fixtures/mock_data/geohash/_rows')
];
var names = ['geojson', 'columns', 'rows'];
var mapTypes = ['Scaled Circle Markers', 'Shaded Circle Markers', 'Shaded Geohash Grid', 'Pins'];
// TODO: Test the specific behavior of each these
var mapTypes = ['Scaled Circle Markers', 'Shaded Circle Markers', 'Shaded Geohash Grid'];
angular.module('TileMapFactory', ['kibana']);
dataArray.forEach(function (data, i) {
mapTypes.forEach(function (type, j) {
function bootstrapAndRender(data, type) {
var vis;
var visLibParams = {
isDesaturated: true,
type: 'tile_map',
mapType: type
};
describe('TileMap Test Suite for ' + mapTypes[j] + ' with ' + names[i] + ' data', function () {
var vis;
var visLibParams = {
isDesaturated: true,
type: 'tile_map',
mapType: type
};
module('TileMapFactory');
inject(function (Private) {
vis = Private(require('vislib_fixtures/_vis_fixture'))(visLibParams);
require('css!components/vislib/styles/main');
vis.render(data);
});
beforeEach(function () {
module('TileMapFactory');
});
return vis;
beforeEach(function () {
inject(function (Private) {
vis = Private(require('vislib_fixtures/_vis_fixture'))(visLibParams);
require('css!components/vislib/styles/main');
vis.render(data);
});
});
}
afterEach(function () {
$(vis.el).remove();
vis = null;
});
function destroyVis(vis) {
$(vis.el).remove();
vis = null;
}
describe('draw method', function () {
var leafletContainer;
var isDrawn;
describe('TileMap Tests', function () {
describe('Rendering each types of tile map', function () {
dataArray.forEach(function (data, i) {
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
expect(_.isFunction(chart.draw())).to.be(true);
mapTypes.forEach(function (type, j) {
describe('draw() ' + mapTypes[j] + ' with ' + names[i], function () {
var vis;
beforeEach(function () {
vis = bootstrapAndRender(data, type);
});
});
it('should draw a map', function () {
leafletContainer = $(vis.el).find('.leaflet-container');
isDrawn = (leafletContainer.length > 0);
expect(isDrawn).to.be(true);
});
});
describe('geohashMinDistance method', function () {
it('should return a number', function () {
vis.handler.charts.forEach(function (chart) {
var feature = chart.chartData.geoJson.features[0];
expect(_.isNumber(chart.geohashMinDistance(feature))).to.be(true);
afterEach(function () {
destroyVis(vis);
});
});
});
describe('radiusScale method', function () {
it('should return a number', function () {
vis.handler.charts.forEach(function (chart) {
var count = Math.random() * 50;
var max = 50;
var precision = 1;
var feature = chart.chartData.geoJson.features[0];
expect(_.isNumber(chart.radiusScale(count, max, feature))).to.be(true);
it('should return a function', function () {
vis.handler.charts.forEach(function (chart) {
expect(chart.draw()).to.be.a(Function);
});
});
});
});
describe('quantizeColorScale method', function () {
it('should return a hex color', function () {
vis.handler.charts.forEach(function (chart) {
var reds = ['#fed976', '#feb24c', '#fd8d3c', '#f03b20', '#bd0026'];
var count = Math.random() * 300;
var min = 0;
var max = 300;
expect(_.indexOf(reds, chart.quantizeColorScale(count, min, max))).to.not.be(-1);
it('should create .leaflet-container as a by product of map rendering', function () {
expect($(vis.el).find('.leaflet-container').length).to.be.above(0);
});
});
});
});
});
describe('Leaflet controls', function () {
var vis;
var leafletContainer;
beforeEach(function () {
vis = bootstrapAndRender(dataArray[0], 'Scaled Circle Markers');
leafletContainer = $(vis.el).find('.leaflet-container');
});
afterEach(function () {
destroyVis(vis);
});
it('should attach the zoom controls', function () {
expect(leafletContainer.find('.leaflet-control-zoom-in').length).to.be(1);
expect(leafletContainer.find('.leaflet-control-zoom-out').length).to.be(1);
});
it('should attach the filter drawing button', function () {
expect(leafletContainer.find('.leaflet-draw').length).to.be(1);
});
it('should attach the filter drawing button', function () {
expect(leafletContainer.find('.leaflet-control-fit').length).to.be(1);
});
});
// Probably only neccesary to test one of these as we already know the the map will render
describe('Methods', function () {
var vis;
var leafletContainer;
beforeEach(function () {
vis = bootstrapAndRender(dataArray[0], 'Scaled Circle Markers');
leafletContainer = $(vis.el).find('.leaflet-container');
});
afterEach(function () {
destroyVis(vis);
});
describe('geohashMinDistance method', function () {
it('should return a number', function () {
vis.handler.charts.forEach(function (chart) {
var feature = chart.chartData.geoJson.features[0];
expect(_.isNumber(chart.geohashMinDistance(feature))).to.be(true);
});
});
});
describe('radiusScale method', function () {
it('should return a number', function () {
vis.handler.charts.forEach(function (chart) {
var count = Math.random() * 50;
var max = 50;
var precision = 1;
var feature = chart.chartData.geoJson.features[0];
expect(_.isNumber(chart.radiusScale(count, max, feature))).to.be(true);
});
});
});
describe('quantizeColorScale method', function () {
it('should return a hex color', function () {
vis.handler.charts.forEach(function (chart) {
var reds = ['#fed976', '#feb24c', '#fd8d3c', '#f03b20', '#bd0026'];
var count = Math.random() * 300;
var min = 0;
var max = 300;
expect(_.indexOf(reds, chart.quantizeColorScale(count, min, max))).to.not.be(-1);
});
});
});
});
});
});