Просмотр исходного кода

added graphiteTargetCtrl specs

Torkel Ödegaard 11 лет назад
Родитель
Сommit
76aab2a2ac

+ 3 - 1
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"
   }
 }

+ 1 - 1
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: [

+ 24 - 13
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);
       });
+
     });
   });
 });

+ 43 - 0
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
+  };
+
+});

+ 1 - 0
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',