avatar.spec.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {mountWithTheme, 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. mountWithTheme(<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. mountWithTheme(<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. mountWithTheme(<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 = mountWithTheme(<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 = mountWithTheme(<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 = mountWithTheme(<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. mountWithTheme(<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. mountWithTheme(<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. mountWithTheme(<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. mountWithTheme(<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. mountWithTheme(
  94. <AvatarComponent gravatar user={{...user, email: '', avatar: undefined}} />
  95. );
  96. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  97. expect(screen.getByText(userNameInitials)).toBeInTheDocument();
  98. });
  99. it('can display a team Avatar', function () {
  100. const team = TestStubs.Team({slug: 'test-team_test'});
  101. mountWithTheme(<AvatarComponent team={team} />);
  102. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  103. expect(screen.getByText('TT')).toBeInTheDocument();
  104. });
  105. it('can display an organization Avatar', function () {
  106. const organization = TestStubs.Organization({slug: 'test-organization'});
  107. mountWithTheme(<AvatarComponent organization={organization} />);
  108. expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
  109. expect(screen.getByText('TO')).toBeInTheDocument();
  110. });
  111. it('displays platform list icons for project Avatar', function () {
  112. const project = TestStubs.Project({
  113. platforms: ['python', 'javascript'],
  114. platform: 'java',
  115. });
  116. mountWithTheme(<AvatarComponent project={project} />);
  117. const platformIcon = screen.getByRole('img');
  118. expect(platformIcon).toBeInTheDocument();
  119. expect(platformIcon).toHaveAttribute(
  120. 'data-test-id',
  121. `platform-icon-${project.platform}`
  122. );
  123. });
  124. it('displays a fallback platform list for project Avatar using the `platform` specified during onboarding', function () {
  125. const project = TestStubs.Project({platform: 'java'});
  126. mountWithTheme(<AvatarComponent project={project} />);
  127. const platformIcon = screen.getByRole('img');
  128. expect(platformIcon).toBeInTheDocument();
  129. expect(platformIcon).toHaveAttribute(
  130. 'data-test-id',
  131. `platform-icon-${project.platform}`
  132. );
  133. });
  134. it('uses onboarding project when platforms is an empty array', function () {
  135. const project = TestStubs.Project({platforms: [], platform: 'java'});
  136. mountWithTheme(<AvatarComponent project={project} />);
  137. const platformIcon = screen.getByRole('img');
  138. expect(platformIcon).toBeInTheDocument();
  139. expect(platformIcon).toHaveAttribute(
  140. 'data-test-id',
  141. `platform-icon-${project.platform}`
  142. );
  143. });
  144. it('renders the correct SentryApp depending on its props', async function () {
  145. const colorAvatar = {avatarUuid: 'abc', avatarType: 'upload', color: true};
  146. const simpleAvatar = {avatarUuid: 'def', avatarType: 'upload', color: false};
  147. const sentryApp = TestStubs.SentryApp({
  148. avatars: [colorAvatar, simpleAvatar],
  149. });
  150. const avatar1 = mountWithTheme(<AvatarComponent sentryApp={sentryApp} isColor />);
  151. expect(await screen.findByRole('img')).toHaveAttribute(
  152. 'src',
  153. `/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
  154. );
  155. avatar1.unmount();
  156. const avatar2 = mountWithTheme(
  157. <AvatarComponent sentryApp={sentryApp} isColor={false} />
  158. );
  159. expect(await screen.findByRole('img')).toHaveAttribute(
  160. 'src',
  161. `/sentry-app-avatar/${simpleAvatar.avatarUuid}/?s=120`
  162. );
  163. avatar2.unmount();
  164. mountWithTheme(<AvatarComponent sentryApp={sentryApp} isDefault />);
  165. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  166. });
  167. it('renders the correct fallbacks for SentryAppAvatars', async function () {
  168. const colorAvatar = {avatarUuid: 'abc', avatarType: 'upload', color: true};
  169. const sentryApp = TestStubs.SentryApp({avatars: []});
  170. // No existing avatars
  171. const avatar1 = mountWithTheme(<AvatarComponent sentryApp={sentryApp} isColor />);
  172. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  173. avatar1.unmount();
  174. // No provided `isColor` attribute
  175. sentryApp.avatars.push(colorAvatar);
  176. const avatar2 = mountWithTheme(<AvatarComponent sentryApp={sentryApp} />);
  177. expect(await screen.findByRole('img')).toHaveAttribute(
  178. 'src',
  179. `/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
  180. );
  181. avatar2.unmount();
  182. // avatarType of `default`
  183. sentryApp.avatars[0].avatarType = 'default';
  184. mountWithTheme(<AvatarComponent sentryApp={sentryApp} isColor />);
  185. expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
  186. });
  187. });
  188. });