singlestat-specs.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. ctx.datasource.query = sinon.stub().returns(ctx.$q.when({
  18. data: [{target: 'test.cpu1', datapoints: ctx.datapoints}]
  19. }));
  20. ctx.ctrl.refreshData(ctx.datasource);
  21. ctx.scope.$digest();
  22. ctx.data = ctx.ctrl.data;
  23. });
  24. };
  25. func(ctx);
  26. });
  27. }
  28. singleStatScenario('with defaults', function(ctx) {
  29. ctx.setup(function() {
  30. ctx.datapoints = [[10,1], [20,2]];
  31. });
  32. it('Should use series avg as default main value', function() {
  33. expect(ctx.data.value).to.be(15);
  34. expect(ctx.data.valueRounded).to.be(15);
  35. });
  36. it('should set formated falue', function() {
  37. expect(ctx.data.valueFormated).to.be('15');
  38. });
  39. });
  40. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) {
  41. ctx.setup(function() {
  42. ctx.datapoints = [[99.999,1], [99.99999,2]];
  43. });
  44. it('Should be rounded', function() {
  45. expect(ctx.data.value).to.be(99.999495);
  46. expect(ctx.data.valueRounded).to.be(100);
  47. });
  48. it('should set formated falue', function() {
  49. expect(ctx.data.valueFormated).to.be('100');
  50. });
  51. });
  52. singleStatScenario('When value to text mapping is specified', function(ctx) {
  53. ctx.setup(function() {
  54. ctx.datapoints = [[10,1]];
  55. ctx.ctrl.panel.valueMaps = [{value: '10', text: 'OK'}];
  56. });
  57. it('Should replace value with text', function() {
  58. expect(ctx.data.value).to.be(10);
  59. expect(ctx.data.valueFormated).to.be('OK');
  60. });
  61. });
  62. });