formatters.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import {
  2. DAY,
  3. formatAbbreviatedNumber,
  4. formatFloat,
  5. formatPercentage,
  6. formatSecondsToClock,
  7. getDuration,
  8. getExactDuration,
  9. MONTH,
  10. parseClockToSeconds,
  11. userDisplayName,
  12. WEEK,
  13. } from 'sentry/utils/formatters';
  14. describe('getDuration()', function () {
  15. it('should format durations', function () {
  16. expect(formatSecondsToClock(0)).toBe('00:00');
  17. expect(formatSecondsToClock(0.001)).toBe('00:00.001');
  18. expect(formatSecondsToClock(0.01)).toBe('00:00.010');
  19. expect(getDuration(0.1)).toBe('100 milliseconds');
  20. expect(getDuration(0.1, 2)).toBe('100.00 milliseconds');
  21. expect(getDuration(1)).toBe('1 second');
  22. expect(getDuration(2)).toBe('2 seconds');
  23. expect(getDuration(65)).toBe('1 minute');
  24. expect(getDuration(122)).toBe('2 minutes');
  25. expect(getDuration(3720)).toBe('1 hour');
  26. expect(getDuration(36000)).toBe('10 hours');
  27. expect(getDuration(86400)).toBe('1 day');
  28. expect(getDuration(86400 * 2)).toBe('2 days');
  29. expect(getDuration(604800)).toBe('1 week');
  30. expect(getDuration(604800 * 4)).toBe('4 weeks');
  31. expect(getDuration(2629800)).toBe('1 month');
  32. expect(getDuration(604800 * 12)).toBe('3 months');
  33. });
  34. it('should format negative durations', function () {
  35. expect(formatSecondsToClock(0)).toBe('00:00');
  36. expect(formatSecondsToClock(-0.001)).toBe('00:00.001');
  37. expect(formatSecondsToClock(-0.01)).toBe('00:00.010');
  38. expect(getDuration(-0.1)).toBe('-100 milliseconds');
  39. expect(getDuration(-0.1, 2)).toBe('-100.00 milliseconds');
  40. expect(getDuration(-1)).toBe('-1 second');
  41. expect(getDuration(-2)).toBe('-2 seconds');
  42. expect(getDuration(-65)).toBe('-1 minute');
  43. expect(getDuration(-122)).toBe('-2 minutes');
  44. expect(getDuration(-3720)).toBe('-1 hour');
  45. expect(getDuration(-36000)).toBe('-10 hours');
  46. expect(getDuration(-86400)).toBe('-1 day');
  47. expect(getDuration(-86400 * 2)).toBe('-2 days');
  48. expect(getDuration(-604800)).toBe('-1 week');
  49. expect(getDuration(-604800 * 4)).toBe('-4 weeks');
  50. expect(getDuration(-2629800)).toBe('-1 month');
  51. expect(getDuration(-604800 * 12)).toBe('-3 months');
  52. });
  53. it('should format numbers and abbreviate units', function () {
  54. expect(getDuration(0, 2, true)).toBe('0.00ms');
  55. expect(getDuration(0, 0, true)).toBe('0ms');
  56. expect(getDuration(0.1, 0, true)).toBe('100ms');
  57. expect(getDuration(0.1, 2, true)).toBe('100.00ms');
  58. expect(getDuration(1, 2, true)).toBe('1.00s');
  59. expect(getDuration(122, 0, true)).toBe('2min');
  60. expect(getDuration(3600, 0, true)).toBe('1hr');
  61. expect(getDuration(86400, 0, true)).toBe('1d');
  62. expect(getDuration(86400 * 2, 0, true)).toBe('2d');
  63. expect(getDuration(604800, 0, true)).toBe('1wk');
  64. expect(getDuration(604800 * 2, 0, true)).toBe('2wk');
  65. expect(getDuration(2629800, 0, true)).toBe('1mo');
  66. expect(getDuration(604800 * 12, 0, true)).toBe('3mo');
  67. });
  68. it('should format numbers and abbreviate units with one letter', function () {
  69. expect(getDuration(0, 2, false, true)).toBe('0.00ms');
  70. expect(getDuration(0, 0, false, true)).toBe('0ms');
  71. expect(getDuration(0.1, 0, false, true)).toBe('100ms');
  72. expect(getDuration(0.1, 2, false, true)).toBe('100.00ms');
  73. expect(getDuration(1, 2, false, true)).toBe('1.00s');
  74. expect(getDuration(122, 0, false, true)).toBe('2m');
  75. expect(getDuration(3600, 0, false, true)).toBe('1h');
  76. expect(getDuration(86400, 0, false, true)).toBe('1d');
  77. expect(getDuration(86400 * 2, 0, false, true)).toBe('2d');
  78. expect(getDuration(604800, 0, false, true)).toBe('1w');
  79. expect(getDuration(604800 * 2, 0, false, true)).toBe('2w');
  80. expect(getDuration(2629800, 0, false, true)).toBe('4w');
  81. expect(getDuration(604800 * 12, 0, false, true)).toBe('12w');
  82. });
  83. it('should format negative durations with absolute', function () {
  84. expect(formatSecondsToClock(0)).toBe('00:00');
  85. expect(formatSecondsToClock(-0.001)).toBe('00:00.001');
  86. expect(formatSecondsToClock(-0.01)).toBe('00:00.010');
  87. expect(getDuration(-0.1, 0, false, false, true)).toBe('100 milliseconds');
  88. expect(getDuration(-0.1, 2, false, false, true)).toBe('100.00 milliseconds');
  89. expect(getDuration(-1, 0, false, false, true)).toBe('1 second');
  90. expect(getDuration(-2, 0, false, false, true)).toBe('2 seconds');
  91. expect(getDuration(-65, 0, false, false, true)).toBe('1 minute');
  92. expect(getDuration(-122, 0, false, false, true)).toBe('2 minutes');
  93. expect(getDuration(-3720, 0, false, false, true)).toBe('1 hour');
  94. expect(getDuration(-36000, 0, false, false, true)).toBe('10 hours');
  95. expect(getDuration(-86400, 0, false, false, true)).toBe('1 day');
  96. expect(getDuration(-86400 * 2, 0, false, false, true)).toBe('2 days');
  97. expect(getDuration(-604800, 0, false, false, true)).toBe('1 week');
  98. expect(getDuration(-604800 * 4, 0, false, false, true)).toBe('4 weeks');
  99. expect(getDuration(-2629800, 0, false, false, true)).toBe('1 month');
  100. expect(getDuration(-604800 * 12, 0, false, false, true)).toBe('3 months');
  101. });
  102. });
  103. describe('formatSecondsToClock', function () {
  104. it('should format durations', function () {
  105. expect(formatSecondsToClock(0)).toBe('00:00');
  106. expect(formatSecondsToClock(0.1)).toBe('00:00.100');
  107. expect(formatSecondsToClock(1)).toBe('00:01');
  108. expect(formatSecondsToClock(2)).toBe('00:02');
  109. expect(formatSecondsToClock(65)).toBe('01:05');
  110. expect(formatSecondsToClock(65.123)).toBe('01:05.123');
  111. expect(formatSecondsToClock(122)).toBe('02:02');
  112. expect(formatSecondsToClock(3720)).toBe('01:02:00');
  113. expect(formatSecondsToClock(36000)).toBe('10:00:00');
  114. expect(formatSecondsToClock(86400)).toBe('24:00:00');
  115. expect(formatSecondsToClock(86400 * 2)).toBe('48:00:00');
  116. });
  117. it('should format negative durations', function () {
  118. expect(formatSecondsToClock(-0)).toBe('00:00');
  119. expect(formatSecondsToClock(-0.1)).toBe('00:00.100');
  120. expect(formatSecondsToClock(-1)).toBe('00:01');
  121. expect(formatSecondsToClock(-2)).toBe('00:02');
  122. expect(formatSecondsToClock(-65)).toBe('01:05');
  123. expect(formatSecondsToClock(-65.123)).toBe('01:05.123');
  124. expect(formatSecondsToClock(-122)).toBe('02:02');
  125. expect(formatSecondsToClock(-3720)).toBe('01:02:00');
  126. expect(formatSecondsToClock(-36000)).toBe('10:00:00');
  127. expect(formatSecondsToClock(-86400)).toBe('24:00:00');
  128. expect(formatSecondsToClock(-86400 * 2)).toBe('48:00:00');
  129. });
  130. it('should not pad when padAll:false is set', function () {
  131. const padAll = false;
  132. expect(formatSecondsToClock(0, {padAll})).toBe('0:00');
  133. expect(formatSecondsToClock(0.1, {padAll})).toBe('0:00.100');
  134. expect(formatSecondsToClock(1, {padAll})).toBe('0:01');
  135. expect(formatSecondsToClock(65, {padAll})).toBe('1:05');
  136. expect(formatSecondsToClock(3720, {padAll})).toBe('1:02:00');
  137. });
  138. });
  139. describe('parseClockToSeconds', function () {
  140. it('should format durations', function () {
  141. expect(parseClockToSeconds('0:00')).toBe(0);
  142. expect(parseClockToSeconds('0:00.100')).toBe(0.1);
  143. expect(parseClockToSeconds('0:01')).toBe(1);
  144. expect(parseClockToSeconds('0:02')).toBe(2);
  145. expect(parseClockToSeconds('1:05')).toBe(65);
  146. expect(parseClockToSeconds('1:05.123')).toBe(65.123);
  147. expect(parseClockToSeconds('2:02')).toBe(122);
  148. expect(parseClockToSeconds('1:02:00')).toBe(3720);
  149. expect(parseClockToSeconds('10:00:00')).toBe(36000);
  150. expect(parseClockToSeconds('24:00:00')).toBe(DAY / 1000);
  151. expect(parseClockToSeconds('48:00:00')).toBe((DAY * 2) / 1000);
  152. expect(parseClockToSeconds('2:00:00:00')).toBe((DAY * 2) / 1000);
  153. expect(parseClockToSeconds('1:00:00:00:00')).toBe(WEEK / 1000);
  154. expect(parseClockToSeconds('1:00:00:00:00:00')).toBe(MONTH / 1000);
  155. });
  156. it('should ignore non-numeric input', function () {
  157. expect(parseClockToSeconds('hello world')).toBe(0);
  158. expect(parseClockToSeconds('a:b:c')).toBe(0);
  159. expect(parseClockToSeconds('a:b:c.d')).toBe(0);
  160. expect(parseClockToSeconds('a:b:10.d')).toBe(10);
  161. expect(parseClockToSeconds('a:10:c.d')).toBe(600);
  162. });
  163. it('should handle as much invalid input as possible', function () {
  164. expect(parseClockToSeconds('a:b:c.123')).toBe(0.123);
  165. expect(parseClockToSeconds('a:b:10.d')).toBe(10);
  166. expect(parseClockToSeconds('a:10:c.d')).toBe(600);
  167. });
  168. });
  169. describe('formatAbbreviatedNumber()', function () {
  170. it('should abbreviate numbers', function () {
  171. expect(formatAbbreviatedNumber(0)).toBe('0');
  172. expect(formatAbbreviatedNumber(100)).toBe('100');
  173. expect(formatAbbreviatedNumber(1000)).toBe('1k');
  174. expect(formatAbbreviatedNumber(10000000)).toBe('10m');
  175. expect(formatAbbreviatedNumber(100000000000)).toBe('100b');
  176. expect(formatAbbreviatedNumber(1000000000000)).toBe('1000b');
  177. });
  178. it('should abbreviate numbers that are strings', function () {
  179. expect(formatAbbreviatedNumber('00')).toBe('0');
  180. expect(formatAbbreviatedNumber('100')).toBe('100');
  181. expect(formatAbbreviatedNumber('1000')).toBe('1k');
  182. expect(formatAbbreviatedNumber('10000000')).toBe('10m');
  183. expect(formatAbbreviatedNumber('100000000000')).toBe('100b');
  184. expect(formatAbbreviatedNumber('1000000000000')).toBe('1000b');
  185. });
  186. });
  187. describe('formatFloat()', function () {
  188. it('should format decimals', function () {
  189. expect(formatFloat(0, 0)).toBe(0);
  190. expect(formatFloat(10.513434, 1)).toBe(10.5);
  191. expect(formatFloat(10.513494, 3)).toBe(10.513);
  192. });
  193. it('should not round', function () {
  194. expect(formatFloat(10.513494, 4)).toBe(10.5134);
  195. });
  196. });
  197. describe('formatPercentage()', function () {
  198. it('should format decimals', function () {
  199. expect(formatPercentage(0.0, 0)).toBe('0%');
  200. expect(formatPercentage(0.0, 2)).toBe('0%');
  201. expect(formatPercentage(0.10513434, 1)).toBe('10.5%');
  202. expect(formatPercentage(0.10513494, 3)).toBe('10.513%');
  203. expect(formatPercentage(0.10513494, 4)).toBe('10.5135%');
  204. });
  205. });
  206. describe('userDisplayName', function () {
  207. it('should only show email, if name and email are the same', function () {
  208. expect(
  209. userDisplayName({
  210. name: 'foo@bar.com',
  211. email: 'foo@bar.com',
  212. })
  213. ).toEqual('foo@bar.com');
  214. });
  215. it('should show name + email, if name and email differ', function () {
  216. expect(
  217. userDisplayName({
  218. name: 'user',
  219. email: 'foo@bar.com',
  220. })
  221. ).toEqual('user (foo@bar.com)');
  222. });
  223. it('should show unknown author with email, if email is only provided', function () {
  224. expect(
  225. userDisplayName({
  226. email: 'foo@bar.com',
  227. })
  228. ).toEqual('Unknown author (foo@bar.com)');
  229. });
  230. it('should show unknown author, if author or email is just whitespace', function () {
  231. expect(
  232. userDisplayName({
  233. // eslint-disable-next-line quotes
  234. name: `\t\n `,
  235. })
  236. ).toEqual('Unknown author');
  237. expect(
  238. userDisplayName({
  239. // eslint-disable-next-line quotes
  240. email: `\t\n `,
  241. })
  242. ).toEqual('Unknown author');
  243. });
  244. it('should show unknown author, if user object is either not an object or incomplete', function () {
  245. // @ts-expect-error
  246. expect(userDisplayName()).toEqual('Unknown author');
  247. expect(userDisplayName({})).toEqual('Unknown author');
  248. });
  249. });
  250. describe('getExactDuration', () => {
  251. it('should provide default value', () => {
  252. expect(getExactDuration(0)).toEqual('0 milliseconds');
  253. });
  254. it('should format in the right way', () => {
  255. expect(getExactDuration(2.030043848568126)).toEqual('2 seconds 30 milliseconds');
  256. expect(getExactDuration(0.2)).toEqual('200 milliseconds');
  257. expect(getExactDuration(13)).toEqual('13 seconds');
  258. expect(getExactDuration(60)).toEqual('1 minute');
  259. expect(getExactDuration(121)).toEqual('2 minutes 1 second');
  260. expect(getExactDuration(234235435)).toEqual(
  261. '387 weeks 2 days 1 hour 23 minutes 55 seconds'
  262. );
  263. });
  264. it('should format negative durations', () => {
  265. expect(getExactDuration(-2.030043848568126)).toEqual('-2 seconds -30 milliseconds');
  266. expect(getExactDuration(-0.2)).toEqual('-200 milliseconds');
  267. expect(getExactDuration(-13)).toEqual('-13 seconds');
  268. expect(getExactDuration(-60)).toEqual('-1 minute');
  269. expect(getExactDuration(-121)).toEqual('-2 minutes -1 second');
  270. expect(getExactDuration(-234235435)).toEqual(
  271. '-387 weeks -2 days -1 hour -23 minutes -55 seconds'
  272. );
  273. });
  274. it('should abbreviate label', () => {
  275. expect(getExactDuration(234235435, true)).toEqual('387wk 2d 1hr 23min 55s');
  276. });
  277. });