singlestat-specs.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(angularMocks.module(function($compileProvider) {
  14. $compileProvider.preAssignBindingsEnabled(true);
  15. }));
  16. beforeEach(ctx.providePhase());
  17. beforeEach(ctx.createPanelController(SingleStatCtrl));
  18. beforeEach(function() {
  19. setupFunc();
  20. var data = [
  21. {target: 'test.cpu1', datapoints: ctx.datapoints}
  22. ];
  23. ctx.ctrl.onDataReceived(data);
  24. ctx.data = ctx.ctrl.data;
  25. });
  26. };
  27. func(ctx);
  28. });
  29. }
  30. singleStatScenario('with defaults', function(ctx) {
  31. ctx.setup(function() {
  32. ctx.datapoints = [[10,1], [20,2]];
  33. });
  34. it('Should use series avg as default main value', function() {
  35. expect(ctx.data.value).to.be(15);
  36. expect(ctx.data.valueRounded).to.be(15);
  37. });
  38. it('should set formated falue', function() {
  39. expect(ctx.data.valueFormated).to.be('15');
  40. });
  41. });
  42. singleStatScenario('showing serie name instead of value', function(ctx) {
  43. ctx.setup(function() {
  44. ctx.datapoints = [[10,1], [20,2]];
  45. ctx.ctrl.panel.valueName = 'name';
  46. });
  47. it('Should use series avg as default main value', function() {
  48. expect(ctx.data.value).to.be(0);
  49. expect(ctx.data.valueRounded).to.be(0);
  50. });
  51. it('should set formated falue', function() {
  52. expect(ctx.data.valueFormated).to.be('test.cpu1');
  53. });
  54. });
  55. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) {
  56. ctx.setup(function() {
  57. ctx.datapoints = [[99.999,1], [99.99999,2]];
  58. });
  59. it('Should be rounded', function() {
  60. expect(ctx.data.value).to.be(99.999495);
  61. expect(ctx.data.valueRounded).to.be(100);
  62. });
  63. it('should set formated falue', function() {
  64. expect(ctx.data.valueFormated).to.be('100');
  65. });
  66. });
  67. singleStatScenario('When value to text mapping is specified', function(ctx) {
  68. ctx.setup(function() {
  69. ctx.datapoints = [[9.9,1]];
  70. ctx.ctrl.panel.valueMaps = [{value: '10', text: 'OK'}];
  71. });
  72. it('value should remain', function() {
  73. expect(ctx.data.value).to.be(9.9);
  74. });
  75. it('round should be rounded up', function() {
  76. expect(ctx.data.valueRounded).to.be(10);
  77. });
  78. it('Should replace value with text', function() {
  79. expect(ctx.data.valueFormated).to.be('OK');
  80. });
  81. });
  82. singleStatScenario('When range to text mapping is specifiedfor first range', function(ctx) {
  83. ctx.setup(function() {
  84. ctx.datapoints = [[41,50]];
  85. ctx.ctrl.panel.mappingType = 2;
  86. ctx.ctrl.panel.rangeMaps = [{from: '10', to: '50', text: 'OK'},{from: '51', to: '100', text: 'NOT OK'}];
  87. });
  88. it('Should replace value with text OK', function() {
  89. expect(ctx.data.valueFormated).to.be('OK');
  90. });
  91. });
  92. singleStatScenario('When range to text mapping is specified for other ranges', function(ctx) {
  93. ctx.setup(function() {
  94. ctx.datapoints = [[65,75]];
  95. ctx.ctrl.panel.mappingType = 2;
  96. ctx.ctrl.panel.rangeMaps = [{from: '10', to: '50', text: 'OK'},{from: '51', to: '100', text: 'NOT OK'}];
  97. });
  98. it('Should replace value with text NOT OK', function() {
  99. expect(ctx.data.valueFormated).to.be('NOT OK');
  100. });
  101. });
  102. });