From b0a24ae4aadc3067b55f1f8cb140f125c77652cb Mon Sep 17 00:00:00 2001 From: bergquist <carl.bergquist@gmail.com> Date: Mon, 25 Jan 2016 14:48:23 +0100 Subject: [PATCH] tech(singlestat): move singlestat test to plugin and change to typescript --- .../singlestat/specs/singlestat-specs.ts | 88 +++++++++++++++++++ .../singlestat/specs/singlestat_panel_spec.ts | 58 ++++++++++++ public/test/specs/singlestat-specs.js | 88 ------------------- 3 files changed, 146 insertions(+), 88 deletions(-) create mode 100644 public/app/plugins/panel/singlestat/specs/singlestat-specs.ts delete mode 100644 public/test/specs/singlestat-specs.js diff --git a/public/app/plugins/panel/singlestat/specs/singlestat-specs.ts b/public/app/plugins/panel/singlestat/specs/singlestat-specs.ts new file mode 100644 index 00000000000..8d532d17d80 --- /dev/null +++ b/public/app/plugins/panel/singlestat/specs/singlestat-specs.ts @@ -0,0 +1,88 @@ +///<reference path="../../../../headers/common.d.ts" /> + +import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common'; + +import 'app/features/panel/panel_srv'; +import 'app/features/panel/panel_helper'; + +import angular from 'angular'; +import helpers from '../../../../../test/specs/helpers'; +import {SingleStatCtrl} from '../controller'; + + +angular.module('grafana.controllers').controller('SingleStatCtrl', SingleStatCtrl); + +describe('SingleStatCtrl', function() { + var ctx = new helpers.ControllerTestContext(); + + function singleStatScenario(desc, func) { + + describe(desc, function() { + + ctx.setup = function (setupFunc) { + + beforeEach(angularMocks.module('grafana.services')); + beforeEach(angularMocks.module('grafana.controllers')); + + beforeEach(ctx.providePhase()); + beforeEach(ctx.createControllerPhase('SingleStatCtrl')); + + beforeEach(function() { + setupFunc(); + ctx.datasource.query = sinon.stub().returns(ctx.$q.when({ + data: [{target: 'test.cpu1', datapoints: ctx.datapoints}] + })); + + ctx.scope.refreshData(ctx.datasource); + ctx.scope.$digest(); + ctx.data = ctx.scope.data; + }); + }; + + func(ctx); + }); + } + + singleStatScenario('with defaults', function(ctx) { + ctx.setup(function() { + ctx.datapoints = [[10,1], [20,2]]; + }); + + it('Should use series avg as default main value', function() { + expect(ctx.data.value).to.be(15); + expect(ctx.data.valueRounded).to.be(15); + }); + + it('should set formated falue', function() { + expect(ctx.data.valueFormated).to.be('15'); + }); + }); + + singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) { + ctx.setup(function() { + ctx.datapoints = [[99.999,1], [99.99999,2]]; + }); + + it('Should be rounded', function() { + expect(ctx.data.value).to.be(99.999495); + expect(ctx.data.valueRounded).to.be(100); + }); + + it('should set formated falue', function() { + expect(ctx.data.valueFormated).to.be('100'); + }); + }); + + singleStatScenario('When value to text mapping is specified', function(ctx) { + ctx.setup(function() { + ctx.datapoints = [[10,1]]; + ctx.scope.panel.valueMaps = [{value: '10', text: 'OK'}]; + }); + + it('Should replace value with text', function() { + expect(ctx.data.value).to.be(10); + expect(ctx.data.valueFormated).to.be('OK'); + }); + + }); +}); diff --git a/public/app/plugins/panel/singlestat/specs/singlestat_panel_spec.ts b/public/app/plugins/panel/singlestat/specs/singlestat_panel_spec.ts index e69de29bb2d..17b34f6bcef 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat_panel_spec.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat_panel_spec.ts @@ -0,0 +1,58 @@ +import {describe, beforeEach, it, sinon, expect} from 'test/lib/common'; + +import {getColorForValue} from '../module'; + +describe('grafanaSingleStat', function() { + describe('legacy thresholds', () => { + describe('positive thresholds', () => { + var data: any = { + colorMap: ['green', 'yellow', 'red'], + thresholds: [0, 20, 50] + }; + + it('5 should return green', () => { + expect(getColorForValue(data, 5)).to.be('green'); + }); + + it('25 should return green', () => { + expect(getColorForValue(data, 25)).to.be('yellow'); + }); + + it('55 should return green', () => { + expect(getColorForValue(data, 55)).to.be('red'); + }); + }); + }); + + + + describe('negative thresholds', () => { + var data: any = { + colorMap: ['green', 'yellow', 'red'], + thresholds: [ -20, 0, 20] + }; + + it('-30 should return green', () => { + expect(getColorForValue(data, -30)).to.be('green'); + }); + + it('1 should return green', () => { + expect(getColorForValue(data, 1)).to.be('yellow'); + }); + + it('22 should return green', () => { + expect(getColorForValue(data, 22)).to.be('red'); + }); + }); + + describe('negative thresholds', () => { + var data: any = { + colorMap: ['green', 'yellow', 'red'], + thresholds: [ -40, -27, 20] + }; + + it('-30 should return green', () => { + expect(getColorForValue(data, -26)).to.be('yellow'); + }); + }); +}); diff --git a/public/test/specs/singlestat-specs.js b/public/test/specs/singlestat-specs.js deleted file mode 100644 index 60953345f2d..00000000000 --- a/public/test/specs/singlestat-specs.js +++ /dev/null @@ -1,88 +0,0 @@ -define([ - 'angular', - './helpers', - 'app/plugins/panel/singlestat/controller', - 'app/features/panel/panel_srv', - 'app/features/panel/panel_helper', -], function(angular, helpers, SingleStatCtrl) { - 'use strict'; - - angular.module('grafana.controllers').controller('SingleStatCtrl', SingleStatCtrl); - - describe('SingleStatCtrl', function() { - var ctx = new helpers.ControllerTestContext(); - - function singleStatScenario(desc, func) { - - describe(desc, function() { - - ctx.setup = function (setupFunc) { - - beforeEach(module('grafana.services')); - beforeEach(module('grafana.controllers')); - - beforeEach(ctx.providePhase()); - beforeEach(ctx.createControllerPhase('SingleStatCtrl')); - - beforeEach(function() { - setupFunc(); - ctx.datasource.query = sinon.stub().returns(ctx.$q.when({ - data: [{target: 'test.cpu1', datapoints: ctx.datapoints}] - })); - - ctx.scope.refreshData(ctx.datasource); - ctx.scope.$digest(); - ctx.data = ctx.scope.data; - }); - }; - - func(ctx); - }); - } - - singleStatScenario('with defaults', function(ctx) { - ctx.setup(function() { - ctx.datapoints = [[10,1], [20,2]]; - }); - - it('Should use series avg as default main value', function() { - expect(ctx.data.value).to.be(15); - expect(ctx.data.valueRounded).to.be(15); - }); - - it('should set formated falue', function() { - expect(ctx.data.valueFormated).to.be('15'); - }); - }); - - singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) { - ctx.setup(function() { - ctx.datapoints = [[99.999,1], [99.99999,2]]; - }); - - it('Should be rounded', function() { - expect(ctx.data.value).to.be(99.999495); - expect(ctx.data.valueRounded).to.be(100); - }); - - it('should set formated falue', function() { - expect(ctx.data.valueFormated).to.be('100'); - }); - }); - - singleStatScenario('When value to text mapping is specified', function(ctx) { - ctx.setup(function() { - ctx.datapoints = [[10,1]]; - ctx.scope.panel.valueMaps = [{value: '10', text: 'OK'}]; - }); - - it('Should replace value with text', function() { - expect(ctx.data.value).to.be(10); - expect(ctx.data.valueFormated).to.be('OK'); - }); - - }); - - }); -}); -