123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import {Organization} from 'sentry-fixture/organization';
- import {SentryApp} from 'sentry-fixture/sentryApp';
- import {render, screen} from 'sentry-test/reactTestingLibrary';
- import AvatarComponent from 'sentry/components/avatar';
- import ConfigStore from 'sentry/stores/configStore';
- import {Avatar} from 'sentry/types';
- describe('Avatar', function () {
- const avatar: Avatar = {
- avatarType: 'gravatar',
- avatarUuid: '2d641b5d-8c74-44de-9cb6-fbd54701b35e',
- avatarUrl: 'https://sentry.io/avatar/2d641b5d-8c74-44de-9cb6-fbd54701b35e/',
- };
- const user = {
- id: '1',
- name: 'Jane Bloggs',
- username: 'janebloggs@example.com',
- email: 'janebloggs@example.com',
- ip_address: '127.0.0.1',
- avatar,
- };
- window.__initialData = {
- links: {sentryUrl: 'https://sentry.io'},
- } as any;
- const userNameInitials = user.name
- .split(' ')
- .map(n => n[0])
- .join('');
- describe('render()', function () {
- it('has `avatar` className', async function () {
- render(<AvatarComponent user={user} />);
- await screen.findByRole('img');
- const avatarElement = screen.getByTestId(`${avatar.avatarType}-avatar`);
- expect(avatarElement).toBeInTheDocument();
- expect(avatarElement).toHaveAttribute('title', user.name);
- });
- it('should show a gravatar when avatar type is gravatar', async function () {
- const gravatarBaseUrl = 'gravatarBaseUrl';
- ConfigStore.set('gravatarBaseUrl', gravatarBaseUrl);
- render(<AvatarComponent user={user} />);
- expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
- const avatarImage = await screen.findByRole('img');
- expect(avatarImage).toHaveAttribute(
- 'src',
- `${gravatarBaseUrl}/avatar/a94c88e18c44e553497bf642449b6398?d=404&s=120`
- );
- });
- it('should show an upload when avatar type is upload', async function () {
- avatar.avatarType = 'upload';
- render(<AvatarComponent user={user} />);
- expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
- const avatarImage = await screen.findByRole('img');
- expect(avatarImage).toHaveAttribute(
- 'src',
- `https://sentry.io/avatar/${avatar.avatarUuid}/?s=120`
- );
- });
- it('should show an upload with the correct size (static 120 size)', async function () {
- const avatar1 = render(<AvatarComponent user={user} size={76} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/avatar/${avatar.avatarUuid}/?s=120`
- );
- avatar1.unmount();
- const avatar2 = render(<AvatarComponent user={user} size={121} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/avatar/${avatar.avatarUuid}/?s=120`
- );
- avatar2.unmount();
- const avatar3 = render(<AvatarComponent user={user} size={32} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/avatar/${avatar.avatarUuid}/?s=120`
- );
- avatar3.unmount();
- render(<AvatarComponent user={user} size={1} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/avatar/${avatar.avatarUuid}/?s=120`
- );
- });
- it('should not show upload or gravatar when avatar type is letter', function () {
- avatar.avatarType = 'letter_avatar';
- render(<AvatarComponent user={user} />);
- expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
- expect(screen.getByText(userNameInitials)).toBeInTheDocument();
- });
- it('use letter avatar by default, when no avatar type is set and user has an email address', function () {
- render(<AvatarComponent user={{...user, avatar: undefined}} />);
- expect(screen.getByTestId(`${avatar.avatarType}-avatar`)).toBeInTheDocument();
- expect(screen.getByText(userNameInitials)).toBeInTheDocument();
- });
- it('should show a gravatar when no avatar type is set and user has an email address', async function () {
- render(<AvatarComponent gravatar user={{...user, avatar: undefined}} />);
- await screen.findByRole('img');
- const avatarElement = screen.getByTestId(`gravatar-avatar`);
- expect(avatarElement).toBeInTheDocument();
- expect(avatarElement).toHaveAttribute('title', user.name);
- });
- it('should not show a gravatar when no avatar type is set and user has no email address', function () {
- render(<AvatarComponent gravatar user={{...user, email: '', avatar: undefined}} />);
- expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
- expect(screen.getByText(userNameInitials)).toBeInTheDocument();
- });
- it('can display a team Avatar', function () {
- const team = TestStubs.Team({slug: 'test-team_test'});
- render(<AvatarComponent team={team} />);
- expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
- expect(screen.getByText('TT')).toBeInTheDocument();
- });
- it('can display an organization Avatar', function () {
- const organization = Organization({
- slug: 'test-organization',
- avatar: {avatarType: 'letter_avatar', avatarUuid: ''},
- });
- render(<AvatarComponent organization={organization} />);
- expect(screen.getByTestId(`letter_avatar-avatar`)).toBeInTheDocument();
- expect(screen.getByText('TO')).toBeInTheDocument();
- });
- it('can display an organization Avatar upload', function () {
- const organization = Organization({
- slug: 'test-organization',
- avatar: {
- avatarType: 'upload',
- avatarUuid: 'abc123def',
- avatarUrl: 'https://us.sentry.io/organization-avatar/abc123def/',
- },
- });
- render(<AvatarComponent organization={organization} />);
- expect(screen.getByRole('img')).toHaveAttribute(
- 'src',
- 'https://us.sentry.io/organization-avatar/abc123def/?s=120'
- );
- });
- it('displays platform list icons for project Avatar', function () {
- const project = TestStubs.Project({
- platforms: ['python', 'javascript'],
- platform: 'java',
- });
- render(<AvatarComponent project={project} />);
- const platformIcon = screen.getByRole('img');
- expect(platformIcon).toBeInTheDocument();
- expect(platformIcon).toHaveAttribute(
- 'data-test-id',
- `platform-icon-${project.platform}`
- );
- });
- it('displays a fallback platform list for project Avatar using the `platform` specified during onboarding', function () {
- const project = TestStubs.Project({platform: 'java'});
- render(<AvatarComponent project={project} />);
- const platformIcon = screen.getByRole('img');
- expect(platformIcon).toBeInTheDocument();
- expect(platformIcon).toHaveAttribute(
- 'data-test-id',
- `platform-icon-${project.platform}`
- );
- });
- it('uses onboarding project when platforms is an empty array', function () {
- const project = TestStubs.Project({platforms: [], platform: 'java'});
- render(<AvatarComponent project={project} />);
- const platformIcon = screen.getByRole('img');
- expect(platformIcon).toBeInTheDocument();
- expect(platformIcon).toHaveAttribute(
- 'data-test-id',
- `platform-icon-${project.platform}`
- );
- });
- it('renders the correct SentryApp depending on its props', async function () {
- const colorAvatar = {
- avatarUuid: 'abc',
- avatarType: 'upload' as const,
- avatarUrl: 'https://sentry.io/sentry-app-avatar/abc/',
- color: true,
- };
- const simpleAvatar = {
- avatarUuid: 'def',
- avatarType: 'upload' as const,
- avatarUrl: 'https://sentry.io/sentry-app-avatar/def/',
- color: false,
- };
- const sentryApp = SentryApp({
- avatars: [colorAvatar, simpleAvatar],
- });
- const avatar1 = render(<AvatarComponent sentryApp={sentryApp} isColor />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
- );
- avatar1.unmount();
- const avatar2 = render(<AvatarComponent sentryApp={sentryApp} isColor={false} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/sentry-app-avatar/${simpleAvatar.avatarUuid}/?s=120`
- );
- avatar2.unmount();
- render(<AvatarComponent sentryApp={sentryApp} isDefault />);
- expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
- });
- it('renders the correct fallbacks for SentryAppAvatars', async function () {
- const colorAvatar = {
- avatarUuid: 'abc',
- avatarType: 'upload' as const,
- avatarUrl: 'https://sentry.io/sentry-app-avatar/abc/',
- color: true,
- };
- const sentryApp = SentryApp({avatars: []});
- // No existing avatars
- const avatar1 = render(<AvatarComponent sentryApp={sentryApp} isColor />);
- expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
- avatar1.unmount();
- // No provided `isColor` attribute
- sentryApp.avatars!.push(colorAvatar);
- const avatar2 = render(<AvatarComponent sentryApp={sentryApp} />);
- expect(await screen.findByRole('img')).toHaveAttribute(
- 'src',
- `https://sentry.io/sentry-app-avatar/${colorAvatar.avatarUuid}/?s=120`
- );
- avatar2.unmount();
- // avatarType of `default`
- sentryApp.avatars![0].avatarType = 'default';
- render(<AvatarComponent sentryApp={sentryApp} isColor />);
- expect(screen.getByTestId('default-sentry-app-avatar')).toBeInTheDocument();
- });
- });
- });
|