singlestat-specs.js 2.4 KB

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