kbn-format-specs.js 3.8 KB

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