activitySection.spec.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import {GroupFixture} from 'sentry-fixture/group';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {UserFixture} from 'sentry-fixture/user';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import * as indicators from 'sentry/actionCreators/indicator';
  11. import ConfigStore from 'sentry/stores/configStore';
  12. import GroupStore from 'sentry/stores/groupStore';
  13. import ProjectsStore from 'sentry/stores/projectsStore';
  14. import type {GroupActivity} from 'sentry/types/group';
  15. import {GroupActivityType} from 'sentry/types/group';
  16. import StreamlinedActivitySection from 'sentry/views/issueDetails/streamline/sidebar/activitySection';
  17. describe('StreamlinedActivitySection', function () {
  18. const project = ProjectFixture();
  19. const user = UserFixture();
  20. user.options.prefersIssueDetailsStreamlinedUI = true;
  21. ConfigStore.set('user', user);
  22. ProjectsStore.loadInitialData([project]);
  23. GroupStore.init();
  24. const group = GroupFixture({
  25. id: '1337',
  26. activity: [
  27. {
  28. type: GroupActivityType.NOTE,
  29. id: 'note-1',
  30. data: {text: 'Test Note'},
  31. dateCreated: '2020-01-01T00:00:00',
  32. user,
  33. project,
  34. },
  35. ],
  36. project,
  37. });
  38. GroupStore.add([group]);
  39. beforeEach(() => {
  40. jest.restoreAllMocks();
  41. MockApiClient.clearMockResponses();
  42. });
  43. it('renders the input with a comment button', async function () {
  44. const comment = 'nice work friends';
  45. const postMock = MockApiClient.addMockResponse({
  46. url: '/organizations/org-slug/issues/1337/comments/',
  47. method: 'POST',
  48. body: {
  49. id: 'note-2',
  50. user: UserFixture({id: '2'}),
  51. type: 'note',
  52. data: {text: comment},
  53. dateCreated: '2024-10-31T00:00:00.000000Z',
  54. },
  55. });
  56. render(<StreamlinedActivitySection group={group} />);
  57. const commentInput = screen.getByRole('textbox', {name: 'Add a comment'});
  58. expect(commentInput).toBeInTheDocument();
  59. expect(
  60. screen.queryByRole('button', {name: 'Submit comment'})
  61. ).not.toBeInTheDocument();
  62. await userEvent.click(commentInput);
  63. // Button appears after input is focused
  64. const submitButton = await screen.findByRole('button', {name: 'Submit comment'});
  65. expect(submitButton).toBeInTheDocument();
  66. expect(submitButton).toBeDisabled();
  67. await userEvent.type(commentInput, comment);
  68. expect(submitButton).toBeEnabled();
  69. await userEvent.click(submitButton);
  70. expect(postMock).toHaveBeenCalled();
  71. });
  72. it('allows submitting the comment field with hotkeys', async function () {
  73. const comment = 'nice work friends';
  74. const postMock = MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/issues/1337/comments/',
  76. method: 'POST',
  77. body: {
  78. id: 'note-3',
  79. user: UserFixture({id: '2'}),
  80. type: 'note',
  81. data: {text: comment},
  82. dateCreated: '2024-10-31T00:00:00.000000Z',
  83. },
  84. });
  85. render(<StreamlinedActivitySection group={group} />);
  86. const commentInput = screen.getByRole('textbox', {name: 'Add a comment'});
  87. await userEvent.type(commentInput, comment);
  88. await userEvent.keyboard('{Meta>}{Enter}{/Meta}');
  89. expect(postMock).toHaveBeenCalled();
  90. });
  91. it('renders note and allows for delete', async function () {
  92. const deleteMock = MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/issues/1337/comments/note-1/',
  94. method: 'DELETE',
  95. });
  96. render(<StreamlinedActivitySection group={group} />);
  97. renderGlobalModal();
  98. expect(await screen.findByText('Test Note')).toBeInTheDocument();
  99. expect(screen.getByRole('button', {name: 'Comment Actions'})).toBeInTheDocument();
  100. await userEvent.click(screen.getByRole('button', {name: 'Comment Actions'}));
  101. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Remove'}));
  102. expect(
  103. screen.getByText('Are you sure you want to remove this comment?')
  104. ).toBeInTheDocument();
  105. await userEvent.click(screen.getByRole('button', {name: 'Remove comment'}));
  106. expect(deleteMock).toHaveBeenCalledTimes(1);
  107. expect(screen.queryByText('Test Note')).not.toBeInTheDocument();
  108. });
  109. it('renders note and allows for edit', async function () {
  110. jest.spyOn(indicators, 'addSuccessMessage');
  111. const editGroup = GroupFixture({
  112. id: '1123',
  113. activity: [
  114. {
  115. type: GroupActivityType.NOTE,
  116. id: 'note-1',
  117. data: {text: 'Group Test'},
  118. dateCreated: '2020-01-01T00:00:00',
  119. user,
  120. project,
  121. },
  122. ],
  123. project,
  124. });
  125. const editMock = MockApiClient.addMockResponse({
  126. url: '/organizations/org-slug/issues/1123/comments/note-1/',
  127. method: 'PUT',
  128. body: {
  129. id: 'note-1',
  130. data: {text: 'Group Test Updated'},
  131. },
  132. });
  133. render(<StreamlinedActivitySection group={editGroup} />);
  134. expect(await screen.findByText('Group Test')).toBeInTheDocument();
  135. await userEvent.click(screen.getByRole('button', {name: 'Comment Actions'}));
  136. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Edit'}));
  137. await userEvent.type(screen.getByRole('textbox', {name: 'Edit comment'}), ' Updated');
  138. await userEvent.click(screen.getByRole('button', {name: 'Cancel'}));
  139. expect(editMock).not.toHaveBeenCalled();
  140. expect(await screen.findByText('Group Test')).toBeInTheDocument();
  141. await userEvent.click(screen.getByRole('button', {name: 'Comment Actions'}));
  142. await userEvent.click(screen.getByRole('menuitemradio', {name: 'Edit'}));
  143. await userEvent.type(screen.getByRole('textbox', {name: 'Edit comment'}), ' Updated');
  144. await userEvent.click(screen.getByRole('button', {name: 'Save comment'}));
  145. expect(editMock).toHaveBeenCalledTimes(1);
  146. expect(indicators.addSuccessMessage).toHaveBeenCalledWith('Comment updated');
  147. });
  148. it('renders note but does not allow for deletion if written by someone else', async function () {
  149. const updatedActivityGroup = GroupFixture({
  150. id: '1338',
  151. activity: [
  152. {
  153. type: GroupActivityType.NOTE,
  154. id: 'note-1',
  155. data: {text: 'Test Note'},
  156. dateCreated: '2020-01-01T00:00:00',
  157. user: UserFixture({id: '2'}),
  158. project,
  159. },
  160. ],
  161. project,
  162. });
  163. render(<StreamlinedActivitySection group={updatedActivityGroup} />);
  164. expect(await screen.findByText('Test Note')).toBeInTheDocument();
  165. expect(
  166. screen.queryByRole('button', {name: 'Comment Actions'})
  167. ).not.toBeInTheDocument();
  168. });
  169. it('collapses activity when there are more than 5 items', async function () {
  170. const activities: GroupActivity[] = Array.from({length: 7}, (_, index) => ({
  171. type: GroupActivityType.NOTE,
  172. id: `note-${index + 1}`,
  173. data: {text: `Test Note ${index + 1}`},
  174. dateCreated: '2020-01-01T00:00:00',
  175. user: UserFixture({id: '2'}),
  176. project,
  177. }));
  178. const updatedActivityGroup = GroupFixture({
  179. id: '1338',
  180. activity: activities,
  181. project,
  182. });
  183. render(<StreamlinedActivitySection group={updatedActivityGroup} />);
  184. expect(await screen.findByText('Test Note 1')).toBeInTheDocument();
  185. expect(await screen.findByText('Test Note 3')).toBeInTheDocument();
  186. expect(screen.queryByText('Test Note 7')).not.toBeInTheDocument();
  187. expect(await screen.findByText('View 4 more')).toBeInTheDocument();
  188. });
  189. it('does not collapse activity when rendered in the drawer', function () {
  190. const activities: GroupActivity[] = Array.from({length: 7}, (_, index) => ({
  191. type: GroupActivityType.NOTE,
  192. id: `note-${index + 1}`,
  193. data: {text: `Test Note ${index + 1}`},
  194. dateCreated: '2020-01-01T00:00:00',
  195. user: UserFixture({id: '2'}),
  196. project,
  197. }));
  198. const updatedActivityGroup = GroupFixture({
  199. id: '1338',
  200. activity: activities,
  201. project,
  202. });
  203. render(<StreamlinedActivitySection group={updatedActivityGroup} isDrawer />);
  204. for (const activity of activities) {
  205. expect(
  206. screen.getByText((activity.data as {text: string}).text)
  207. ).toBeInTheDocument();
  208. }
  209. expect(screen.queryByRole('button')).not.toBeInTheDocument();
  210. });
  211. });