assigneeSelector.spec.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import {openInviteMembersModal} from 'sentry/actionCreators/modal';
  3. import {Client} from 'sentry/api';
  4. import AssigneeSelectorComponent, {
  5. putSessionUserFirst,
  6. } from 'sentry/components/assigneeSelector';
  7. import ConfigStore from 'sentry/stores/configStore';
  8. import GroupStore from 'sentry/stores/groupStore';
  9. import MemberListStore from 'sentry/stores/memberListStore';
  10. import ProjectsStore from 'sentry/stores/projectsStore';
  11. import TeamStore from 'sentry/stores/teamStore';
  12. jest.mock('sentry/actionCreators/modal', () => ({
  13. openInviteMembersModal: jest.fn(),
  14. }));
  15. describe('AssigneeSelector', () => {
  16. let assignMock;
  17. let assignGroup2Mock;
  18. let USER_1, USER_2, USER_3;
  19. let TEAM_1;
  20. let PROJECT_1;
  21. let GROUP_1;
  22. let GROUP_2;
  23. beforeEach(() => {
  24. USER_1 = TestStubs.User({
  25. id: '1',
  26. name: 'Jane Bloggs',
  27. email: 'janebloggs@example.com',
  28. });
  29. USER_2 = TestStubs.User({
  30. id: '2',
  31. name: 'John Smith',
  32. email: 'johnsmith@example.com',
  33. });
  34. USER_3 = TestStubs.User({
  35. id: '3',
  36. name: 'J J',
  37. email: 'jj@example.com',
  38. });
  39. TEAM_1 = TestStubs.Team({
  40. id: '3',
  41. name: 'COOL TEAM',
  42. slug: 'cool-team',
  43. });
  44. PROJECT_1 = TestStubs.Project({
  45. teams: [TEAM_1],
  46. });
  47. GROUP_1 = TestStubs.Group({
  48. id: '1337',
  49. project: {
  50. id: PROJECT_1.id,
  51. slug: PROJECT_1.slug,
  52. },
  53. });
  54. GROUP_2 = TestStubs.Group({
  55. id: '1338',
  56. project: {
  57. id: PROJECT_1.id,
  58. slug: PROJECT_1.slug,
  59. },
  60. owners: [
  61. {
  62. type: 'suspectCommit',
  63. owner: 'user:1',
  64. date_added: '',
  65. },
  66. ],
  67. });
  68. TeamStore.reset();
  69. TeamStore.setTeams([TEAM_1]);
  70. GroupStore.reset();
  71. jest.spyOn(MemberListStore, 'getAll').mockImplementation(() => null);
  72. jest.spyOn(ProjectsStore, 'getAll').mockImplementation(() => [PROJECT_1]);
  73. jest.spyOn(GroupStore, 'get').mockImplementation(() => GROUP_1);
  74. assignMock = Client.addMockResponse({
  75. method: 'PUT',
  76. url: `/issues/${GROUP_1.id}/`,
  77. body: {
  78. ...GROUP_1,
  79. assignedTo: {...USER_1, type: 'user'},
  80. },
  81. });
  82. assignGroup2Mock = Client.addMockResponse({
  83. method: 'PUT',
  84. url: `/issues/${GROUP_2.id}/`,
  85. body: {
  86. ...GROUP_2,
  87. assignedTo: {...USER_1, type: 'user'},
  88. },
  89. });
  90. MemberListStore.state = [];
  91. MemberListStore.loaded = false;
  92. });
  93. // Doesn't need to always be async, but it was easier to prevent flakes this way
  94. const openMenu = async () => {
  95. userEvent.click(await screen.findByTestId('assignee-selector'), undefined, {
  96. // Skip hover to prevent tooltip from rendering
  97. skipHover: true,
  98. });
  99. };
  100. afterEach(() => {
  101. Client.clearMockResponses();
  102. MemberListStore.teardown();
  103. GroupStore.teardown();
  104. ProjectsStore.teardown();
  105. TeamStore.teardown();
  106. });
  107. describe('render with props', () => {
  108. it('renders members from the prop when present', async () => {
  109. MemberListStore.loadInitialData([USER_1]);
  110. render(<AssigneeSelectorComponent id={GROUP_1.id} memberList={[USER_2, USER_3]} />);
  111. await openMenu();
  112. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  113. // 3 total items
  114. expect(screen.getAllByTestId('assignee-option')).toHaveLength(3);
  115. // 1 team
  116. expect(screen.getByText(`#${TEAM_1.slug}`)).toBeInTheDocument();
  117. // 2 Users
  118. expect(screen.getByText(USER_2.name)).toBeInTheDocument();
  119. expect(screen.getByText(USER_3.name)).toBeInTheDocument();
  120. });
  121. });
  122. describe('putSessionUserFirst()', () => {
  123. it('should place the session user at the top of the member list if present', () => {
  124. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  125. jest.spyOn(ConfigStore, 'get').mockImplementation(() => ({
  126. id: '2',
  127. name: 'John Smith',
  128. email: 'johnsmith@example.com',
  129. }));
  130. expect(putSessionUserFirst([USER_1, USER_2])).toEqual([USER_2, USER_1]);
  131. ConfigStore.get.mockRestore();
  132. });
  133. it("should return the same member list if the session user isn't present", () => {
  134. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  135. jest.spyOn(ConfigStore, 'get').mockImplementation(() => ({
  136. id: '555',
  137. name: 'Here Comes a New Challenger',
  138. email: 'guile@mail.us.af.mil',
  139. }));
  140. expect(putSessionUserFirst([USER_1, USER_2])).toEqual([USER_1, USER_2]);
  141. ConfigStore.get.mockRestore();
  142. });
  143. });
  144. it('should initially have loading state', async () => {
  145. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  146. await openMenu();
  147. expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
  148. });
  149. it('does not have loading state and shows member list after calling MemberListStore.loadInitialData', async () => {
  150. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  151. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  152. await openMenu();
  153. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  154. // 3 total items
  155. expect(screen.getAllByTestId('assignee-option')).toHaveLength(3);
  156. // 1 team
  157. expect(screen.getByText(`#${TEAM_1.slug}`)).toBeInTheDocument();
  158. // 2 Users including self
  159. expect(screen.getByText(`${USER_1.name} (You)`)).toBeInTheDocument();
  160. expect(screen.getByText(USER_2.name)).toBeInTheDocument();
  161. });
  162. it('does NOT update member list after initial load', async () => {
  163. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  164. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  165. await openMenu();
  166. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  167. expect(screen.getByText(`${USER_1.name} (You)`)).toBeInTheDocument();
  168. expect(screen.getByText(USER_2.name)).toBeInTheDocument();
  169. act(() => MemberListStore.loadInitialData([USER_1, USER_2, USER_3]));
  170. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  171. expect(screen.getByText(`${USER_1.name} (You)`)).toBeInTheDocument();
  172. expect(screen.getByText(USER_2.name)).toBeInTheDocument();
  173. expect(screen.queryByText(USER_3.name)).not.toBeInTheDocument();
  174. });
  175. it('successfully assigns users', async () => {
  176. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  177. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  178. await openMenu();
  179. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  180. userEvent.click(screen.getByText(`${USER_1.name} (You)`));
  181. expect(assignMock).toHaveBeenLastCalledWith(
  182. '/issues/1337/',
  183. expect.objectContaining({
  184. data: {assignedTo: 'user:1', assignedBy: 'assignee_selector'},
  185. })
  186. );
  187. expect(await screen.findByTestId('letter_avatar-avatar')).toBeInTheDocument();
  188. // USER_1 initials
  189. expect(screen.getByTestId('assignee-selector')).toHaveTextContent('JB');
  190. });
  191. it('successfully assigns teams', async () => {
  192. Client.clearMockResponses();
  193. assignMock = Client.addMockResponse({
  194. method: 'PUT',
  195. url: `/issues/${GROUP_1.id}/`,
  196. body: {
  197. ...GROUP_1,
  198. assignedTo: {...TEAM_1, type: 'team'},
  199. },
  200. });
  201. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  202. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  203. await openMenu();
  204. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  205. userEvent.click(screen.getByText(`#${TEAM_1.slug}`));
  206. await waitFor(() =>
  207. expect(assignMock).toHaveBeenCalledWith(
  208. '/issues/1337/',
  209. expect.objectContaining({
  210. data: {assignedTo: 'team:3', assignedBy: 'assignee_selector'},
  211. })
  212. )
  213. );
  214. expect(await screen.findByTestId('letter_avatar-avatar')).toBeInTheDocument();
  215. // TEAM_1 initials
  216. expect(screen.getByTestId('assignee-selector')).toHaveTextContent('CT');
  217. });
  218. it('successfully clears assignment', async () => {
  219. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  220. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  221. await openMenu();
  222. // Assign first item in list, which is TEAM_1
  223. userEvent.click(screen.getByText(`#${TEAM_1.slug}`));
  224. await waitFor(() =>
  225. expect(assignMock).toHaveBeenCalledWith(
  226. '/issues/1337/',
  227. expect.objectContaining({
  228. data: {assignedTo: 'team:3', assignedBy: 'assignee_selector'},
  229. })
  230. )
  231. );
  232. await openMenu();
  233. userEvent.click(screen.getByRole('button', {name: 'Clear Assignee'}));
  234. // api was called with empty string, clearing assignment
  235. await waitFor(() =>
  236. expect(assignMock).toHaveBeenLastCalledWith(
  237. '/issues/1337/',
  238. expect.objectContaining({
  239. data: {assignedTo: '', assignedBy: 'assignee_selector'},
  240. })
  241. )
  242. );
  243. });
  244. it('shows invite member button', async () => {
  245. MemberListStore.loadInitialData([USER_1, USER_2]);
  246. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  247. jest.spyOn(ConfigStore, 'get').mockImplementation(() => true);
  248. await openMenu();
  249. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  250. userEvent.click(await screen.findByRole('link', {name: 'Invite Member'}));
  251. expect(openInviteMembersModal).toHaveBeenCalled();
  252. ConfigStore.get.mockRestore();
  253. });
  254. it('filters user by email and selects with keyboard', async () => {
  255. render(<AssigneeSelectorComponent id={GROUP_2.id} />);
  256. act(() => MemberListStore.loadInitialData([USER_1, USER_2]));
  257. await openMenu();
  258. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  259. userEvent.type(screen.getByRole('textbox'), 'JohnSmith@example.com');
  260. // 1 total item
  261. expect(screen.getByTestId('assignee-option')).toBeInTheDocument();
  262. expect(screen.getByText(`${USER_2.name}`)).toBeInTheDocument();
  263. userEvent.keyboard('{enter}');
  264. await waitFor(() =>
  265. expect(assignGroup2Mock).toHaveBeenLastCalledWith(
  266. '/issues/1338/',
  267. expect.objectContaining({
  268. data: {assignedTo: 'user:2', assignedBy: 'assignee_selector'},
  269. })
  270. )
  271. );
  272. expect(await screen.findByTestId('letter_avatar-avatar')).toBeInTheDocument();
  273. // USER_2 initials
  274. expect(screen.getByTestId('assignee-selector')).toHaveTextContent('JB');
  275. });
  276. it('successfully shows suggested assignees', async () => {
  277. jest.spyOn(GroupStore, 'get').mockImplementation(() => GROUP_2);
  278. const onAssign = jest.fn();
  279. render(<AssigneeSelectorComponent id={GROUP_2.id} onAssign={onAssign} />);
  280. act(() => MemberListStore.loadInitialData([USER_1, USER_2, USER_3]));
  281. expect(screen.getByTestId('suggested-avatar-stack')).toBeInTheDocument();
  282. // Hover over avatar
  283. userEvent.hover(screen.getByTestId('letter_avatar-avatar'));
  284. expect(await screen.findByText('Suggestion:')).toBeInTheDocument();
  285. expect(screen.getByText('Jane Bloggs')).toBeInTheDocument();
  286. expect(screen.getByText('commit data')).toBeInTheDocument();
  287. await openMenu();
  288. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
  289. expect(await screen.findByText('Suggested')).toBeInTheDocument();
  290. const options = screen.getAllByTestId('assignee-option');
  291. // Suggested assignee initials
  292. expect(options[0]).toHaveTextContent('JB');
  293. userEvent.click(options[0]);
  294. await waitFor(() =>
  295. expect(assignGroup2Mock).toHaveBeenCalledWith(
  296. '/issues/1338/',
  297. expect.objectContaining({
  298. data: {assignedTo: 'user:1', assignedBy: 'assignee_selector'},
  299. })
  300. )
  301. );
  302. // Suggested assignees shouldn't show anymore because we assigned to the suggested actor
  303. expect(screen.queryByTestId('suggested-avatar-stack')).not.toBeInTheDocument();
  304. expect(onAssign).toHaveBeenCalledWith(
  305. 'member',
  306. expect.objectContaining({id: '1'}),
  307. expect.objectContaining({id: '1'})
  308. );
  309. });
  310. it('renders unassigned', async () => {
  311. jest.spyOn(GroupStore, 'get').mockImplementation(() => GROUP_1);
  312. render(<AssigneeSelectorComponent id={GROUP_1.id} />);
  313. userEvent.hover(screen.getByTestId('unassigned'));
  314. expect(await screen.findByText('Unassigned')).toBeInTheDocument();
  315. });
  316. });