formatters.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {
  2. formatAbbreviatedNumber,
  3. formatFloat,
  4. formatPercentage,
  5. getDuration,
  6. } from 'sentry/utils/formatters';
  7. describe('getDuration()', function () {
  8. it('should format durations', function () {
  9. expect(getDuration(0, 2)).toBe('0.00ms');
  10. expect(getDuration(0.1)).toBe('100ms');
  11. expect(getDuration(0.1, 2)).toBe('100.00ms');
  12. expect(getDuration(1)).toBe('1 second');
  13. expect(getDuration(2)).toBe('2 seconds');
  14. expect(getDuration(65)).toBe('1 minute');
  15. expect(getDuration(122)).toBe('2 minutes');
  16. expect(getDuration(3720)).toBe('1 hour');
  17. expect(getDuration(36000)).toBe('10 hours');
  18. expect(getDuration(86400)).toBe('1 day');
  19. expect(getDuration(86400 * 2)).toBe('2 days');
  20. expect(getDuration(604800)).toBe('1 week');
  21. expect(getDuration(604800 * 4)).toBe('4 weeks');
  22. expect(getDuration(2629800)).toBe('1 month');
  23. expect(getDuration(604800 * 12)).toBe('3 months');
  24. });
  25. it('should format negative durations', function () {
  26. expect(getDuration(-0, 2)).toBe('0.00ms');
  27. expect(getDuration(-0.1)).toBe('-100ms');
  28. expect(getDuration(-0.1, 2)).toBe('-100.00ms');
  29. expect(getDuration(-1)).toBe('-1 second');
  30. expect(getDuration(-2)).toBe('-2 seconds');
  31. expect(getDuration(-65)).toBe('-1 minute');
  32. expect(getDuration(-122)).toBe('-2 minutes');
  33. expect(getDuration(-3720)).toBe('-1 hour');
  34. expect(getDuration(-36000)).toBe('-10 hours');
  35. expect(getDuration(-86400)).toBe('-1 day');
  36. expect(getDuration(-86400 * 2)).toBe('-2 days');
  37. expect(getDuration(-604800)).toBe('-1 week');
  38. expect(getDuration(-604800 * 4)).toBe('-4 weeks');
  39. expect(getDuration(-2629800)).toBe('-1 month');
  40. expect(getDuration(-604800 * 12)).toBe('-3 months');
  41. });
  42. it('should format numbers and abbreviate units', function () {
  43. expect(getDuration(0, 2, true)).toBe('0.00ms');
  44. expect(getDuration(0, 0, true)).toBe('0ms');
  45. expect(getDuration(0.1, 0, true)).toBe('100ms');
  46. expect(getDuration(0.1, 2, true)).toBe('100.00ms');
  47. expect(getDuration(1, 2, true)).toBe('1.00s');
  48. expect(getDuration(122, 0, true)).toBe('2min');
  49. expect(getDuration(3600, 0, true)).toBe('1hr');
  50. expect(getDuration(86400, 0, true)).toBe('1d');
  51. expect(getDuration(86400 * 2, 0, true)).toBe('2d');
  52. expect(getDuration(604800, 0, true)).toBe('1wk');
  53. expect(getDuration(604800 * 2, 0, true)).toBe('2wk');
  54. expect(getDuration(2629800, 0, true)).toBe('1mo');
  55. expect(getDuration(604800 * 12, 0, true)).toBe('3mo');
  56. });
  57. it('should format numbers and abbreviate units with one letter', function () {
  58. expect(getDuration(0, 2, false, true)).toBe('0.00ms');
  59. expect(getDuration(0, 0, false, true)).toBe('0ms');
  60. expect(getDuration(0.1, 0, false, true)).toBe('100ms');
  61. expect(getDuration(0.1, 2, false, true)).toBe('100.00ms');
  62. expect(getDuration(1, 2, false, true)).toBe('1.00s');
  63. expect(getDuration(122, 0, false, true)).toBe('2m');
  64. expect(getDuration(3600, 0, false, true)).toBe('1h');
  65. expect(getDuration(86400, 0, false, true)).toBe('1d');
  66. expect(getDuration(86400 * 2, 0, false, true)).toBe('2d');
  67. expect(getDuration(604800, 0, false, true)).toBe('1w');
  68. expect(getDuration(604800 * 2, 0, false, true)).toBe('2w');
  69. expect(getDuration(2629800, 0, false, true)).toBe('4w');
  70. expect(getDuration(604800 * 12, 0, false, true)).toBe('12w');
  71. });
  72. });
  73. describe('formatAbbreviatedNumber()', function () {
  74. it('should abbreviate numbers', function () {
  75. expect(formatAbbreviatedNumber(0)).toBe('0');
  76. expect(formatAbbreviatedNumber(100)).toBe('100');
  77. expect(formatAbbreviatedNumber(1000)).toBe('1k');
  78. expect(formatAbbreviatedNumber(10000000)).toBe('10m');
  79. expect(formatAbbreviatedNumber(100000000000)).toBe('100b');
  80. expect(formatAbbreviatedNumber(1000000000000)).toBe('1000b');
  81. });
  82. it('should abbreviate numbers that are strings', function () {
  83. expect(formatAbbreviatedNumber('00')).toBe('0');
  84. expect(formatAbbreviatedNumber('100')).toBe('100');
  85. expect(formatAbbreviatedNumber('1000')).toBe('1k');
  86. expect(formatAbbreviatedNumber('10000000')).toBe('10m');
  87. expect(formatAbbreviatedNumber('100000000000')).toBe('100b');
  88. expect(formatAbbreviatedNumber('1000000000000')).toBe('1000b');
  89. });
  90. });
  91. describe('formatFloat()', function () {
  92. it('should format decimals', function () {
  93. expect(formatFloat(0, 0)).toBe(0);
  94. expect(formatFloat(10.513434, 1)).toBe(10.5);
  95. expect(formatFloat(10.513494, 3)).toBe(10.513);
  96. });
  97. it('should not round', function () {
  98. expect(formatFloat(10.513494, 4)).toBe(10.5134);
  99. });
  100. });
  101. describe('formatPercentage()', function () {
  102. it('should format decimals', function () {
  103. expect(formatPercentage(0.0, 0)).toBe('0%');
  104. expect(formatPercentage(0.0, 2)).toBe('0%');
  105. expect(formatPercentage(0.10513434, 1)).toBe('10.5%');
  106. expect(formatPercentage(0.10513494, 3)).toBe('10.513%');
  107. expect(formatPercentage(0.10513494, 4)).toBe('10.5135%');
  108. });
  109. });