letterAvatar.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import LetterAvatar from 'sentry/components/letterAvatar';
  3. describe('LetterAvatar', function () {
  4. const USER_1 = {
  5. identifier: 'janebloggs@example.com',
  6. displayName: 'Jane Bloggs',
  7. };
  8. const USER_2 = {
  9. identifier: 'johnsmith@example.com',
  10. displayName: 'johnsmith@example.com',
  11. };
  12. const USER_3 = {
  13. identifier: 'foo@example.com',
  14. displayName: 'foo@example.com',
  15. };
  16. const USER_4 = {
  17. identifier: '2',
  18. displayName: '',
  19. };
  20. const USER_5 = {
  21. identifier: '127.0.0.1',
  22. displayName: '',
  23. };
  24. const USER_6 = {
  25. identifier: 'janebloggs@example.com',
  26. displayName: 'Jane Bloggs ',
  27. };
  28. const USER_7 = {
  29. identifier: 'janebloggs@example.com',
  30. displayName: ' ',
  31. };
  32. const USER_8 = {
  33. identifier: 'janebloggs@example.com',
  34. displayName: '\u2603super \u2603duper',
  35. };
  36. const USER_9 = {
  37. identifier: 'janebloggs@example.com',
  38. displayName: 'jane austen bloggs',
  39. };
  40. describe('display name', function () {
  41. it('should get initials based on name', function () {
  42. render(<LetterAvatar {...USER_1} />);
  43. expect(screen.getByText('JB')).toBeInTheDocument();
  44. });
  45. it('should get initials based on email', function () {
  46. render(<LetterAvatar {...USER_2} />);
  47. expect(screen.getByText('J')).toBeInTheDocument();
  48. });
  49. it('should get initials based on username', function () {
  50. render(<LetterAvatar {...USER_3} />);
  51. expect(screen.getByText('F')).toBeInTheDocument();
  52. });
  53. it('should show question mark if user has no display name', function () {
  54. render(<LetterAvatar {...USER_4} />);
  55. expect(screen.getByText('?')).toBeInTheDocument();
  56. });
  57. it('should show question mark even if display name is a space', function () {
  58. render(<LetterAvatar {...USER_7} />);
  59. expect(screen.getByText('?')).toBeInTheDocument();
  60. });
  61. it('should get initials based on name even if there are trailing spaces', function () {
  62. render(<LetterAvatar {...USER_6} />);
  63. expect(screen.getByText('JB')).toBeInTheDocument();
  64. });
  65. it('should not slice multibyte characters in half', function () {
  66. render(<LetterAvatar {...USER_8} />);
  67. expect(screen.getByText('\u2603\u2603')).toBeInTheDocument();
  68. });
  69. it('should pick most last name', function () {
  70. render(<LetterAvatar {...USER_9} />);
  71. expect(screen.getByText('JB')).toBeInTheDocument();
  72. });
  73. });
  74. describe('color', function () {
  75. it('should return a color based on email', function () {
  76. const {container} = render(<LetterAvatar {...USER_1} />);
  77. expect(container).toSnapshot();
  78. });
  79. it('should return a color based on username', function () {
  80. const {container} = render(<LetterAvatar {...USER_3} />);
  81. expect(container).toSnapshot();
  82. });
  83. it('should return a color based on id', function () {
  84. const {container} = render(<LetterAvatar {...USER_4} />);
  85. expect(container).toSnapshot();
  86. });
  87. it('should return a color based on ip address', function () {
  88. const {container} = render(<LetterAvatar {...USER_5} />);
  89. expect(container).toSnapshot();
  90. });
  91. });
  92. });