assignedTo.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import AssignedTo from 'sentry/components/group/assignedTo';
  3. import GroupStore from 'sentry/stores/groupStore';
  4. import MemberListStore from 'sentry/stores/memberListStore';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import TeamStore from 'sentry/stores/teamStore';
  7. describe('Group > AssignedTo', () => {
  8. let USER_1, USER_2;
  9. let TEAM_1;
  10. let PROJECT_1;
  11. let GROUP_1;
  12. let organization;
  13. const project = TestStubs.Project();
  14. beforeEach(() => {
  15. organization = TestStubs.Organization();
  16. USER_1 = TestStubs.User({
  17. id: '1',
  18. name: 'Jane Bloggs',
  19. email: 'janebloggs@example.com',
  20. });
  21. USER_2 = TestStubs.User({
  22. id: '2',
  23. name: 'John Smith',
  24. email: 'johnsmith@example.com',
  25. });
  26. TEAM_1 = TestStubs.Team({
  27. id: '3',
  28. name: 'COOL TEAM',
  29. slug: 'cool-team',
  30. });
  31. PROJECT_1 = TestStubs.Project({
  32. teams: [TEAM_1],
  33. });
  34. GROUP_1 = TestStubs.Group({
  35. id: '1337',
  36. project: {
  37. id: PROJECT_1.id,
  38. slug: PROJECT_1.slug,
  39. },
  40. });
  41. TeamStore.loadInitialData([TEAM_1]);
  42. ProjectsStore.loadInitialData([PROJECT_1]);
  43. GroupStore.loadInitialData([GROUP_1]);
  44. jest.spyOn(GroupStore, 'get').mockImplementation(() => GROUP_1);
  45. MockApiClient.addMockResponse({
  46. url: '/organizations/org-slug/users/',
  47. body: [],
  48. });
  49. });
  50. afterEach(() => {
  51. MockApiClient.clearMockResponses();
  52. GroupStore.reset();
  53. TeamStore.reset();
  54. MemberListStore.state = [];
  55. MemberListStore.loaded = false;
  56. });
  57. const openMenu = async () => {
  58. userEvent.click(await screen.findByTestId('assignee-selector'), undefined, {
  59. // Skip hover to prevent tooltip from rendering
  60. skipHover: true,
  61. });
  62. };
  63. it('renders unassigned', () => {
  64. render(<AssignedTo projectId={project.id} group={GROUP_1} />, {organization});
  65. expect(screen.getByText('No-one')).toBeInTheDocument();
  66. });
  67. it('can assign team', async () => {
  68. const assignMock = MockApiClient.addMockResponse({
  69. method: 'PUT',
  70. url: `/issues/${GROUP_1.id}/`,
  71. body: {
  72. ...GROUP_1,
  73. assignedTo: {...TEAM_1, type: 'team'},
  74. },
  75. });
  76. render(<AssignedTo projectId={project.id} group={GROUP_1} />, {organization});
  77. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  78. await openMenu();
  79. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  80. const team1slug = `#${TEAM_1.slug}`;
  81. userEvent.click(screen.getByText(team1slug));
  82. await waitFor(() =>
  83. expect(assignMock).toHaveBeenCalledWith(
  84. '/issues/1337/',
  85. expect.objectContaining({
  86. data: {assignedTo: 'team:3', assignedBy: 'assignee_selector'},
  87. })
  88. )
  89. );
  90. expect(await screen.findByText(team1slug)).toBeInTheDocument();
  91. // TEAM_1 initials
  92. expect(screen.getByTestId('assignee-selector')).toHaveTextContent('CT');
  93. });
  94. it('successfully clears assignment', async () => {
  95. const assignMock = MockApiClient.addMockResponse({
  96. method: 'PUT',
  97. url: `/issues/${GROUP_1.id}/`,
  98. body: {
  99. ...GROUP_1,
  100. assignedTo: {...TEAM_1, type: 'team'},
  101. },
  102. });
  103. render(<AssignedTo projectId={project.id} group={GROUP_1} />, {organization});
  104. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  105. await openMenu();
  106. // Assign first item in list, which is TEAM_1
  107. userEvent.click(screen.getByText(`#${TEAM_1.slug}`));
  108. await waitFor(() =>
  109. expect(assignMock).toHaveBeenCalledWith(
  110. '/issues/1337/',
  111. expect.objectContaining({
  112. data: {assignedTo: 'team:3', assignedBy: 'assignee_selector'},
  113. })
  114. )
  115. );
  116. await openMenu();
  117. userEvent.click(screen.getByRole('button', {name: 'Clear Assignee'}));
  118. // api was called with empty string, clearing assignment
  119. await waitFor(() =>
  120. expect(assignMock).toHaveBeenLastCalledWith(
  121. '/issues/1337/',
  122. expect.objectContaining({
  123. data: {assignedTo: '', assignedBy: 'assignee_selector'},
  124. })
  125. )
  126. );
  127. });
  128. });