utils.spec.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import {DATA_CATEGORY_INFO} from 'sentry/constants';
  2. import {formatUsageWithUnits} from 'sentry/views/organizationStats/utils';
  3. const MILLION = 10 ** 6;
  4. const BILLION = 10 ** 9;
  5. const GIGABYTE = 10 ** 9;
  6. describe('formatUsageWithUnits', function () {
  7. it('returns correct strings for Errors', function () {
  8. expect(formatUsageWithUnits(0, DATA_CATEGORY_INFO.error.plural)).toBe('0');
  9. expect(formatUsageWithUnits(1000, DATA_CATEGORY_INFO.error.plural)).toBe('1,000');
  10. expect(formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.error.plural)).toBe(
  11. '1,000,000'
  12. );
  13. expect(formatUsageWithUnits(BILLION, DATA_CATEGORY_INFO.error.plural)).toBe(
  14. '1,000,000,000'
  15. );
  16. expect(
  17. formatUsageWithUnits(0, DATA_CATEGORY_INFO.error.plural, {isAbbreviated: true})
  18. ).toBe('0');
  19. expect(
  20. formatUsageWithUnits(1000, DATA_CATEGORY_INFO.error.plural, {isAbbreviated: true})
  21. ).toBe('1K');
  22. expect(
  23. formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.error.plural, {
  24. isAbbreviated: true,
  25. })
  26. ).toBe('1M');
  27. expect(
  28. formatUsageWithUnits(1.234 * MILLION, DATA_CATEGORY_INFO.error.plural, {
  29. isAbbreviated: true,
  30. })
  31. ).toBe('1.2M');
  32. expect(
  33. formatUsageWithUnits(1.234 * BILLION, DATA_CATEGORY_INFO.error.plural, {
  34. isAbbreviated: true,
  35. })
  36. ).toBe('1.23B');
  37. });
  38. it('returns correct strings for Transactions', function () {
  39. expect(formatUsageWithUnits(0, DATA_CATEGORY_INFO.transaction.plural)).toBe('0');
  40. expect(formatUsageWithUnits(1000, DATA_CATEGORY_INFO.transaction.plural)).toBe(
  41. '1,000'
  42. );
  43. expect(formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.transaction.plural)).toBe(
  44. '1,000,000'
  45. );
  46. expect(formatUsageWithUnits(BILLION, DATA_CATEGORY_INFO.transaction.plural)).toBe(
  47. '1,000,000,000'
  48. );
  49. expect(
  50. formatUsageWithUnits(0, DATA_CATEGORY_INFO.transaction.plural, {
  51. isAbbreviated: true,
  52. })
  53. ).toBe('0');
  54. expect(
  55. formatUsageWithUnits(1000, DATA_CATEGORY_INFO.transaction.plural, {
  56. isAbbreviated: true,
  57. })
  58. ).toBe('1K');
  59. expect(
  60. formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.transaction.plural, {
  61. isAbbreviated: true,
  62. })
  63. ).toBe('1M');
  64. expect(
  65. formatUsageWithUnits(1.234 * MILLION, DATA_CATEGORY_INFO.transaction.plural, {
  66. isAbbreviated: true,
  67. })
  68. ).toBe('1.2M');
  69. expect(
  70. formatUsageWithUnits(1.234 * BILLION, DATA_CATEGORY_INFO.transaction.plural, {
  71. isAbbreviated: true,
  72. })
  73. ).toBe('1.23B');
  74. });
  75. it('returns correct strings for Attachments', function () {
  76. expect(formatUsageWithUnits(0, DATA_CATEGORY_INFO.attachment.plural)).toBe('0 GB');
  77. expect(formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.attachment.plural)).toBe(
  78. '0 GB'
  79. );
  80. expect(formatUsageWithUnits(BILLION, DATA_CATEGORY_INFO.attachment.plural)).toBe(
  81. '1 GB'
  82. );
  83. expect(
  84. formatUsageWithUnits(1.234 * BILLION, DATA_CATEGORY_INFO.attachment.plural)
  85. ).toBe('1.23 GB');
  86. expect(
  87. formatUsageWithUnits(1234 * GIGABYTE, DATA_CATEGORY_INFO.attachment.plural)
  88. ).toBe('1,234 GB');
  89. expect(
  90. formatUsageWithUnits(0, DATA_CATEGORY_INFO.attachment.plural, {isAbbreviated: true})
  91. ).toBe('0 GB');
  92. expect(
  93. formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.attachment.plural, {
  94. isAbbreviated: true,
  95. })
  96. ).toBe('0 GB');
  97. expect(
  98. formatUsageWithUnits(BILLION, DATA_CATEGORY_INFO.attachment.plural, {
  99. isAbbreviated: true,
  100. })
  101. ).toBe('1 GB');
  102. expect(
  103. formatUsageWithUnits(1.234 * BILLION, DATA_CATEGORY_INFO.attachment.plural, {
  104. isAbbreviated: true,
  105. })
  106. ).toBe('1 GB');
  107. expect(
  108. formatUsageWithUnits(1234 * BILLION, DATA_CATEGORY_INFO.attachment.plural, {
  109. isAbbreviated: true,
  110. })
  111. ).toBe('1K GB');
  112. expect(
  113. formatUsageWithUnits(0, DATA_CATEGORY_INFO.attachment.plural, {
  114. useUnitScaling: true,
  115. })
  116. ).toBe('0 B');
  117. expect(
  118. formatUsageWithUnits(1000, DATA_CATEGORY_INFO.attachment.plural, {
  119. useUnitScaling: true,
  120. })
  121. ).toBe('1 KB');
  122. expect(
  123. formatUsageWithUnits(MILLION, DATA_CATEGORY_INFO.attachment.plural, {
  124. useUnitScaling: true,
  125. })
  126. ).toBe('1 MB');
  127. expect(
  128. formatUsageWithUnits(1.234 * MILLION, DATA_CATEGORY_INFO.attachment.plural, {
  129. useUnitScaling: true,
  130. })
  131. ).toBe('1.23 MB');
  132. expect(
  133. formatUsageWithUnits(1.234 * BILLION, DATA_CATEGORY_INFO.attachment.plural, {
  134. useUnitScaling: true,
  135. })
  136. ).toBe('1.23 GB');
  137. expect(
  138. formatUsageWithUnits(1234 * BILLION, DATA_CATEGORY_INFO.attachment.plural, {
  139. useUnitScaling: true,
  140. })
  141. ).toBe('1.23 TB');
  142. });
  143. });