singlestat-specs.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ///<reference path="../../../../headers/common.d.ts" />
  2. import {describe, beforeEach, it, sinon, expect, angularMocks} from '../../../../../test/lib/common';
  3. import 'app/features/panel/panel_srv';
  4. import 'app/features/panel/panel_helper';
  5. import angular from 'angular';
  6. import helpers from '../../../../../test/specs/helpers';
  7. import {SingleStatCtrl} from '../controller';
  8. angular.module('grafana.controllers').controller('SingleStatCtrl', SingleStatCtrl);
  9. describe('SingleStatCtrl', function() {
  10. var ctx = new helpers.ControllerTestContext();
  11. function singleStatScenario(desc, func) {
  12. describe(desc, function() {
  13. ctx.setup = function (setupFunc) {
  14. beforeEach(angularMocks.module('grafana.services'));
  15. beforeEach(angularMocks.module('grafana.controllers'));
  16. beforeEach(ctx.providePhase());
  17. beforeEach(ctx.createControllerPhase('SingleStatCtrl'));
  18. beforeEach(function() {
  19. setupFunc();
  20. ctx.datasource.query = sinon.stub().returns(ctx.$q.when({
  21. data: [{target: 'test.cpu1', datapoints: ctx.datapoints}]
  22. }));
  23. ctx.scope.refreshData(ctx.datasource);
  24. ctx.scope.$digest();
  25. ctx.data = ctx.scope.data;
  26. });
  27. };
  28. func(ctx);
  29. });
  30. }
  31. singleStatScenario('with defaults', function(ctx) {
  32. ctx.setup(function() {
  33. ctx.datapoints = [[10,1], [20,2]];
  34. });
  35. it('Should use series avg as default main value', function() {
  36. expect(ctx.data.value).to.be(15);
  37. expect(ctx.data.valueRounded).to.be(15);
  38. });
  39. it('should set formated falue', function() {
  40. expect(ctx.data.valueFormated).to.be('15');
  41. });
  42. });
  43. singleStatScenario('MainValue should use same number for decimals as displayed when checking thresholds', function(ctx) {
  44. ctx.setup(function() {
  45. ctx.datapoints = [[99.999,1], [99.99999,2]];
  46. });
  47. it('Should be rounded', function() {
  48. expect(ctx.data.value).to.be(99.999495);
  49. expect(ctx.data.valueRounded).to.be(100);
  50. });
  51. it('should set formated falue', function() {
  52. expect(ctx.data.valueFormated).to.be('100');
  53. });
  54. });
  55. singleStatScenario('When value to text mapping is specified', function(ctx) {
  56. ctx.setup(function() {
  57. ctx.datapoints = [[10,1]];
  58. ctx.scope.panel.valueMaps = [{value: '10', text: 'OK'}];
  59. });
  60. it('Should replace value with text', function() {
  61. expect(ctx.data.value).to.be(10);
  62. expect(ctx.data.valueFormated).to.be('OK');
  63. });
  64. });
  65. });