singlestat-specs.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common';
  3. import angular from 'angular';
  4. import helpers from '../../../../../test/specs/helpers';
  5. import {SingleStatCtrl} from '../module';
  6. describe('SingleStatCtrl', function() {
  7. var ctx = new helpers.ControllerTestContext();
  8. function singleStatScenario(desc, func) {
  9. describe(desc, function() {
  10. ctx.setup = function (setupFunc) {
  11. beforeEach(angularMocks.module('grafana.services'));
  12. beforeEach(angularMocks.module('grafana.controllers'));
  13. beforeEach(ctx.providePhase());
  14. beforeEach(ctx.createPanelController(SingleStatCtrl));
  15. beforeEach(function() {
  16. setupFunc();
  17. var data = [
  18. {target: 'test.cpu1', datapoints: ctx.datapoints}
  19. ];
  20. ctx.ctrl.onDataReceived(data);
  21. ctx.data = ctx.ctrl.data;
  22. });
  23. };
  24. func(ctx);
  25. });
  26. }
  27. singleStatScenario('with defaults', function(ctx) {
  28. ctx.setup(function() {
  29. ctx.datapoints = [[10,1], [20,2]];
  30. });
  31. it('Should use series avg as default main value', function() {
  32. expect(ctx.data.value).to.be(15);
  33. expect(ctx.data.valueRounded).to.be(15);
  34. });
  35. it('should set formated falue', function() {
  36. expect(ctx.data.valueFormated).to.be('15');
  37. });
  38. });
  39. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) {
  40. ctx.setup(function() {
  41. ctx.datapoints = [[99.999,1], [99.99999,2]];
  42. });
  43. it('Should be rounded', function() {
  44. expect(ctx.data.value).to.be(99.999495);
  45. expect(ctx.data.valueRounded).to.be(100);
  46. });
  47. it('should set formated falue', function() {
  48. expect(ctx.data.valueFormated).to.be('100');
  49. });
  50. });
  51. singleStatScenario('When value to text mapping is specified', function(ctx) {
  52. ctx.setup(function() {
  53. ctx.datapoints = [[9.9,1]];
  54. ctx.ctrl.panel.valueMaps = [{value: '10', text: 'OK'}];
  55. });
  56. it('value should remain', function() {
  57. expect(ctx.data.value).to.be(9.9);
  58. });
  59. it('round should be rounded up', function() {
  60. expect(ctx.data.valueRounded).to.be(10);
  61. });
  62. it('Should replace value with text', function() {
  63. expect(ctx.data.valueFormated).to.be('OK');
  64. });
  65. });
  66. singleStatScenario('When range to text mapping is specifiedfor first range', function(ctx) {
  67. ctx.setup(function() {
  68. ctx.datapoints = [[41,50]];
  69. ctx.ctrl.panel.mappingType = 2;
  70. ctx.ctrl.panel.rangeMaps = [{from: '10', to: '50', text: 'OK'},{from: '51', to: '100', text: 'NOT OK'}];
  71. });
  72. it('Should replace value with text OK', function() {
  73. expect(ctx.data.valueFormated).to.be('OK');
  74. });
  75. });
  76. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  77. ctx.setup(function() {
  78. ctx.datapoints = [[65,75]];
  79. ctx.ctrl.panel.mappingType = 2;
  80. ctx.ctrl.panel.rangeMaps = [{from: '10', to: '50', text: 'OK'},{from: '51', to: '100', text: 'NOT OK'}];
  81. });
  82. it('Should replace value with text NOT OK', function() {
  83. expect(ctx.data.valueFormated).to.be('NOT OK');
  84. });
  85. });
  86. });