formatters.spec.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {getExactDuration, userDisplayName} from 'sentry/utils/formatters';
  2. describe('formatters', function () {
  3. describe('userDisplayName', function () {
  4. it('should only show email, if name and email are the same', function () {
  5. expect(
  6. userDisplayName({
  7. name: 'foo@bar.com',
  8. email: 'foo@bar.com',
  9. })
  10. ).toEqual('foo@bar.com');
  11. });
  12. it('should show name + email, if name and email differ', function () {
  13. expect(
  14. userDisplayName({
  15. name: 'user',
  16. email: 'foo@bar.com',
  17. })
  18. ).toEqual('user (foo@bar.com)');
  19. });
  20. it('should show unknown author with email, if email is only provided', function () {
  21. expect(
  22. userDisplayName({
  23. email: 'foo@bar.com',
  24. })
  25. ).toEqual('Unknown author (foo@bar.com)');
  26. });
  27. it('should show unknown author, if author or email is just whitespace', function () {
  28. expect(
  29. userDisplayName({
  30. // eslint-disable-next-line quotes
  31. name: `\t\n `,
  32. })
  33. ).toEqual('Unknown author');
  34. expect(
  35. userDisplayName({
  36. // eslint-disable-next-line quotes
  37. email: `\t\n `,
  38. })
  39. ).toEqual('Unknown author');
  40. });
  41. it('should show unknown author, if user object is either not an object or incomplete', function () {
  42. expect(userDisplayName()).toEqual('Unknown author');
  43. expect(userDisplayName({})).toEqual('Unknown author');
  44. });
  45. });
  46. describe('getExactDuration', () => {
  47. it('should provide default value', () => {
  48. expect(getExactDuration(0)).toEqual('0 milliseconds');
  49. });
  50. it('should format in the right way', () => {
  51. expect(getExactDuration(2.030043848568126)).toEqual('2 seconds 30 milliseconds');
  52. expect(getExactDuration(0.2)).toEqual('200 milliseconds');
  53. expect(getExactDuration(13)).toEqual('13 seconds');
  54. expect(getExactDuration(60)).toEqual('1 minute');
  55. expect(getExactDuration(121)).toEqual('2 minutes 1 second');
  56. expect(getExactDuration(234235435)).toEqual(
  57. '387 weeks 2 days 1 hour 23 minutes 55 seconds'
  58. );
  59. });
  60. it('should format negative durations', () => {
  61. expect(getExactDuration(-2.030043848568126)).toEqual('-2 seconds -30 milliseconds');
  62. expect(getExactDuration(-0.2)).toEqual('-200 milliseconds');
  63. expect(getExactDuration(-13)).toEqual('-13 seconds');
  64. expect(getExactDuration(-60)).toEqual('-1 minute');
  65. expect(getExactDuration(-121)).toEqual('-2 minutes -1 second');
  66. expect(getExactDuration(-234235435)).toEqual(
  67. '-387 weeks -2 days -1 hour -23 minutes -55 seconds'
  68. );
  69. });
  70. it('should abbreviate label', () => {
  71. expect(getExactDuration(234235435, true)).toEqual('387wk 2d 1hr 23min 55s');
  72. });
  73. });
  74. });