diff --git a/package.json b/package.json index 5434fdef8b7..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", @@ -58,6 +58,8 @@ }, "license": "Apache License", "dependencies": { - "grunt-jscs-checker": "^0.4.4" + "grunt-jscs-checker": "^0.4.4", + "karma-sinon": "^1.0.3", + "sinon": "^1.10.3" } } 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 042a056f97b..2a3868c2011 100644 --- a/src/test/karma.conf.js +++ b/src/test/karma.conf.js @@ -4,7 +4,7 @@ module.exports = function(config) { config.set({ basePath: '../', - frameworks: ['mocha', 'requirejs', 'expect'], + frameworks: ['mocha', 'requirejs', 'expect', 'sinon'], // list of files / patterns to load in the browser files: [ @@ -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 c4f2502e62d..dfe51960fc5 100644 --- a/src/test/specs/graphiteTargetCtrl-specs.js +++ b/src/test/specs/graphiteTargetCtrl-specs.js @@ -1,24 +1,89 @@ define([ -], function() { + './helpers', + 'services/graphite/gfunc', + 'controllers/graphiteTarget' +], function(helpers, gfunc) { 'use strict'; - describe('graphiteTargetCtrl', function() { - var _targetCtrl; + describe('GraphiteTargetCtrl', function() { + var ctx = new helpers.ControllerTestContext(); - beforeEach(module('grafana.services')); - beforeEach(module(function($provide){ - $provide.value('filterSrv',{}); - })); + beforeEach(module('grafana.controllers')); + beforeEach(ctx.providePhase()); + beforeEach(ctx.createControllerPhase('GraphiteTargetCtrl')); - beforeEach(inject(function($controller, $rootScope) { - _targetCtrl = $controller({ - $scope: $rootScope.$new() - }); - })); + 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.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); + }); + }); + + }); }); diff --git a/src/test/specs/helpers.js b/src/test/specs/helpers.js new file mode 100644 index 00000000000..5c5fc3edfc3 --- /dev/null +++ b/src/test/specs/helpers.js @@ -0,0 +1,43 @@ +define([ +], function() { + 'use strict'; + + function ControllerTestContext() { + var self = this; + + this.datasource = {}; + this.datasourceSrv = { + getMetricSources: function() {}, + get: function() { return self.datasource; } + }; + + this.providePhase = function() { + return module(function($provide) { + $provide.value('datasourceSrv', self.datasourceSrv); + }); + }; + + this.createControllerPhase = function(controllerName) { + return inject(function($controller, $rootScope, $q) { + self.scope = $rootScope.$new(); + self.scope.panel = {}; + self.scope.filter = { + timeRange: function() {} + }; + + self.$q = $q; + self.scope.skipDataOnInit = true; + self.controller = $controller(controllerName, { + $scope: self.scope + }); + + }); + }; + } + + + return { + ControllerTestContext: ControllerTestContext + }; + +}); diff --git a/src/test/test-main.js b/src/test/test-main.js index 46d69def575..0c5e01b5d4e 100644 --- a/src/test/test-main.js +++ b/src/test/test-main.js @@ -121,6 +121,7 @@ require([ 'specs/lexer-specs', 'specs/parser-specs', 'specs/gfunc-specs', + 'specs/graphiteTargetCtrl-specs', 'specs/filterSrv-specs', 'specs/kbn-format-specs', 'specs/dashboardModel-specs',