From 76aab2a2ac6239e178b1c8cdcff2e073377bc173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 7 Aug 2014 10:42:05 +0200 Subject: [PATCH] added graphiteTargetCtrl specs --- package.json | 4 +- src/test/karma.conf.js | 2 +- src/test/specs/graphiteTargetCtrl-specs.js | 37 ++++++++++++------- src/test/specs/helpers.js | 43 ++++++++++++++++++++++ src/test/test-main.js | 1 + 5 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 src/test/specs/helpers.js diff --git a/package.json b/package.json index 5434fdef8b7..0725dca19da 100644 --- a/package.json +++ b/package.json @@ -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/test/karma.conf.js b/src/test/karma.conf.js index 042a056f97b..ff247fe391a 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: [ diff --git a/src/test/specs/graphiteTargetCtrl-specs.js b/src/test/specs/graphiteTargetCtrl-specs.js index c4f2502e62d..93a083af8e7 100644 --- a/src/test/specs/graphiteTargetCtrl-specs.js +++ b/src/test/specs/graphiteTargetCtrl-specs.js @@ -1,24 +1,35 @@ define([ -], function() { + './helpers', + 'controllers/graphiteTarget' +], function(helpers) { '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(inject(function($controller, $rootScope) { - _targetCtrl = $controller({ - $scope: $rootScope.$new() - }); - })); + beforeEach(module('grafana.controllers')); + beforeEach(ctx.providePhase()); + beforeEach(ctx.createControllerPhase('GraphiteTargetCtrl')); 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(); }); + + it('should validate metric key exists', function() { + expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[1]).to.be('test.prod.*'); + }); + + it('should parse expression and build function model', function() { + expect(ctx.scope.functions.length).to.be(2); + }); + }); }); }); 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 cf955bc1852..a82c3d180fd 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',