From c3a6ae1622cb42cc4f7f0adc6f87962ec9ff747b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 7 Aug 2014 13:44:09 +0200 Subject: [PATCH] added more graphite target controller tests --- package.json | 66 ++++++++++---------- src/app/controllers/graphiteTarget.js | 2 +- src/test/.jshintrc | 3 +- src/test/karma.conf.js | 2 +- src/test/specs/graphiteTargetCtrl-specs.js | 70 +++++++++++++++++++--- 5 files changed, 99 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 0725dca19da..c554d26dbfe 100644 --- a/package.json +++ b/package.json @@ -10,44 +10,44 @@ "url": "http://github.com/torkelo/grafana.git" }, "devDependencies": { - "rjs-build-analysis": "0.0.3", - "grunt": "~0.4.0", - "grunt-ngmin": "0.0.3", - "grunt-contrib-less": "~0.7.0", - "grunt-contrib-copy": "~0.5.0", - "grunt-git-describe": "~2.3.2", - "grunt-contrib-clean": "~0.5.0", - "grunt-contrib-cssmin": "~0.6.1", - "grunt-contrib-jshint": "~0.10.0", - "grunt-string-replace": "~0.2.4", - "grunt-contrib-htmlmin": "~0.1.3", - "grunt-contrib-requirejs": "~0.4.1", - "grunt-angular-templates": "^0.5.5", - "grunt-contrib-compress": "~0.5.2", - "grunt-contrib-uglify": "~0.2.4", - "load-grunt-tasks": "~0.2.0", - "glob": "~3.2.7", - "grunt-contrib-connect": "~0.5.0", - "mocha": "~1.16.1", "expect.js": "~0.2.0", - "karma-script-launcher": "~0.1.0", - "karma-firefox-launcher": "~0.1.3", + "glob": "~3.2.7", + "grunt": "~0.4.0", + "grunt-angular-templates": "^0.5.5", + "grunt-cli": "~0.1.13", + "grunt-contrib-clean": "~0.5.0", + "grunt-contrib-compress": "~0.5.2", + "grunt-contrib-concat": "^0.4.0", + "grunt-contrib-connect": "~0.5.0", + "grunt-contrib-copy": "~0.5.0", + "grunt-contrib-cssmin": "~0.6.1", + "grunt-contrib-htmlmin": "~0.1.3", + "grunt-contrib-jshint": "~0.10.0", + "grunt-contrib-less": "~0.7.0", + "grunt-contrib-requirejs": "~0.4.1", + "grunt-contrib-uglify": "~0.2.4", + "grunt-filerev": "^0.2.1", + "grunt-git-describe": "~2.3.2", + "grunt-karma": "~0.8.3", + "grunt-ngmin": "0.0.3", + "grunt-string-replace": "~0.2.4", + "grunt-usemin": "^2.1.1", + "jshint-stylish": "~0.1.5", + "karma": "~0.12.16", "karma-chrome-launcher": "~0.1.4", + "karma-coffee-preprocessor": "~0.1.2", + "karma-expect": "~1.1.0", + "karma-firefox-launcher": "~0.1.3", "karma-html2js-preprocessor": "~0.1.0", "karma-jasmine": "~0.2.2", - "requirejs": "~2.1.14", - "karma-requirejs": "~0.2.1", - "karma-coffee-preprocessor": "~0.1.2", - "karma-phantomjs-launcher": "~0.1.1", - "karma": "~0.12.16", - "grunt-karma": "~0.8.3", "karma-mocha": "~0.1.4", - "karma-expect": "~1.1.0", - "grunt-cli": "~0.1.13", - "jshint-stylish": "~0.1.5", - "grunt-contrib-concat": "^0.4.0", - "grunt-usemin": "^2.1.1", - "grunt-filerev": "^0.2.1" + "karma-phantomjs-launcher": "~0.1.1", + "karma-requirejs": "~0.2.1", + "karma-script-launcher": "~0.1.0", + "load-grunt-tasks": "~0.2.0", + "mocha": "~1.16.1", + "requirejs": "~2.1.14", + "rjs-build-analysis": "0.0.3" }, "engines": { "node": "0.10.x", diff --git a/src/app/controllers/graphiteTarget.js b/src/app/controllers/graphiteTarget.js index 71edbdfaed7..c783fef1d14 100644 --- a/src/app/controllers/graphiteTarget.js +++ b/src/app/controllers/graphiteTarget.js @@ -249,7 +249,7 @@ function (angular, _, config, gfunc, Parser) { return; } for(var i = 0; i < $scope.segments.length; i++) { - if ($scope.segments[i].val.indexOf('*') >= 0) { + if ($scope.segments[i].value.indexOf('*') >= 0) { func.params[0] = i; func.added = false; $scope.targetChanged(); diff --git a/src/test/.jshintrc b/src/test/.jshintrc index 24155c73881..4da11286ca8 100644 --- a/src/test/.jshintrc +++ b/src/test/.jshintrc @@ -28,6 +28,7 @@ "expect": true, "it": true, "describe": true, + "sinon": true, "define": true, "module": true, "beforeEach": true, @@ -35,4 +36,4 @@ "require": true, "setImmediate": true } -} \ No newline at end of file +} diff --git a/src/test/karma.conf.js b/src/test/karma.conf.js index ff247fe391a..2a3868c2011 100644 --- a/src/test/karma.conf.js +++ b/src/test/karma.conf.js @@ -25,6 +25,6 @@ module.exports = function(config) { autoWatch: true, browsers: ['Chrome'], captureTimeout: 60000, - singleRun: false + singleRun: false, }); }; diff --git a/src/test/specs/graphiteTargetCtrl-specs.js b/src/test/specs/graphiteTargetCtrl-specs.js index 93a083af8e7..dfe51960fc5 100644 --- a/src/test/specs/graphiteTargetCtrl-specs.js +++ b/src/test/specs/graphiteTargetCtrl-specs.js @@ -1,7 +1,8 @@ define([ './helpers', + 'services/graphite/gfunc', 'controllers/graphiteTarget' -], function(helpers) { +], function(helpers, gfunc) { 'use strict'; describe('GraphiteTargetCtrl', function() { @@ -11,25 +12,78 @@ define([ beforeEach(ctx.providePhase()); beforeEach(ctx.createControllerPhase('GraphiteTargetCtrl')); + beforeEach(function() { + ctx.scope.target = { + target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' + }; + + ctx.scope.datasource = ctx.datasource; + ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); + }); + describe('init', function() { beforeEach(function() { - ctx.scope.target = { - target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)' - }; - - ctx.scope.datasource = ctx.datasource; - ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([])); ctx.scope.init(); + ctx.scope.$digest(); }); it('should validate metric key exists', function() { expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[1]).to.be('test.prod.*'); }); + it('should delete last segment if no metrics are found', function() { + expect(ctx.scope.segments[2].value).to.be('select metric'); + }); + it('should parse expression and build function model', function() { expect(ctx.scope.functions.length).to.be(2); }); - }); + + describe('when adding function', function() { + beforeEach(function() { + ctx.scope.target.target = 'test.prod.*.count'; + ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}])); + ctx.scope.init(); + ctx.scope.$digest(); + + ctx.scope.$parent = { get_data: sinon.spy() }; + ctx.scope.addFunction(gfunc.getFuncDef('aliasByNode')); + }); + + it('should add function with correct node number', function() { + expect(ctx.scope.functions[0].params[0]).to.be(2); + }); + + it('should update target', function() { + expect(ctx.scope.target.target).to.be('aliasByNode(test.prod.*.count,2)'); + }); + + it('should call get_data', function() { + expect(ctx.scope.$parent.get_data.called).to.be(true); + }); + }); + + describe('targetChanged', function() { + beforeEach(function() { + ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}])); + ctx.scope.init(); + ctx.scope.$digest(); + + ctx.scope.$parent = { get_data: sinon.spy() }; + ctx.scope.target.target = ''; + ctx.scope.targetChanged(); + }); + + it('should rebuld target after expression model', function() { + expect(ctx.scope.target.target).to.be('aliasByNode(scaleToSeconds(test.prod.*,1),2)'); + }); + + it('should call get_data', function() { + expect(ctx.scope.$parent.get_data.called).to.be(true); + }); + }); + + }); });