formatters.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. it('should round to 1 decimal place', function () {
  187. expect(formatAbbreviatedNumber(100.12)).toBe('100.12');
  188. expect(formatAbbreviatedNumber(1500)).toBe('1.5k');
  189. expect(formatAbbreviatedNumber(1213122)).toBe('1.2m');
  190. });
  191. });
  192. describe('formatFloat()', function () {
  193. it('should format decimals', function () {
  194. expect(formatFloat(0, 0)).toBe(0);
  195. expect(formatFloat(10.513434, 1)).toBe(10.5);
  196. expect(formatFloat(10.513494, 3)).toBe(10.513);
  197. });
  198. it('should not round', function () {
  199. expect(formatFloat(10.513494, 4)).toBe(10.5134);
  200. });
  201. });
  202. describe('formatPercentage()', function () {
  203. it('should format decimals', function () {
  204. expect(formatPercentage(0.0, 0)).toBe('0%');
  205. expect(formatPercentage(0.0, 2)).toBe('0%');
  206. expect(formatPercentage(0.10513434, 1)).toBe('10.5%');
  207. expect(formatPercentage(0.10513494, 3)).toBe('10.513%');
  208. expect(formatPercentage(0.10513494, 4)).toBe('10.5135%');
  209. });
  210. });
  211. describe('userDisplayName', function () {
  212. it('should only show email, if name and email are the same', function () {
  213. expect(
  214. userDisplayName({
  215. name: 'foo@bar.com',
  216. email: 'foo@bar.com',
  217. })
  218. ).toEqual('foo@bar.com');
  219. });
  220. it('should show name + email, if name and email differ', function () {
  221. expect(
  222. userDisplayName({
  223. name: 'user',
  224. email: 'foo@bar.com',
  225. })
  226. ).toEqual('user (foo@bar.com)');
  227. });
  228. it('should show unknown author with email, if email is only provided', function () {
  229. expect(
  230. userDisplayName({
  231. email: 'foo@bar.com',
  232. })
  233. ).toEqual('Unknown author (foo@bar.com)');
  234. });
  235. it('should show unknown author, if author or email is just whitespace', function () {
  236. expect(
  237. userDisplayName({
  238. // eslint-disable-next-line quotes
  239. name: `\t\n `,
  240. })
  241. ).toEqual('Unknown author');
  242. expect(
  243. userDisplayName({
  244. // eslint-disable-next-line quotes
  245. email: `\t\n `,
  246. })
  247. ).toEqual('Unknown author');
  248. });
  249. it('should show unknown author, if user object is either not an object or incomplete', function () {
  250. // @ts-expect-error
  251. expect(userDisplayName()).toEqual('Unknown author');
  252. expect(userDisplayName({})).toEqual('Unknown author');
  253. });
  254. });
  255. describe('getExactDuration', () => {
  256. it('should provide default value', () => {
  257. expect(getExactDuration(0)).toEqual('0 milliseconds');
  258. });
  259. it('should format in the right way', () => {
  260. expect(getExactDuration(2.030043848568126)).toEqual('2 seconds 30 milliseconds');
  261. expect(getExactDuration(0.2)).toEqual('200 milliseconds');
  262. expect(getExactDuration(13)).toEqual('13 seconds');
  263. expect(getExactDuration(60)).toEqual('1 minute');
  264. expect(getExactDuration(121)).toEqual('2 minutes 1 second');
  265. expect(getExactDuration(234235435)).toEqual(
  266. '387 weeks 2 days 1 hour 23 minutes 55 seconds'
  267. );
  268. });
  269. it('should format negative durations', () => {
  270. expect(getExactDuration(-2.030043848568126)).toEqual('-2 seconds -30 milliseconds');
  271. expect(getExactDuration(-0.2)).toEqual('-200 milliseconds');
  272. expect(getExactDuration(-13)).toEqual('-13 seconds');
  273. expect(getExactDuration(-60)).toEqual('-1 minute');
  274. expect(getExactDuration(-121)).toEqual('-2 minutes -1 second');
  275. expect(getExactDuration(-234235435)).toEqual(
  276. '-387 weeks -2 days -1 hour -23 minutes -55 seconds'
  277. );
  278. });
  279. it('should abbreviate label', () => {
  280. expect(getExactDuration(234235435, true)).toEqual('387wk 2d 1hr 23min 55s');
  281. });
  282. });