index.spec.tsx 9.6 KB

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