index.spec.tsx 8.3 KB

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