Commit dfd1d096 by Torkel Ödegaard

added unit tests for filterSrv

parent 5ac6d28a
......@@ -13,10 +13,8 @@ function (angular, _, config) {
module.service('datasourceSrv', function($q, filterSrv, $http, GraphiteDatasource, InfluxDatasource) {
this.init = function() {
var defaultDatasource = _.findWhere(_.values(config.datasources), { default: true } );
this.default = this.datasourceFactory(defaultDatasource);
};
this.datasourceFactory = function(ds) {
......
......@@ -32,12 +32,12 @@ define([
};
if (self.list.length) {
this.updateTemplateData(true);
this._updateTemplateData(true);
}
};
this.updateTemplateData = function(initial) {
self.filterTemplateData = {};
this._updateTemplateData = function(initial) {
self._filterTemplateData = {};
_.each(self.list, function(filter) {
if (initial) {
......@@ -46,18 +46,17 @@ define([
filter.current = { text: urlValue, value: urlValue };
}
}
if (!filter.current || !filter.current.value) {
return;
}
self.filterTemplateData[filter.name] = filter.current.value;
self._filterTemplateData[filter.name] = filter.current.value;
});
};
this.filterOptionSelected = function(filter, option) {
filter.current = option;
this.updateTemplateData();
this._updateTemplateData();
dashboard.refresh();
};
......@@ -70,7 +69,7 @@ define([
return target;
}
return _.template(target, self.filterTemplateData, self.templateSettings);
return _.template(target, self._filterTemplateData, self.templateSettings);
};
this.remove = function(filter) {
......
......@@ -14,8 +14,7 @@ module.exports = function(config) {
],
// list of files to exclude
exclude: [
],
exclude: [],
reporters: ['progress'],
port: 9876,
......
define([],
function() {
return {
create: function() {
return {
refresh: function() {},
current: {
title: "",
tags: [],
style: "dark",
timezone: 'browser',
editable: true,
failover: false,
panel_hints: true,
rows: [],
pulldowns: [ { type: 'templating' }, { type: 'annotations' } ],
nav: [ { type: 'timepicker' } ],
services: {},
loader: {
save_gist: false,
save_elasticsearch: true,
save_local: true,
save_default: true,
save_temp: true,
save_temp_ttl_enable: true,
save_temp_ttl: '30d',
load_gist: false,
load_elasticsearch: true,
load_elasticsearch_size: 20,
load_local: false,
hide: false
},
refresh: false
}
}
}
}
});
define([
'angular',
'angularMocks',
'panels/graphite/module'
], function(angular) {
/* describe('controller', function() {
var scope, metricCtrl;
beforeEach(function() {
angular.mock.inject(function($rootScope, $controller) {
scope = $rootScope.$new();
metricCtrl = $controller('kibana.panels.graphite.graphite', {
$scope: scope
});
});
});
it('should work', function() {
metricCtrl.toggleYAxis({alias:'myAlias'});
scope.panel.aliasYAxis['myAlias'].should.be(2);
});
});*/
});
define([
'mocks/dashboard-mock',
'underscore',
'services/filterSrv'
], function(dashboardMock, _) {
describe('filterSrv', function() {
var _filterSrv;
beforeEach(module('kibana.services'));
beforeEach(module(function($provide){
$provide.value('dashboard', dashboardMock.create());
}));
beforeEach(inject(function(filterSrv) {
_filterSrv = filterSrv;
}));
describe('init', function() {
beforeEach(function() {
_filterSrv.add({ name: 'test', current: { value: 'oogle' } });
_filterSrv.init();
});
it('should initialize template data', function() {
var target = _filterSrv.applyFilterToTarget('this.[[test]].filters');
expect(target).to.be('this.oogle.filters');
});
});
describe.only('filterOptionSelected', function() {
beforeEach(function() {
_filterSrv.add({ name: 'test' });
_filterSrv.filterOptionSelected(_filterSrv.list[0], { value: 'muuuu' });
});
it('should set current value and update template data', function() {
var target = _filterSrv.applyFilterToTarget('this.[[test]].filters');
expect(target).to.be('this.muuuu.filters');
});
});
describe('timeRange', function() {
it('should return unparsed when parse is false', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(false);
expect(time.from).to.be('now');
expect(time.to).to.be('now-1h');
});
it('should return parsed when parse is true', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(true);
expect(_.isDate(time.from)).to.be(true);
expect(_.isDate(time.to)).to.be(true);
});
});
});
});
define([
'mocks/dashboard-mock',
'underscore',
'services/filterSrv'
], function(dashboardMock, _) {
describe('filterSrv', function() {
var _filterSrv;
beforeEach(module('kibana.services'));
beforeEach(module(function($provide){
$provide.value('dashboard', dashboardMock.create());
}));
beforeEach(inject(function(filterSrv) {
_filterSrv = filterSrv;
}));
describe('init', function() {
beforeEach(function() {
_filterSrv.add({ name: 'test', current: { value: 'oogle' } });
_filterSrv.init();
});
it('should initialize template data', function() {
var target = _filterSrv.applyFilterToTarget('this.[[test]].filters');
expect(target).to.be('this.oogle.filters');
});
});
describe.only('filterOptionSelected', function() {
beforeEach(function() {
_filterSrv.add({ name: 'test' });
_filterSrv.filterOptionSelected(_filterSrv.list[0], { value: 'muuuu' });
});
it('should set current value and update template data', function() {
var target = _filterSrv.applyFilterToTarget('this.[[test]].filters');
expect(target).to.be('this.muuuu.filters');
});
});
describe('timeRange', function() {
it('should return unparsed when parse is false', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(false);
expect(time.from).to.be('now');
expect(time.to).to.be('now-1h');
});
it('should return parsed when parse is true', function() {
_filterSrv.setTime({from: 'now', to: 'now-1h' });
var time = _filterSrv.timeRange(true);
expect(_.isDate(time.from)).to.be(true);
expect(_.isDate(time.to)).to.be(true);
});
});
});
});
......@@ -3,6 +3,7 @@ require.config({
paths: {
specs: '../test/specs',
mocks: '../test/mocks',
config: '../config.sample',
kbn: 'components/kbn',
......@@ -102,10 +103,20 @@ require.config({
});
require([
'specs/lexer-specs',
'specs/parser-specs',
'specs/gfunc-specs',
'specs/ctrl-specs',
], function () {
window.__karma__.start();
});
\ No newline at end of file
'angular',
'angularMocks',
], function(angular) {
angular.module('kibana', []);
angular.module('kibana.services', []);
require([
'specs/lexer-specs',
'specs/parser-specs',
'specs/gfunc-specs',
'specs/filterSrv-specs',
], function () {
window.__karma__.start();
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment