savedIssueSearches.spec.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {ComponentProps, Fragment} from 'react';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. waitFor,
  8. within,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import GlobalModalContainer from 'sentry/components/globalModal';
  11. import {SavedSearchVisibility} from 'sentry/types';
  12. import localStorageWrapper from 'sentry/utils/localStorage';
  13. import SavedIssueSearches from 'sentry/views/issueList/savedIssueSearches';
  14. import {SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY} from 'sentry/views/issueList/utils';
  15. describe('SavedIssueSearches', function () {
  16. const organization = TestStubs.Organization();
  17. const recommendedSearch = TestStubs.Search({
  18. id: 'global-search',
  19. isGlobal: true,
  20. name: 'Assigned to Me',
  21. query: 'is:unresolved assigned:me',
  22. visibility: SavedSearchVisibility.ORGANIZATION,
  23. });
  24. const userSearch = TestStubs.Search({
  25. id: 'user-search',
  26. isGlobal: false,
  27. name: 'Just Firefox',
  28. query: 'browser:firefox',
  29. visibility: SavedSearchVisibility.OWNER,
  30. });
  31. const orgSearch = TestStubs.Search({
  32. id: 'org-search',
  33. isGlobal: false,
  34. name: 'Last 4 Hours',
  35. query: 'age:-4h',
  36. visibility: SavedSearchVisibility.ORGANIZATION,
  37. });
  38. const pinnedSearch = TestStubs.Search({
  39. id: 'pinned-search',
  40. isGlobal: false,
  41. isPinned: true,
  42. name: 'My Pinned Search',
  43. query: 'age:-4h',
  44. });
  45. const defaultProps: ComponentProps<typeof SavedIssueSearches> = {
  46. organization,
  47. onSavedSearchSelect: jest.fn(),
  48. query: 'is:unresolved',
  49. sort: 'date',
  50. };
  51. beforeEach(() => {
  52. localStorageWrapper.setItem(SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY, 'true');
  53. MockApiClient.clearMockResponses();
  54. jest.restoreAllMocks();
  55. });
  56. it('displays saved searches with correct text and in correct sections', async function () {
  57. MockApiClient.addMockResponse({
  58. url: '/organizations/org-slug/searches/',
  59. body: [userSearch, recommendedSearch, orgSearch, pinnedSearch],
  60. });
  61. render(<SavedIssueSearches {...defaultProps} />);
  62. await waitFor(() => {
  63. expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
  64. });
  65. });
  66. it('hides saves searches by default past first 4', async function () {
  67. MockApiClient.addMockResponse({
  68. url: '/organizations/org-slug/searches/',
  69. body: [...new Array(6)].map((_, i) => ({
  70. ...orgSearch,
  71. name: 'Test Search',
  72. id: i,
  73. })),
  74. });
  75. render(<SavedIssueSearches {...defaultProps} />);
  76. expect(await screen.findAllByText('Test Search')).toHaveLength(4);
  77. await userEvent.click(screen.getByRole('button', {name: /show 2 more/i}));
  78. expect(screen.getAllByText('Test Search')).toHaveLength(6);
  79. });
  80. it('can select a saved search', async function () {
  81. MockApiClient.addMockResponse({
  82. url: '/organizations/org-slug/searches/',
  83. body: [recommendedSearch, orgSearch, pinnedSearch],
  84. });
  85. render(<SavedIssueSearches {...defaultProps} />);
  86. await userEvent.click(await screen.findByRole('button', {name: 'Assigned to Me'}));
  87. expect(defaultProps.onSavedSearchSelect).toHaveBeenLastCalledWith(recommendedSearch);
  88. await userEvent.click(screen.getByRole('button', {name: 'Last 4 Hours'}));
  89. expect(defaultProps.onSavedSearchSelect).toHaveBeenLastCalledWith(orgSearch);
  90. });
  91. it('does not show header when there are no org saved searches', async function () {
  92. MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/searches/',
  94. body: [recommendedSearch],
  95. });
  96. render(<SavedIssueSearches {...defaultProps} />);
  97. expect(
  98. await screen.findByText(/You don't have any saved searches/i)
  99. ).toBeInTheDocument();
  100. });
  101. it('does not show overflow menu for recommended searches', async function () {
  102. MockApiClient.addMockResponse({
  103. url: '/organizations/org-slug/searches/',
  104. body: [recommendedSearch],
  105. });
  106. render(<SavedIssueSearches {...defaultProps} />);
  107. await waitFor(() => {
  108. expect(
  109. screen.queryByRole('button', {name: /saved search options/i})
  110. ).not.toBeInTheDocument();
  111. });
  112. });
  113. it('can delete an org saved search with correct permissions', async function () {
  114. MockApiClient.addMockResponse({
  115. url: '/organizations/org-slug/searches/',
  116. body: [recommendedSearch, orgSearch, pinnedSearch],
  117. });
  118. const deleteMock = MockApiClient.addMockResponse({
  119. url: '/organizations/org-slug/searches/org-search/',
  120. method: 'DELETE',
  121. });
  122. render(<SavedIssueSearches {...defaultProps} />);
  123. renderGlobalModal();
  124. await userEvent.click(
  125. await screen.findByRole('button', {name: /saved search options/i})
  126. );
  127. await userEvent.click(screen.getByRole('menuitemradio', {name: /delete/i}));
  128. const modal = screen.getByRole('dialog');
  129. expect(within(modal).getByText(/are you sure/i)).toBeInTheDocument();
  130. await userEvent.click(within(modal).getByRole('button', {name: /confirm/i}));
  131. await waitFor(() => {
  132. expect(deleteMock).toHaveBeenCalledTimes(1);
  133. expect(screen.queryByText(orgSearch.name)).not.toBeInTheDocument();
  134. });
  135. });
  136. it('can edit an org saved search with correct permissions', async function () {
  137. MockApiClient.addMockResponse({
  138. url: '/organizations/org-slug/searches/',
  139. body: [recommendedSearch, orgSearch, pinnedSearch],
  140. });
  141. const putMock = MockApiClient.addMockResponse({
  142. url: '/organizations/org-slug/searches/org-search/',
  143. method: 'PUT',
  144. body: {
  145. ...orgSearch,
  146. name: 'new name',
  147. },
  148. });
  149. render(
  150. <Fragment>
  151. <SavedIssueSearches {...defaultProps} />
  152. <GlobalModalContainer />
  153. </Fragment>
  154. );
  155. await userEvent.click(
  156. await screen.findByRole('button', {name: /saved search options/i})
  157. );
  158. await userEvent.click(screen.getByRole('menuitemradio', {name: /edit/i}));
  159. const modal = screen.getByRole('dialog');
  160. await userEvent.clear(within(modal).getByRole('textbox', {name: /name/i}));
  161. await userEvent.type(within(modal).getByRole('textbox', {name: /name/i}), 'new name');
  162. await userEvent.click(within(modal).getByRole('button', {name: /save/i}));
  163. await waitFor(() => {
  164. expect(putMock).toHaveBeenCalledWith(
  165. expect.anything(),
  166. expect.objectContaining({
  167. data: expect.objectContaining({
  168. name: 'new name',
  169. }),
  170. })
  171. );
  172. expect(screen.getByText('new name')).toBeInTheDocument();
  173. });
  174. });
  175. it('cannot delete or edit a saved search without correct permissions', async function () {
  176. MockApiClient.addMockResponse({
  177. url: '/organizations/org-slug/searches/',
  178. body: [recommendedSearch, orgSearch, pinnedSearch],
  179. });
  180. render(
  181. <SavedIssueSearches
  182. {...defaultProps}
  183. organization={{
  184. ...organization,
  185. access: organization.access.filter(access => access !== 'org:write'),
  186. }}
  187. />
  188. );
  189. await userEvent.click(
  190. await screen.findByRole('button', {name: /saved search options/i})
  191. );
  192. expect(
  193. screen.getByText('You do not have permission to delete this search.')
  194. ).toBeInTheDocument();
  195. expect(
  196. screen.getByText('You do not have permission to edit this search.')
  197. ).toBeInTheDocument();
  198. });
  199. it('can create a new saved search', async function () {
  200. MockApiClient.addMockResponse({
  201. url: '/organizations/org-slug/searches/',
  202. body: [recommendedSearch],
  203. });
  204. const mockSave = MockApiClient.addMockResponse({
  205. url: '/organizations/org-slug/searches/',
  206. method: 'POST',
  207. body: {},
  208. });
  209. render(<SavedIssueSearches {...defaultProps} />);
  210. renderGlobalModal();
  211. await userEvent.click(await screen.findByRole('button', {name: /add saved search/i}));
  212. const modal = screen.getByRole('dialog');
  213. await userEvent.type(
  214. within(modal).getByRole('textbox', {name: /name/i}),
  215. 'new saved search'
  216. );
  217. await userEvent.click(within(modal).getByRole('button', {name: /save/i}));
  218. await waitFor(() => {
  219. expect(mockSave).toHaveBeenCalledWith(
  220. expect.anything(),
  221. expect.objectContaining({
  222. data: expect.objectContaining({
  223. name: 'new saved search',
  224. query: 'is:unresolved',
  225. }),
  226. })
  227. );
  228. });
  229. // Modal should close
  230. await waitFor(() => {
  231. expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
  232. });
  233. });
  234. });