utils.spec.tsx 5.6 KB

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