From abc8077a962cc1bb0c26b43b4b5b3423d17a0405 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 7 Aug 2014 18:17:26 +0200 Subject: [PATCH] added some unit tests for graph panel controller --- src/app/panels/graph/module.html | 2 +- src/app/panels/graph/module.js | 11 +++-- src/test/specs/graph-ctrl-specs.js | 44 +++++++++++++++++++ .../specs/graph-panel-controller-specs.js | 31 ------------- src/test/specs/helpers.js | 19 +++++++- src/test/test-main.js | 1 + 6 files changed, 70 insertions(+), 38 deletions(-) create mode 100644 src/test/specs/graph-ctrl-specs.js delete mode 100644 src/test/specs/graph-panel-controller-specs.js diff --git a/src/app/panels/graph/module.html b/src/app/panels/graph/module.html index a61a632dd8e..8e27adf5c31 100644 --- a/src/app/panels/graph/module.html +++ b/src/app/panels/graph/module.html @@ -1,4 +1,4 @@ -
diff --git a/src/app/panels/graph/module.js b/src/app/panels/graph/module.js index 8f8ad44c3dc..913c9f052bf 100644 --- a/src/app/panels/graph/module.js +++ b/src/app/panels/graph/module.js @@ -19,6 +19,7 @@ define([ 'kbn', 'moment', './timeSeries', + 'services/panelSrv', 'services/annotationsSrv', 'services/datasourceSrv', 'jquery.flot', @@ -35,7 +36,7 @@ function (angular, app, $, _, kbn, moment, timeSeries) { var module = angular.module('grafana.panels.graph', []); app.useModule(module); - module.controller('graph', function($scope, $rootScope, $timeout, panelSrv, annotationsSrv) { + module.controller('GraphCtrl', function($scope, $rootScope, $timeout, panelSrv, annotationsSrv) { $scope.panelMeta = { modals : [], @@ -190,12 +191,14 @@ function (angular, app, $, _, kbn, moment, timeSeries) { $scope.init = function() { panelSrv.init($scope); $scope.hiddenSeries = {}; - $scope.get_data(); + if (!$scope.skipDataOnInit) { + $scope.get_data(); + } }; $scope.updateTimeRange = function () { - $scope.range = this.filter.timeRange(); - $scope.rangeUnparsed = this.filter.timeRange(false); + $scope.range = $scope.filter.timeRange(); + $scope.rangeUnparsed = $scope.filter.timeRange(false); $scope.resolution = Math.ceil($(window).width() * ($scope.panel.span / 12)); $scope.interval = '10m'; diff --git a/src/test/specs/graph-ctrl-specs.js b/src/test/specs/graph-ctrl-specs.js new file mode 100644 index 00000000000..c01353feb62 --- /dev/null +++ b/src/test/specs/graph-ctrl-specs.js @@ -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); + }); + + }); + }); + +}); + diff --git a/src/test/specs/graph-panel-controller-specs.js b/src/test/specs/graph-panel-controller-specs.js deleted file mode 100644 index 72c2934c4e5..00000000000 --- a/src/test/specs/graph-panel-controller-specs.js +++ /dev/null @@ -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() { - - }); - }); - }); - -}); -*/ \ No newline at end of file diff --git a/src/test/specs/helpers.js b/src/test/specs/helpers.js index 5c5fc3edfc3..189cfb692d0 100644 --- a/src/test/specs/helpers.js +++ b/src/test/specs/helpers.js @@ -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, { diff --git a/src/test/test-main.js b/src/test/test-main.js index b0ea2988bcd..dcfb7ac5b19 100644 --- a/src/test/test-main.js +++ b/src/test/test-main.js @@ -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',