actorAvatar.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {TeamFixture} from 'sentry-fixture/team';
  3. import {UserFixture} from 'sentry-fixture/user';
  4. import {render, screen} from 'sentry-test/reactTestingLibrary';
  5. import ActorAvatar from 'sentry/components/avatar/actorAvatar';
  6. import MemberListStore from 'sentry/stores/memberListStore';
  7. import OrganizationStore from 'sentry/stores/organizationStore';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import type {Team as TeamType} from 'sentry/types/organization';
  10. import type {User as UserType} from 'sentry/types/user';
  11. describe('ActorAvatar', function () {
  12. const user: UserType = {
  13. ...UserFixture(),
  14. id: '1',
  15. name: 'JanActore Bloggs',
  16. email: 'janebloggs@example.com',
  17. };
  18. const team1: TeamType = {
  19. ...TeamFixture(),
  20. id: '3',
  21. slug: 'cool-team',
  22. name: 'COOL TEAM',
  23. };
  24. beforeEach(function () {
  25. MemberListStore.loadInitialData([user]);
  26. TeamStore.loadInitialData([team1]);
  27. });
  28. it('should show a gravatar when actor type is a user', function () {
  29. render(
  30. <ActorAvatar
  31. actor={{
  32. id: '1',
  33. name: 'Jane Bloggs',
  34. type: 'user',
  35. }}
  36. />
  37. );
  38. });
  39. it('should not show a gravatar when actor type is a team', function () {
  40. render(
  41. <ActorAvatar
  42. actor={{
  43. id: '3',
  44. name: 'COOL TEAM',
  45. type: 'team',
  46. }}
  47. />
  48. );
  49. expect(screen.getByText('CT')).toBeInTheDocument();
  50. });
  51. it('should show an avatar even if the user is not in the memberlist', function () {
  52. render(
  53. <ActorAvatar
  54. actor={{
  55. id: '2',
  56. name: 'Jane Vloggs',
  57. type: 'user',
  58. }}
  59. />
  60. );
  61. expect(screen.getByText('JV')).toBeInTheDocument();
  62. });
  63. it('should return null when actor type is a unknown', function () {
  64. render(
  65. <ActorAvatar
  66. actor={{
  67. id: '3',
  68. name: 'COOL TEAM',
  69. // @ts-expect-error (type shall be incorrect here)
  70. type: 'teapot',
  71. }}
  72. />
  73. );
  74. expect(screen.queryByText('CT')).not.toBeInTheDocument();
  75. });
  76. it('should fetch a team not in the store', async function () {
  77. const organization = OrganizationFixture();
  78. OrganizationStore.onUpdate(organization, {replace: true});
  79. const team2 = TeamFixture({id: '2', name: 'COOL TEAM', slug: 'cool-team'});
  80. const mockRequest = MockApiClient.addMockResponse({
  81. url: `/organizations/${organization.slug}/teams/`,
  82. method: 'GET',
  83. body: [team2],
  84. });
  85. render(
  86. <ActorAvatar
  87. actor={{
  88. id: team2.id,
  89. name: team2.name,
  90. type: 'team',
  91. }}
  92. />
  93. );
  94. expect(await screen.findByText('CT')).toBeInTheDocument();
  95. expect(mockRequest).toHaveBeenCalled();
  96. });
  97. it('should fetch a user not in the store', async function () {
  98. const organization = OrganizationFixture();
  99. OrganizationStore.onUpdate(organization, {replace: true});
  100. const user2 = UserFixture({id: '2', name: 'COOL USER'});
  101. const mockRequest = MockApiClient.addMockResponse({
  102. url: `/organizations/${organization.slug}/members/`,
  103. method: 'GET',
  104. body: [{user: user2}],
  105. });
  106. render(
  107. <ActorAvatar
  108. actor={{
  109. id: user2.id,
  110. name: user2.name,
  111. email: user2.email,
  112. type: 'user',
  113. }}
  114. />
  115. );
  116. expect(await screen.findByText('CU')).toBeInTheDocument();
  117. expect(mockRequest).toHaveBeenCalled();
  118. });
  119. });