avatar.spec.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {render, screen} from 'sentry-test/reactTestingLibrary';
  2. import AvatarComponent from 'sentry/components/avatar';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. import {Avatar} from 'sentry/types';
  5. const gravatarBaseUrl = 'gravatarBaseUrl';
  6. const storeConfig = ConfigStore.getConfig();
  7. jest
  8. .spyOn(ConfigStore, 'getConfig')
  9. .mockImplementation(() => ({...storeConfig, gravatarBaseUrl}));
  10. describe('Avatar', function () {
  11. const avatar: Avatar = {
  12. avatarType: 'gravatar',
  13. avatarUuid: '2d641b5d-8c74-44de-9cb6-fbd54701b35e',
  14. };
  15. const user = {
  16. id: '1',
  17. name: 'Jane Bloggs',
  18. username: 'janebloggs@example.com',
  19. email: 'janebloggs@example.com',
  20. ip_address: '127.0.0.1',
  21. avatar,
  22. };
  23. const userNameInitials = user.name
  24. .split(' ')
  25. .map(n => n[0])
  26. .join('');
  27. describe('render()', function () {
  28. it('has `avatar` className', function () {
  29. render(<AvatarComponent user={user} />);
  30. const avatarElement = screen.getByTestId(`${avatar.avatarType}-avatar`);
  31. expect(avatarElement).toBeInTheDocument();
  32. expect(avatarElement).toHaveAttribute('title', user.name);
  33. });
  34. it('should show a gravatar when avatar type is gravatar', async function () {
  35. render(<AvatarComponent user={user} />);
  36. expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
  37. const avatarImage = await screen.findByRole('img');
  38. expect(avatarImage).toHaveAttribute(
  39. 'src',
  40. `${gravatarBaseUrl}/avatar/a94c88e18c44e553497bf642449b6398?d=404&s=120`
  41. );
  42. });
  43. it('should show an upload when avatar type is upload', async function () {
  44. avatar.avatarType = 'upload';
  45. render(<AvatarComponent user={user} />);
  46. expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
  47. const avatarImage = await screen.findByRole('img');
  48. expect(avatarImage).toHaveAttribute('src', `/avatar/${avatar.avatarUuid}/?s=120`);
  49. });
  50. it('should show an upload with the correct size (static 120 size)', async function () {
  51. const avatar1 = render(<AvatarComponent user={user} size={76} />);
  52. expect(await screen.findByRole('img')).toHaveAttribute(
  53. 'src',
  54. `/avatar/${avatar.avatarUuid}/?s=120`
  55. );
  56. avatar1.unmount();
  57. const avatar2 = render(<AvatarComponent user={user} size={121} />);
  58. expect(await screen.findByRole('img')).toHaveAttribute(
  59. 'src',
  60. `/avatar/${avatar.avatarUuid}/?s=120`
  61. );
  62. avatar2.unmount();
  63. const avatar3 = render(<AvatarComponent user={user} size={32} />);
  64. expect(await screen.findByRole('img')).toHaveAttribute(
  65. 'src',
  66. `/avatar/${avatar.avatarUuid}/?s=120`
  67. );
  68. avatar3.unmount();
  69. render(<AvatarComponent user={user} size={1} />);
  70. expect(await screen.findByRole('img')).toHaveAttribute(
  71. 'src',
  72. `/avatar/${avatar.avatarUuid}/?s=120`
  73. );
  74. });
  75. it('should not show upload or gravatar when avatar type is letter', function () {
  76. avatar.avatarType = 'letter_avatar';
  77. render(<AvatarComponent user={user} />);
  78. expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
  79. expect(screen.getByText(userNameInitials)).toBeInTheDocument();
  80. });
  81. it('use letter avatar by default, when no avatar type is set and user has an email address', function () {
  82. render(<AvatarComponent user={{...user, avatar: undefined}} />);
  83. expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
  84. expect(screen.getByText(userNameInitials)).toBeInTheDocument();
  85. });
  86. it('should show a gravatar when no avatar type is set and user has an email address', function () {
  87. render(<AvatarComponent gravatar user={{...user, avatar: undefined}} />);
  88. const avatarElement = screen.getByTestId(`gravatar-avatar`);
  89. expect(avatarElement).toBeInTheDocument();
  90. expect(avatarElement).toHaveAttribute('title', user.name);
  91. });
  92. it('should not show a gravatar when no avatar type is set and user has no email address', function () {
  93. render(<AvatarComponent gravatar user={{...user, email: '', avatar: undefined}} />);
  94. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  95. expect(screen.getByText(userNameInitials)).toBeInTheDocument();
  96. });
  97. it('can display a team Avatar', function () {
  98. const team = TestStubs.Team({slug: 'test-team_test'});
  99. render(<AvatarComponent team={team} />);
  100. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  101. expect(screen.getByText('TT')).toBeInTheDocument();
  102. });
  103. it('can display an organization Avatar', function () {
  104. const organization = TestStubs.Organization({slug: 'test-organization'});
  105. render(<AvatarComponent organization={organization} />);
  106. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  107. expect(screen.getByText('TO')).toBeInTheDocument();
  108. });
  109. it('displays platform list icons for project Avatar', function () {
  110. const project = TestStubs.Project({
  111. platforms: ['python', 'javascript'],
  112. platform: 'java',
  113. });
  114. render(<AvatarComponent project={project} />);
  115. const platformIcon = screen.getByRole('img');
  116. expect(platformIcon).toBeInTheDocument();
  117. expect(platformIcon).toHaveAttribute(
  118. 'data-test-id',
  119. `platform-icon-${project.platform}`
  120. );
  121. });
  122. it('displays a fallback platform list for project Avatar using the `platform` specified during onboarding', function () {
  123. const project = TestStubs.Project({platform: 'java'});
  124. render(<AvatarComponent project={project} />);
  125. const platformIcon = screen.getByRole('img');
  126. expect(platformIcon).toBeInTheDocument();
  127. expect(platformIcon).toHaveAttribute(
  128. 'data-test-id',
  129. `platform-icon-${project.platform}`
  130. );
  131. });
  132. it('uses onboarding project when platforms is an empty array', function () {
  133. const project = TestStubs.Project({platforms: [], platform: 'java'});
  134. render(<AvatarComponent project={project} />);
  135. const platformIcon = screen.getByRole('img');
  136. expect(platformIcon).toBeInTheDocument();
  137. expect(platformIcon).toHaveAttribute(
  138. 'data-test-id',
  139. `platform-icon-${project.platform}`
  140. );
  141. });
  142. it('renders the correct SentryApp depending on its props', async function () {
  143. const colorAvatar = {avatarUuid: 'abc', avatarType: 'upload', color: true};
  144. const simpleAvatar = {avatarUuid: 'def', avatarType: 'upload', color: false};
  145. const sentryApp = TestStubs.SentryApp({
  146. avatars: [colorAvatar, simpleAvatar],
  147. });
  148. const avatar1 = render(<AvatarComponent sentryApp={sentryApp} isColor />);
  149. expect(await screen.findByRole('img')).toHaveAttribute(
  150. 'src',
  151. `/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
  152. );
  153. avatar1.unmount();
  154. const avatar2 = render(<AvatarComponent sentryApp={sentryApp} isColor={false} />);
  155. expect(await screen.findByRole('img')).toHaveAttribute(
  156. 'src',
  157. `/sentry-app-avatar/${simpleAvatar.avatarUuid}/?s=120`
  158. );
  159. avatar2.unmount();
  160. render(<AvatarComponent sentryApp={sentryApp} isDefault />);
  161. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  162. });
  163. it('renders the correct fallbacks for SentryAppAvatars', async function () {
  164. const colorAvatar = {avatarUuid: 'abc', avatarType: 'upload', color: true};
  165. const sentryApp = TestStubs.SentryApp({avatars: []});
  166. // No existing avatars
  167. const avatar1 = render(<AvatarComponent sentryApp={sentryApp} isColor />);
  168. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  169. avatar1.unmount();
  170. // No provided `isColor` attribute
  171. sentryApp.avatars.push(colorAvatar);
  172. const avatar2 = render(<AvatarComponent sentryApp={sentryApp} />);
  173. expect(await screen.findByRole('img')).toHaveAttribute(
  174. 'src',
  175. `/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
  176. );
  177. avatar2.unmount();
  178. // avatarType of `default`
  179. sentryApp.avatars[0].avatarType = 'default';
  180. render(<AvatarComponent sentryApp={sentryApp} isColor />);
  181. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  182. });
  183. });
  184. });