assigneeSelector.spec.jsx 12 KB

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