Commit 517ac3f1 by Torkel Ödegaard Committed by Bruce Sherrod

added unit tests for filterSrv

parent 43f1cfba
...@@ -13,10 +13,8 @@ function (angular, _, config) { ...@@ -13,10 +13,8 @@ function (angular, _, config) {
module.service('datasourceSrv', function($q, filterSrv, $http, GraphiteDatasource, InfluxDatasource) { module.service('datasourceSrv', function($q, filterSrv, $http, GraphiteDatasource, InfluxDatasource) {
this.init = function() { this.init = function() {
var defaultDatasource = _.findWhere(_.values(config.datasources), { default: true } ); var defaultDatasource = _.findWhere(_.values(config.datasources), { default: true } );
this.default = this.datasourceFactory(defaultDatasource); this.default = this.datasourceFactory(defaultDatasource);
}; };
this.datasourceFactory = function(ds) { this.datasourceFactory = function(ds) {
......
...@@ -32,12 +32,12 @@ define([ ...@@ -32,12 +32,12 @@ define([
}; };
if (self.list.length) { if (self.list.length) {
this.updateTemplateData(true); this._updateTemplateData(true);
} }
}; };
this.updateTemplateData = function(initial) { this._updateTemplateData = function(initial) {
self.filterTemplateData = {}; self._filterTemplateData = {};
_.each(self.list, function(filter) { _.each(self.list, function(filter) {
if (initial) { if (initial) {
...@@ -46,18 +46,17 @@ define([ ...@@ -46,18 +46,17 @@ define([
filter.current = { text: urlValue, value: urlValue }; filter.current = { text: urlValue, value: urlValue };
} }
} }
if (!filter.current || !filter.current.value) { if (!filter.current || !filter.current.value) {
return; return;
} }
self.filterTemplateData[filter.name] = filter.current.value; self._filterTemplateData[filter.name] = filter.current.value;
}); });
}; };
this.filterOptionSelected = function(filter, option) { this.filterOptionSelected = function(filter, option) {
filter.current = option; filter.current = option;
this.updateTemplateData(); this._updateTemplateData();
dashboard.refresh(); dashboard.refresh();
}; };
...@@ -70,7 +69,7 @@ define([ ...@@ -70,7 +69,7 @@ define([
return target; return target;
} }
return _.template(target, self.filterTemplateData, self.templateSettings); return _.template(target, self._filterTemplateData, self.templateSettings);
}; };
this.remove = function(filter) { this.remove = function(filter) {
......
...@@ -14,8 +14,7 @@ module.exports = function(config) { ...@@ -14,8 +14,7 @@ module.exports = function(config) {
], ],
// list of files to exclude // list of files to exclude
exclude: [ exclude: [],
],
reporters: ['progress'], reporters: ['progress'],
port: 9876, 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({ ...@@ -3,6 +3,7 @@ require.config({
paths: { paths: {
specs: '../test/specs', specs: '../test/specs',
mocks: '../test/mocks',
config: '../config.sample', config: '../config.sample',
kbn: 'components/kbn', kbn: 'components/kbn',
...@@ -102,10 +103,20 @@ require.config({ ...@@ -102,10 +103,20 @@ require.config({
}); });
require([ require([
'specs/lexer-specs', 'angular',
'specs/parser-specs', 'angularMocks',
'specs/gfunc-specs', ], function(angular) {
'specs/ctrl-specs',
], function () { angular.module('kibana', []);
window.__karma__.start(); angular.module('kibana.services', []);
});
\ No newline at end of file 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