From 88e91b3f51fa2c5a66442bfa3322abbfbeebd950 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Thu, 26 Jul 2018 10:44:40 +0200 Subject: [PATCH 1/9] Begin conversion --- .../panel/singlestat/specs/singlestat.jest.ts | 384 ++++++++++++++++++ 1 file changed, 384 insertions(+) create mode 100644 public/app/plugins/panel/singlestat/specs/singlestat.jest.ts diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts new file mode 100644 index 00000000000..2c945aa6eb2 --- /dev/null +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -0,0 +1,384 @@ +// import { describe, beforeEach, afterEach, it, sinon, expect, angularMocks } from 'test/lib/common'; + +// import helpers from 'test/specs/helpers'; +import { SingleStatCtrl } from '../module'; +import moment from 'moment'; + +describe('SingleStatCtrl', function() { + let ctx = {}; + let epoch = 1505826363746; + let clock; + + let $scope = { + $on: () => {}, + }; + + let $injector = { + get: () => {}, + }; + + SingleStatCtrl.prototype.panel = { + events: { + on: () => {}, + emit: () => {}, + }, + }; + SingleStatCtrl.prototype.dashboard = { + isTimezoneUtc: () => {}, + }; + + function singleStatScenario(desc, func) { + describe(desc, function() { + ctx.setup = function(setupFunc) { + // beforeEach(angularMocks.module('grafana.services')); + // beforeEach(angularMocks.module('grafana.controllers')); + // beforeEach( + // angularMocks.module(function($compileProvider) { + // $compileProvider.preAssignBindingsEnabled(true); + // }) + // ); + + // beforeEach(ctx.providePhase()); + // beforeEach(ctx.createPanelController(SingleStatCtrl)); + + beforeEach(function() { + ctx.ctrl = new SingleStatCtrl($scope, $injector, {}); + setupFunc(); + ctx.ctrl.onDataReceived(ctx.data); + ctx.data = ctx.ctrl.data; + }); + }; + + func(ctx); + }); + } + + singleStatScenario('with defaults', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }]; + }); + + it('Should use series avg as default main value', function() { + expect(ctx.data.value).toBe(15); + expect(ctx.data.valueRounded).toBe(15); + }); + + it('should set formatted falue', function() { + expect(ctx.data.valueFormatted).toBe('15'); + }); + }); + + singleStatScenario('showing serie name instead of value', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }]; + ctx.ctrl.panel.valueName = 'name'; + }); + + it('Should use series avg as default main value', function() { + expect(ctx.data.value).toBe(0); + expect(ctx.data.valueRounded).toBe(0); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe('test.cpu1'); + }); + }); + + singleStatScenario('showing last iso time instead of value', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeAsIso'; + }); + + it('Should use time instead of value', function() { + console.log(ctx.data.value); + expect(ctx.data.value).toBe(1505634997920); + expect(ctx.data.valueRounded).toBe(1505634997920); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss')); + }); + }); + + singleStatScenario('showing last iso time instead of value (in UTC)', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeAsIso'; + // ctx.setIsUtc(true); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe(moment.utc(1505634997920).format('YYYY-MM-DD HH:mm:ss')); + }); + }); + + singleStatScenario('showing last us time instead of value', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeAsUS'; + }); + + it('Should use time instead of value', function() { + expect(ctx.data.value).toBe(1505634997920); + expect(ctx.data.valueRounded).toBe(1505634997920); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe(moment(1505634997920).format('MM/DD/YYYY h:mm:ss a')); + }); + }); + + singleStatScenario('showing last us time instead of value (in UTC)', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeAsUS'; + // ctx.setIsUtc(true); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe(moment.utc(1505634997920).format('MM/DD/YYYY h:mm:ss a')); + }); + }); + + singleStatScenario('showing last time from now instead of value', function(ctx) { + beforeEach(() => { + // clock = sinon.useFakeTimers(epoch); + jest.useFakeTimers(); + }); + + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeFromNow'; + }); + + it('Should use time instead of value', function() { + expect(ctx.data.value).toBe(1505634997920); + expect(ctx.data.valueRounded).toBe(1505634997920); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe('2 days ago'); + }); + + afterEach(() => { + jest.clearAllTimers(); + }); + }); + + singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) { + beforeEach(() => { + // clock = sinon.useFakeTimers(epoch); + jest.useFakeTimers(); + }); + + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.ctrl.panel.valueName = 'last_time'; + ctx.ctrl.panel.format = 'dateTimeFromNow'; + // ctx.setIsUtc(true); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe('2 days ago'); + }); + + afterEach(() => { + jest.clearAllTimers(); + }); + }); + + singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function( + ctx + ) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }]; + }); + + it('Should be rounded', function() { + expect(ctx.data.value).toBe(99.999495); + expect(ctx.data.valueRounded).toBe(100); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe('100'); + }); + }); + + singleStatScenario('When value to text mapping is specified', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }]; + ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }]; + }); + + it('value should remain', function() { + expect(ctx.data.value).toBe(9.9); + }); + + it('round should be rounded up', function() { + expect(ctx.data.valueRounded).toBe(10); + }); + + it('Should replace value with text', function() { + expect(ctx.data.valueFormatted).toBe('OK'); + }); + }); + + singleStatScenario('When range to text mapping is specified for first range', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }]; + ctx.ctrl.panel.mappingType = 2; + ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; + }); + + it('Should replace value with text OK', function() { + expect(ctx.data.valueFormatted).toBe('OK'); + }); + }); + + singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) { + ctx.setup(function() { + ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }]; + ctx.ctrl.panel.mappingType = 2; + ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; + }); + + it('Should replace value with text NOT OK', function() { + expect(ctx.data.valueFormatted).toBe('NOT OK'); + }); + }); + + describe('When table data', function() { + const tableData = [ + { + columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }], + rows: [[1492759673649, 'ignore1', 15, 'ignore2']], + type: 'table', + }, + ]; + + singleStatScenario('with default values', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.ctrl.panel.tableColumn = 'mean'; + }); + + it('Should use first rows value as default main value', function() { + expect(ctx.data.value).toBe(15); + expect(ctx.data.valueRounded).toBe(15); + }); + + it('should set formatted value', function() { + expect(ctx.data.valueFormatted).toBe('15'); + }); + }); + + singleStatScenario('When table data has multiple columns', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.ctrl.panel.tableColumn = ''; + }); + + it('Should set column to first column that is not time', function() { + expect(ctx.ctrl.panel.tableColumn).toBe('test1'); + }); + }); + + singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function( + ctx + ) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'mean'; + }); + + it('Should be rounded', function() { + expect(ctx.data.value).toBe(99.99999); + expect(ctx.data.valueRounded).toBe(100); + }); + + it('should set formatted falue', function() { + expect(ctx.data.valueFormatted).toBe('100'); + }); + }); + + singleStatScenario('When value to text mapping is specified', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'mean'; + ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }]; + }); + + it('value should remain', function() { + expect(ctx.data.value).toBe(9.9); + }); + + it('round should be rounded up', function() { + expect(ctx.data.valueRounded).toBe(10); + }); + + it('Should replace value with text', function() { + expect(ctx.data.valueFormatted).toBe('OK'); + }); + }); + + singleStatScenario('When range to text mapping is specified for first range', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'mean'; + ctx.ctrl.panel.mappingType = 2; + ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; + }); + + it('Should replace value with text OK', function() { + expect(ctx.data.valueFormatted).toBe('OK'); + }); + }); + + singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'mean'; + ctx.ctrl.panel.mappingType = 2; + ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; + }); + + it('Should replace value with text NOT OK', function() { + expect(ctx.data.valueFormatted).toBe('NOT OK'); + }); + }); + + singleStatScenario('When value is string', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'test1'; + }); + + it('Should replace value with text NOT OK', function() { + expect(ctx.data.valueFormatted).toBe('ignore1'); + }); + }); + + singleStatScenario('When value is zero', function(ctx) { + ctx.setup(function() { + ctx.data = tableData; + ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2']; + ctx.ctrl.panel.tableColumn = 'mean'; + }); + + it('Should return zero', function() { + expect(ctx.data.value).toBe(0); + }); + }); + }); +}); From fc06f8bfe71d758148708dee23c52af678935a52 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Thu, 26 Jul 2018 17:22:15 +0200 Subject: [PATCH 2/9] Pass more tests --- public/app/plugins/panel/singlestat/module.ts | 1 + .../panel/singlestat/specs/singlestat.jest.ts | 34 ++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index ebd2628b086..7fafb5902d1 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -310,6 +310,7 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.valueRounded = data.value; data.valueFormatted = formatFunc(data.value, this.dashboard.isTimezoneUtc()); } else { + console.log(lastPoint, lastValue); data.value = this.series[0].stats[this.panel.valueName]; data.flotpairs = this.series[0].flotpairs; diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index 2c945aa6eb2..7b89f86250c 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -7,7 +7,7 @@ import moment from 'moment'; describe('SingleStatCtrl', function() { let ctx = {}; let epoch = 1505826363746; - let clock; + Date.now = () => epoch; let $scope = { $on: () => {}, @@ -24,7 +24,7 @@ describe('SingleStatCtrl', function() { }, }; SingleStatCtrl.prototype.dashboard = { - isTimezoneUtc: () => {}, + isTimezoneUtc: jest.fn(() => true), }; function singleStatScenario(desc, func) { @@ -89,29 +89,30 @@ describe('SingleStatCtrl', function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsIso'; + ctx.ctrl.dashboard.isTimezoneUtc = () => false; }); it('Should use time instead of value', function() { - console.log(ctx.data.value); expect(ctx.data.value).toBe(1505634997920); expect(ctx.data.valueRounded).toBe(1505634997920); }); it('should set formatted value', function() { - expect(ctx.data.valueFormatted).toBe(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss')); + expect(ctx.data.valueFormatted).toBe('2017-09-17 09:56:37'); }); }); singleStatScenario('showing last iso time instead of value (in UTC)', function(ctx) { ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsIso'; // ctx.setIsUtc(true); + ctx.ctrl.dashboard.isTimezoneUtc = () => true; }); - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).toBe(moment.utc(1505634997920).format('YYYY-MM-DD HH:mm:ss')); + it('should set value', function() { + expect(ctx.data.valueFormatted).toBe('1970-01-01 00:00:05'); }); }); @@ -120,6 +121,7 @@ describe('SingleStatCtrl', function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsUS'; + ctx.ctrl.dashboard.isTimezoneUtc = () => false; }); it('Should use time instead of value', function() { @@ -134,21 +136,22 @@ describe('SingleStatCtrl', function() { singleStatScenario('showing last us time instead of value (in UTC)', function(ctx) { ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; + ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsUS'; // ctx.setIsUtc(true); + ctx.ctrl.dashboard.isTimezoneUtc = () => true; }); it('should set formatted value', function() { - expect(ctx.data.valueFormatted).toBe(moment.utc(1505634997920).format('MM/DD/YYYY h:mm:ss a')); + expect(ctx.data.valueFormatted).toBe('01/01/1970 12:00:05 am'); }); }); singleStatScenario('showing last time from now instead of value', function(ctx) { beforeEach(() => { // clock = sinon.useFakeTimers(epoch); - jest.useFakeTimers(); + //jest.useFakeTimers(); }); ctx.setup(function() { @@ -167,16 +170,11 @@ describe('SingleStatCtrl', function() { }); afterEach(() => { - jest.clearAllTimers(); + // jest.clearAllTimers(); }); }); singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) { - beforeEach(() => { - // clock = sinon.useFakeTimers(epoch); - jest.useFakeTimers(); - }); - ctx.setup(function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; ctx.ctrl.panel.valueName = 'last_time'; @@ -187,10 +185,6 @@ describe('SingleStatCtrl', function() { it('should set formatted value', function() { expect(ctx.data.valueFormatted).toBe('2 days ago'); }); - - afterEach(() => { - jest.clearAllTimers(); - }); }); singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function( From 675a031b6c9c367fe27de5e839c1d919ca09021d Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 11:04:01 +0200 Subject: [PATCH 3/9] All except one passing --- public/app/plugins/panel/singlestat/module.ts | 5 ++++- public/app/plugins/panel/singlestat/specs/singlestat.jest.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index 7fafb5902d1..b63182141c1 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -310,11 +310,14 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.valueRounded = data.value; data.valueFormatted = formatFunc(data.value, this.dashboard.isTimezoneUtc()); } else { - console.log(lastPoint, lastValue); + // console.log(lastPoint, lastValue); + // console.log(this.panel.valueName); + // console.log(this.panel); data.value = this.series[0].stats[this.panel.valueName]; data.flotpairs = this.series[0].flotpairs; let decimalInfo = this.getDecimalsForValue(data.value); + console.log(decimalInfo); let formatFunc = kbn.valueFormats[this.panel.format]; data.valueFormatted = formatFunc(data.value, decimalInfo.decimals, decimalInfo.scaledDecimals); data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals); diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index 7b89f86250c..798298415a9 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -192,6 +192,8 @@ describe('SingleStatCtrl', function() { ) { ctx.setup(function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[99.999, 1], [99.99999, 2]] }]; + ctx.ctrl.panel.valueName = 'avg'; + ctx.ctrl.panel.format = 'none'; }); it('Should be rounded', function() { @@ -259,7 +261,9 @@ describe('SingleStatCtrl', function() { singleStatScenario('with default values', function(ctx) { ctx.setup(function() { ctx.data = tableData; + ctx.ctrl.panel = {}; ctx.ctrl.panel.tableColumn = 'mean'; + ctx.ctrl.panel.format = 'none'; }); it('Should use first rows value as default main value', function() { From 47da3e3ae83f36207cedfa26e9b5d51ca21b112f Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 11:28:16 +0200 Subject: [PATCH 4/9] All tests passing --- public/app/plugins/panel/singlestat/module.ts | 4 ---- public/app/plugins/panel/singlestat/specs/singlestat.jest.ts | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index b63182141c1..ebd2628b086 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -310,14 +310,10 @@ class SingleStatCtrl extends MetricsPanelCtrl { data.valueRounded = data.value; data.valueFormatted = formatFunc(data.value, this.dashboard.isTimezoneUtc()); } else { - // console.log(lastPoint, lastValue); - // console.log(this.panel.valueName); - // console.log(this.panel); data.value = this.series[0].stats[this.panel.valueName]; data.flotpairs = this.series[0].flotpairs; let decimalInfo = this.getDecimalsForValue(data.value); - console.log(decimalInfo); let formatFunc = kbn.valueFormats[this.panel.format]; data.valueFormatted = formatFunc(data.value, decimalInfo.decimals, decimalInfo.scaledDecimals); data.valueRounded = kbn.roundValue(data.value, decimalInfo.decimals); diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index 798298415a9..552ac2412d6 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -293,6 +293,7 @@ describe('SingleStatCtrl', function() { ctx.setup(function() { ctx.data = tableData; ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2']; + ctx.ctrl.panel.mappingType = 0; ctx.ctrl.panel.tableColumn = 'mean'; }); @@ -310,6 +311,7 @@ describe('SingleStatCtrl', function() { ctx.setup(function() { ctx.data = tableData; ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2']; + ctx.ctrl.panel.mappingType = 2; ctx.ctrl.panel.tableColumn = 'mean'; ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }]; }); From 3d21e42aac715c28fe3325bd3ce9f7a00cb39312 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 11:30:37 +0200 Subject: [PATCH 5/9] Remove Karma file --- .../singlestat/specs/singlestat_specs.ts | 362 ------------------ 1 file changed, 362 deletions(-) delete mode 100644 public/app/plugins/panel/singlestat/specs/singlestat_specs.ts diff --git a/public/app/plugins/panel/singlestat/specs/singlestat_specs.ts b/public/app/plugins/panel/singlestat/specs/singlestat_specs.ts deleted file mode 100644 index 217ec5ee04c..00000000000 --- a/public/app/plugins/panel/singlestat/specs/singlestat_specs.ts +++ /dev/null @@ -1,362 +0,0 @@ -import { describe, beforeEach, afterEach, it, sinon, expect, angularMocks } from 'test/lib/common'; - -import helpers from 'test/specs/helpers'; -import { SingleStatCtrl } from '../module'; -import moment from 'moment'; - -describe('SingleStatCtrl', function() { - var ctx = new helpers.ControllerTestContext(); - var epoch = 1505826363746; - var clock; - - function singleStatScenario(desc, func) { - describe(desc, function() { - ctx.setup = function(setupFunc) { - beforeEach(angularMocks.module('grafana.services')); - beforeEach(angularMocks.module('grafana.controllers')); - beforeEach( - angularMocks.module(function($compileProvider) { - $compileProvider.preAssignBindingsEnabled(true); - }) - ); - - beforeEach(ctx.providePhase()); - beforeEach(ctx.createPanelController(SingleStatCtrl)); - - beforeEach(function() { - setupFunc(); - ctx.ctrl.onDataReceived(ctx.data); - ctx.data = ctx.ctrl.data; - }); - }; - - func(ctx); - }); - } - - singleStatScenario('with defaults', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', 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 formatted falue', function() { - expect(ctx.data.valueFormatted).to.be('15'); - }); - }); - - singleStatScenario('showing serie name instead of value', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 1], [20, 2]] }]; - ctx.ctrl.panel.valueName = 'name'; - }); - - it('Should use series avg as default main value', function() { - expect(ctx.data.value).to.be(0); - expect(ctx.data.valueRounded).to.be(0); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be('test.cpu1'); - }); - }); - - singleStatScenario('showing last iso time instead of value', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeAsIso'; - }); - - it('Should use time instead of value', function() { - expect(ctx.data.value).to.be(1505634997920); - expect(ctx.data.valueRounded).to.be(1505634997920); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be(moment(1505634997920).format('YYYY-MM-DD HH:mm:ss')); - }); - }); - - singleStatScenario('showing last iso time instead of value (in UTC)', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeAsIso'; - ctx.setIsUtc(true); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be(moment.utc(1505634997920).format('YYYY-MM-DD HH:mm:ss')); - }); - }); - - singleStatScenario('showing last us time instead of value', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeAsUS'; - }); - - it('Should use time instead of value', function() { - expect(ctx.data.value).to.be(1505634997920); - expect(ctx.data.valueRounded).to.be(1505634997920); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be(moment(1505634997920).format('MM/DD/YYYY h:mm:ss a')); - }); - }); - - singleStatScenario('showing last us time instead of value (in UTC)', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeAsUS'; - ctx.setIsUtc(true); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be(moment.utc(1505634997920).format('MM/DD/YYYY h:mm:ss a')); - }); - }); - - singleStatScenario('showing last time from now instead of value', function(ctx) { - beforeEach(() => { - clock = sinon.useFakeTimers(epoch); - }); - - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeFromNow'; - }); - - it('Should use time instead of value', function() { - expect(ctx.data.value).to.be(1505634997920); - expect(ctx.data.valueRounded).to.be(1505634997920); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be('2 days ago'); - }); - - afterEach(() => { - clock.restore(); - }); - }); - - singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) { - beforeEach(() => { - clock = sinon.useFakeTimers(epoch); - }); - - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; - ctx.ctrl.panel.valueName = 'last_time'; - ctx.ctrl.panel.format = 'dateTimeFromNow'; - ctx.setIsUtc(true); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be('2 days ago'); - }); - - afterEach(() => { - clock.restore(); - }); - }); - - singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function( - ctx - ) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', 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 formatted value', function() { - expect(ctx.data.valueFormatted).to.be('100'); - }); - }); - - singleStatScenario('When value to text mapping is specified', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[9.9, 1]] }]; - ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }]; - }); - - it('value should remain', function() { - expect(ctx.data.value).to.be(9.9); - }); - - it('round should be rounded up', function() { - expect(ctx.data.valueRounded).to.be(10); - }); - - it('Should replace value with text', function() { - expect(ctx.data.valueFormatted).to.be('OK'); - }); - }); - - singleStatScenario('When range to text mapping is specified for first range', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[41, 50]] }]; - ctx.ctrl.panel.mappingType = 2; - ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; - }); - - it('Should replace value with text OK', function() { - expect(ctx.data.valueFormatted).to.be('OK'); - }); - }); - - singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) { - ctx.setup(function() { - ctx.data = [{ target: 'test.cpu1', datapoints: [[65, 75]] }]; - ctx.ctrl.panel.mappingType = 2; - ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; - }); - - it('Should replace value with text NOT OK', function() { - expect(ctx.data.valueFormatted).to.be('NOT OK'); - }); - }); - - describe('When table data', function() { - const tableData = [ - { - columns: [{ text: 'Time', type: 'time' }, { text: 'test1' }, { text: 'mean' }, { text: 'test2' }], - rows: [[1492759673649, 'ignore1', 15, 'ignore2']], - type: 'table', - }, - ]; - - singleStatScenario('with default values', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.ctrl.panel.tableColumn = 'mean'; - }); - - it('Should use first rows value as default main value', function() { - expect(ctx.data.value).to.be(15); - expect(ctx.data.valueRounded).to.be(15); - }); - - it('should set formatted value', function() { - expect(ctx.data.valueFormatted).to.be('15'); - }); - }); - - singleStatScenario('When table data has multiple columns', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.ctrl.panel.tableColumn = ''; - }); - - it('Should set column to first column that is not time', function() { - expect(ctx.ctrl.panel.tableColumn).to.be('test1'); - }); - }); - - singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function( - ctx - ) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 99.99999, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'mean'; - }); - - it('Should be rounded', function() { - expect(ctx.data.value).to.be(99.99999); - expect(ctx.data.valueRounded).to.be(100); - }); - - it('should set formatted falue', function() { - expect(ctx.data.valueFormatted).to.be('100'); - }); - }); - - singleStatScenario('When value to text mapping is specified', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 9.9, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'mean'; - ctx.ctrl.panel.valueMaps = [{ value: '10', text: 'OK' }]; - }); - - it('value should remain', function() { - expect(ctx.data.value).to.be(9.9); - }); - - it('round should be rounded up', function() { - expect(ctx.data.valueRounded).to.be(10); - }); - - it('Should replace value with text', function() { - expect(ctx.data.valueFormatted).to.be('OK'); - }); - }); - - singleStatScenario('When range to text mapping is specified for first range', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 41, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'mean'; - ctx.ctrl.panel.mappingType = 2; - ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; - }); - - it('Should replace value with text OK', function() { - expect(ctx.data.valueFormatted).to.be('OK'); - }); - }); - - singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'mean'; - ctx.ctrl.panel.mappingType = 2; - ctx.ctrl.panel.rangeMaps = [{ from: '10', to: '50', text: 'OK' }, { from: '51', to: '100', text: 'NOT OK' }]; - }); - - it('Should replace value with text NOT OK', function() { - expect(ctx.data.valueFormatted).to.be('NOT OK'); - }); - }); - - singleStatScenario('When value is string', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 65, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'test1'; - }); - - it('Should replace value with text NOT OK', function() { - expect(ctx.data.valueFormatted).to.be('ignore1'); - }); - }); - - singleStatScenario('When value is zero', function(ctx) { - ctx.setup(function() { - ctx.data = tableData; - ctx.data[0].rows[0] = [1492759673649, 'ignore1', 0, 'ignore2']; - ctx.ctrl.panel.tableColumn = 'mean'; - }); - - it('Should return zero', function() { - expect(ctx.data.value).to.be(0); - }); - }); - }); -}); From bff7a293562125dc8423919f23a871d7141fa189 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 11:34:14 +0200 Subject: [PATCH 6/9] Cleanup --- .../panel/singlestat/specs/singlestat.jest.ts | 26 ------------------- 1 file changed, 26 deletions(-) diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index 552ac2412d6..7e8915ca537 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -1,6 +1,3 @@ -// import { describe, beforeEach, afterEach, it, sinon, expect, angularMocks } from 'test/lib/common'; - -// import helpers from 'test/specs/helpers'; import { SingleStatCtrl } from '../module'; import moment from 'moment'; @@ -30,17 +27,6 @@ describe('SingleStatCtrl', function() { function singleStatScenario(desc, func) { describe(desc, function() { ctx.setup = function(setupFunc) { - // beforeEach(angularMocks.module('grafana.services')); - // beforeEach(angularMocks.module('grafana.controllers')); - // beforeEach( - // angularMocks.module(function($compileProvider) { - // $compileProvider.preAssignBindingsEnabled(true); - // }) - // ); - - // beforeEach(ctx.providePhase()); - // beforeEach(ctx.createPanelController(SingleStatCtrl)); - beforeEach(function() { ctx.ctrl = new SingleStatCtrl($scope, $injector, {}); setupFunc(); @@ -107,7 +93,6 @@ describe('SingleStatCtrl', function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsIso'; - // ctx.setIsUtc(true); ctx.ctrl.dashboard.isTimezoneUtc = () => true; }); @@ -139,7 +124,6 @@ describe('SingleStatCtrl', function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 5000]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeAsUS'; - // ctx.setIsUtc(true); ctx.ctrl.dashboard.isTimezoneUtc = () => true; }); @@ -149,11 +133,6 @@ describe('SingleStatCtrl', function() { }); singleStatScenario('showing last time from now instead of value', function(ctx) { - beforeEach(() => { - // clock = sinon.useFakeTimers(epoch); - //jest.useFakeTimers(); - }); - ctx.setup(function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; ctx.ctrl.panel.valueName = 'last_time'; @@ -168,10 +147,6 @@ describe('SingleStatCtrl', function() { it('should set formatted value', function() { expect(ctx.data.valueFormatted).toBe('2 days ago'); }); - - afterEach(() => { - // jest.clearAllTimers(); - }); }); singleStatScenario('showing last time from now instead of value (in UTC)', function(ctx) { @@ -179,7 +154,6 @@ describe('SingleStatCtrl', function() { ctx.data = [{ target: 'test.cpu1', datapoints: [[10, 12], [20, 1505634997920]] }]; ctx.ctrl.panel.valueName = 'last_time'; ctx.ctrl.panel.format = 'dateTimeFromNow'; - // ctx.setIsUtc(true); }); it('should set formatted value', function() { From 55111c801fbdc74687d74136dc73daf2aa29131c Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 13:41:07 +0200 Subject: [PATCH 7/9] Update test for local time --- .../plugins/panel/singlestat/specs/singlestat.jest.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index 7e8915ca537..dd02b5c169c 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -23,6 +23,9 @@ describe('SingleStatCtrl', function() { SingleStatCtrl.prototype.dashboard = { isTimezoneUtc: jest.fn(() => true), }; + SingleStatCtrl.prototype.events = { + on: () => {}, + }; function singleStatScenario(desc, func) { describe(desc, function() { @@ -84,7 +87,7 @@ describe('SingleStatCtrl', function() { }); it('should set formatted value', function() { - expect(ctx.data.valueFormatted).toBe('2017-09-17 09:56:37'); + expect(moment(ctx.data.valueFormatted).isSame('2017-09-17 09:56:37')).toBe(true); }); }); @@ -235,7 +238,9 @@ describe('SingleStatCtrl', function() { singleStatScenario('with default values', function(ctx) { ctx.setup(function() { ctx.data = tableData; - ctx.ctrl.panel = {}; + ctx.ctrl.panel = { + emit: () => {}, + }; ctx.ctrl.panel.tableColumn = 'mean'; ctx.ctrl.panel.format = 'none'; }); From 2db4a54f75c7c1bd8a3a70ea0d4be50f88ab0552 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 14:40:56 +0200 Subject: [PATCH 8/9] Fix test --- public/app/plugins/panel/singlestat/specs/singlestat.jest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts index dd02b5c169c..0480d0be5c3 100644 --- a/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts +++ b/public/app/plugins/panel/singlestat/specs/singlestat.jest.ts @@ -87,7 +87,7 @@ describe('SingleStatCtrl', function() { }); it('should set formatted value', function() { - expect(moment(ctx.data.valueFormatted).isSame('2017-09-17 09:56:37')).toBe(true); + expect(moment(ctx.data.valueFormatted).valueOf()).toBe(1505634997000); }); }); From 766c23a1eb86d6ba47b2d61d9b72153089b73264 Mon Sep 17 00:00:00 2001 From: Tobias Skarhed Date: Fri, 27 Jul 2018 15:16:19 +0200 Subject: [PATCH 9/9] Fix emit errors --- public/app/plugins/panel/graph/specs/graph_ctrl.jest.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/app/plugins/panel/graph/specs/graph_ctrl.jest.ts b/public/app/plugins/panel/graph/specs/graph_ctrl.jest.ts index 3ebcf6cdf31..a0c7dd0ab9c 100644 --- a/public/app/plugins/panel/graph/specs/graph_ctrl.jest.ts +++ b/public/app/plugins/panel/graph/specs/graph_ctrl.jest.ts @@ -34,6 +34,9 @@ describe('GraphCtrl', () => { beforeEach(() => { ctx.ctrl = new GraphCtrl(scope, injector, {}); + ctx.ctrl.events = { + emit: () => {}, + }; ctx.ctrl.annotationsPromise = Promise.resolve({}); ctx.ctrl.updateTimeRange(); });