mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
added unit tests for filterSrv
This commit is contained in:
parent
5ac6d28abf
commit
dfd1d09641
@ -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([
|
|||||||
};
|
};
|
||||||
|
|
||||||
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([
|
|||||||
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([
|
|||||||
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) {
|
|||||||
],
|
],
|
||||||
|
|
||||||
// list of files to exclude
|
// list of files to exclude
|
||||||
exclude: [
|
exclude: [],
|
||||||
],
|
|
||||||
|
|
||||||
reporters: ['progress'],
|
reporters: ['progress'],
|
||||||
port: 9876,
|
port: 9876,
|
||||||
|
40
src/test/mocks/dashboard-mock.js
Normal file
40
src/test/mocks/dashboard-mock.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
@ -1,26 +0,0 @@
|
|||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
});*/
|
|
||||||
|
|
||||||
});
|
|
60
src/test/specs/filterSrv-specs.js
Normal file
60
src/test/specs/filterSrv-specs.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
60
src/test/specs/graphiteTargetCtrl-specs.js
Normal file
60
src/test/specs/graphiteTargetCtrl-specs.js
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
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: {
|
paths: {
|
||||||
specs: '../test/specs',
|
specs: '../test/specs',
|
||||||
|
mocks: '../test/mocks',
|
||||||
config: '../config.sample',
|
config: '../config.sample',
|
||||||
kbn: 'components/kbn',
|
kbn: 'components/kbn',
|
||||||
|
|
||||||
@ -101,11 +102,21 @@ require.config({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
require([
|
||||||
|
'angular',
|
||||||
|
'angularMocks',
|
||||||
|
], function(angular) {
|
||||||
|
|
||||||
|
angular.module('kibana', []);
|
||||||
|
angular.module('kibana.services', []);
|
||||||
|
|
||||||
require([
|
require([
|
||||||
'specs/lexer-specs',
|
'specs/lexer-specs',
|
||||||
'specs/parser-specs',
|
'specs/parser-specs',
|
||||||
'specs/gfunc-specs',
|
'specs/gfunc-specs',
|
||||||
'specs/ctrl-specs',
|
'specs/filterSrv-specs',
|
||||||
], function () {
|
], function () {
|
||||||
window.__karma__.start();
|
window.__karma__.start();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user