kbn-format-specs.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. define([
  2. 'kbn',
  3. 'app/core/utils/datemath'
  4. ], function(kbn, dateMath) {
  5. 'use strict';
  6. function describeValueFormat(desc, value, tickSize, tickDecimals, result) {
  7. describe('value format: ' + desc, function() {
  8. it('should translate ' + value + ' as ' + result, function() {
  9. var scaledDecimals = tickDecimals - Math.floor(Math.log(tickSize) / Math.LN10);
  10. var str = kbn.valueFormats[desc](value, tickDecimals, scaledDecimals);
  11. expect(str).to.be(result);
  12. });
  13. });
  14. }
  15. describeValueFormat('ms', 0.0024, 0.0005, 4, '0.0024 ms');
  16. describeValueFormat('ms', 100, 1, 0, '100 ms');
  17. describeValueFormat('ms', 1250, 10, 0, '1.25 s');
  18. describeValueFormat('ms', 1250, 300, 0, '1.3 s');
  19. describeValueFormat('ms', 65150, 10000, 0, '1.1 min');
  20. describeValueFormat('ms', 6515000, 1500000, 0, '1.8 hour');
  21. describeValueFormat('ms', 651500000, 150000000, 0, '8 day');
  22. describeValueFormat('none', 2.75e-10, 0, 10, '3e-10');
  23. describeValueFormat('none', 0, 0, 2, '0');
  24. describeValueFormat('bytes', -1.57e+308, -1.57e+308, 2, 'NA');
  25. describeValueFormat('ns', 25, 1, 0, '25 ns');
  26. describeValueFormat('ns', 2558, 50, 0, '2.56 µs');
  27. describe('kbn.toFixed and negative decimals', function() {
  28. it('should treat as zero decimals', function() {
  29. var str = kbn.toFixed(186.123, -2);
  30. expect(str).to.be('186');
  31. });
  32. });
  33. describe('kbn ms format when scaled decimals is null do not use it', function() {
  34. it('should use specified decimals', function() {
  35. var str = kbn.valueFormats['ms'](10000086.123, 1, null);
  36. expect(str).to.be('2.8 hour');
  37. });
  38. });
  39. describe('kbn kbytes format when scaled decimals is null do not use it', function() {
  40. it('should use specified decimals', function() {
  41. var str = kbn.valueFormats['kbytes'](10000000, 3, null);
  42. expect(str).to.be('9.537 GiB');
  43. });
  44. });
  45. describe('kbn roundValue', function() {
  46. it('should should handle null value', function() {
  47. var str = kbn.roundValue(null, 2);
  48. expect(str).to.be(null);
  49. });
  50. });
  51. describe('calculateInterval', function() {
  52. it('1h 100 resultion', function() {
  53. var range = { from: dateMath.parse('now-1h'), to: dateMath.parse('now') };
  54. var str = kbn.calculateInterval(range, 100, null);
  55. expect(str).to.be('30s');
  56. });
  57. it('10m 1600 resolution', function() {
  58. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  59. var str = kbn.calculateInterval(range, 1600, null);
  60. expect(str).to.be('100ms');
  61. });
  62. it('fixed user interval', function() {
  63. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  64. var str = kbn.calculateInterval(range, 1600, '10s');
  65. expect(str).to.be('10s');
  66. });
  67. it('short time range and user low limit', function() {
  68. var range = { from: dateMath.parse('now-10m'), to: dateMath.parse('now') };
  69. var str = kbn.calculateInterval(range, 1600, '>10s');
  70. expect(str).to.be('10s');
  71. });
  72. it('large time range and user low limit', function() {
  73. var range = { from: dateMath.parse('now-14d'), to: dateMath.parse('now') };
  74. var str = kbn.calculateInterval(range, 1000, '>10s');
  75. expect(str).to.be('30m');
  76. });
  77. });
  78. });