added some unit tests for graph panel controller

This commit is contained in:
Torkel Ödegaard
2014-08-07 18:17:26 +02:00
parent 02fb2baf62
commit abc8077a96
6 changed files with 70 additions and 38 deletions

View File

@@ -0,0 +1,44 @@
define([
'./helpers',
'panels/graph/module'
], function(helpers) {
'use strict';
describe('GraphCtrl', function() {
var ctx = new helpers.ControllerTestContext();
beforeEach(module('grafana.services'));
beforeEach(module('grafana.panels.graph'));
beforeEach(ctx.providePhase());
beforeEach(ctx.createControllerPhase('GraphCtrl'));
describe('get_data with 2 series', function() {
beforeEach(function() {
ctx.annotationsSrv.getAnnotations = sinon.stub().returns(ctx.$q.when([]));
ctx.datasource.query = sinon.stub().returns(ctx.$q.when({
data: [
{ target: 'test.cpu1', datapoints: [[1, 10]]},
{ target: 'test.cpu2', datapoints: [[1, 10]]}
]
}));
ctx.scope.render = sinon.spy();
ctx.scope.get_data();
ctx.scope.$digest();
});
it('should build legend model', function() {
expect(ctx.scope.legend[0].alias).to.be('test.cpu1');
expect(ctx.scope.legend[1].alias).to.be('test.cpu2');
});
it('should send time series to render', function() {
var data = ctx.scope.render.getCall(0).args[0];
expect(data.length).to.be(2);
});
});
});
});

View File

@@ -1,31 +0,0 @@
/*define([
'panels/graphite/module'
], function() {
'use strict';
describe('Graph panel controller', function() {
var _graphPanelCtrl;
beforeEach(module('grafana.panels.graphite'));
beforeEach(module(function($provide){
$provide.value('filterSrv',{});
}));
beforeEach(inject(function($controller, $rootScope) {
_graphPanelCtrl = $controller('graphite', {
$scope: $rootScope.$new()
});
}));
describe('init', function() {
beforeEach(function() {
});
it('asd', function() {
});
});
});
});
*/

View File

@@ -1,11 +1,14 @@
define([
], function() {
'kbn'
], function(kbn) {
'use strict';
function ControllerTestContext() {
var self = this;
this.timeRange = { from:'now-1h', to: 'now'};
this.datasource = {};
this.annotationsSrv = {};
this.datasourceSrv = {
getMetricSources: function() {},
get: function() { return self.datasource; }
@@ -14,6 +17,7 @@ define([
this.providePhase = function() {
return module(function($provide) {
$provide.value('datasourceSrv', self.datasourceSrv);
$provide.value('annotationsSrv', self.annotationsSrv);
});
};
@@ -22,9 +26,20 @@ define([
self.scope = $rootScope.$new();
self.scope.panel = {};
self.scope.filter = {
timeRange: function() {}
timeRange: function(parse) {
if (!parse) {
return self.timeRange;
}
return {
from : kbn.parseDate(self.timeRange.from),
to : kbn.parseDate(self.timeRange.to)
};
}
};
self.scope.colors = [];
for (var i = 0; i < 50; i++) { self.scope.colors.push('#' + i); }
self.$q = $q;
self.scope.skipDataOnInit = true;
self.controller = $controller(controllerName, {

View File

@@ -118,6 +118,7 @@ require([
'specs/parser-specs',
'specs/gfunc-specs',
'specs/graphiteTargetCtrl-specs',
'specs/graph-ctrl-specs',
'specs/filterSrv-specs',
'specs/kbn-format-specs',
'specs/dashboardModel-specs',