savedIssueSearches.spec.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. const {container} = render(<SavedIssueSearches {...defaultProps} />);
  62. await waitFor(() => {
  63. expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
  64. });
  65. expect(container).toSnapshot();
  66. });
  67. it('hides saves searches by default past first 4', async function () {
  68. MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/searches/',
  70. body: [...new Array(6)].map((_, i) => ({
  71. ...orgSearch,
  72. name: 'Test Search',
  73. id: i,
  74. })),
  75. });
  76. render(<SavedIssueSearches {...defaultProps} />);
  77. expect(await screen.findAllByText('Test Search')).toHaveLength(4);
  78. await userEvent.click(screen.getByRole('button', {name: /show 2 more/i}));
  79. expect(screen.getAllByText('Test Search')).toHaveLength(6);
  80. });
  81. it('can select a saved search', async function () {
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/searches/',
  84. body: [recommendedSearch, orgSearch, pinnedSearch],
  85. });
  86. render(<SavedIssueSearches {...defaultProps} />);
  87. await userEvent.click(await screen.findByRole('button', {name: 'Assigned to Me'}));
  88. expect(defaultProps.onSavedSearchSelect).toHaveBeenLastCalledWith(recommendedSearch);
  89. await userEvent.click(screen.getByRole('button', {name: 'Last 4 Hours'}));
  90. expect(defaultProps.onSavedSearchSelect).toHaveBeenLastCalledWith(orgSearch);
  91. });
  92. it('does not show header when there are no org saved searches', async function () {
  93. MockApiClient.addMockResponse({
  94. url: '/organizations/org-slug/searches/',
  95. body: [recommendedSearch],
  96. });
  97. render(<SavedIssueSearches {...defaultProps} />);
  98. expect(
  99. await screen.findByText(/You don't have any saved searches/i)
  100. ).toBeInTheDocument();
  101. });
  102. it('does not show overflow menu for recommended searches', async function () {
  103. MockApiClient.addMockResponse({
  104. url: '/organizations/org-slug/searches/',
  105. body: [recommendedSearch],
  106. });
  107. render(<SavedIssueSearches {...defaultProps} />);
  108. await waitFor(() => {
  109. expect(
  110. screen.queryByRole('button', {name: /saved search options/i})
  111. ).not.toBeInTheDocument();
  112. });
  113. });
  114. it('can delete an org saved search with correct permissions', async function () {
  115. MockApiClient.addMockResponse({
  116. url: '/organizations/org-slug/searches/',
  117. body: [recommendedSearch, orgSearch, pinnedSearch],
  118. });
  119. const deleteMock = MockApiClient.addMockResponse({
  120. url: '/organizations/org-slug/searches/org-search/',
  121. method: 'DELETE',
  122. });
  123. render(<SavedIssueSearches {...defaultProps} />);
  124. renderGlobalModal();
  125. await userEvent.click(
  126. await screen.findByRole('button', {name: /saved search options/i})
  127. );
  128. await userEvent.click(screen.getByRole('menuitemradio', {name: /delete/i}));
  129. const modal = screen.getByRole('dialog');
  130. expect(within(modal).getByText(/are you sure/i)).toBeInTheDocument();
  131. await userEvent.click(within(modal).getByRole('button', {name: /confirm/i}));
  132. await waitFor(() => {
  133. expect(deleteMock).toHaveBeenCalledTimes(1);
  134. expect(screen.queryByText(orgSearch.name)).not.toBeInTheDocument();
  135. });
  136. });
  137. it('can edit an org saved search with correct permissions', async function () {
  138. MockApiClient.addMockResponse({
  139. url: '/organizations/org-slug/searches/',
  140. body: [recommendedSearch, orgSearch, pinnedSearch],
  141. });
  142. const putMock = MockApiClient.addMockResponse({
  143. url: '/organizations/org-slug/searches/org-search/',
  144. method: 'PUT',
  145. body: {
  146. ...orgSearch,
  147. name: 'new name',
  148. },
  149. });
  150. render(
  151. <Fragment>
  152. <SavedIssueSearches {...defaultProps} />
  153. <GlobalModalContainer />
  154. </Fragment>
  155. );
  156. await userEvent.click(
  157. await screen.findByRole('button', {name: /saved search options/i})
  158. );
  159. await userEvent.click(screen.getByRole('menuitemradio', {name: /edit/i}));
  160. const modal = screen.getByRole('dialog');
  161. await userEvent.clear(within(modal).getByRole('textbox', {name: /name/i}));
  162. await userEvent.type(within(modal).getByRole('textbox', {name: /name/i}), 'new name');
  163. await userEvent.click(within(modal).getByRole('button', {name: /save/i}));
  164. await waitFor(() => {
  165. expect(putMock).toHaveBeenCalledWith(
  166. expect.anything(),
  167. expect.objectContaining({
  168. data: expect.objectContaining({
  169. name: 'new name',
  170. }),
  171. })
  172. );
  173. expect(screen.getByText('new name')).toBeInTheDocument();
  174. });
  175. });
  176. it('cannot delete or edit a saved search without correct permissions', async function () {
  177. MockApiClient.addMockResponse({
  178. url: '/organizations/org-slug/searches/',
  179. body: [recommendedSearch, orgSearch, pinnedSearch],
  180. });
  181. render(
  182. <SavedIssueSearches
  183. {...defaultProps}
  184. organization={{
  185. ...organization,
  186. access: organization.access.filter(access => access !== 'org:write'),
  187. }}
  188. />
  189. );
  190. await userEvent.click(
  191. await screen.findByRole('button', {name: /saved search options/i})
  192. );
  193. expect(
  194. screen.getByText('You do not have permission to delete this search.')
  195. ).toBeInTheDocument();
  196. expect(
  197. screen.getByText('You do not have permission to edit this search.')
  198. ).toBeInTheDocument();
  199. });
  200. it('can create a new saved search', async function () {
  201. MockApiClient.addMockResponse({
  202. url: '/organizations/org-slug/searches/',
  203. body: [recommendedSearch],
  204. });
  205. const mockSave = MockApiClient.addMockResponse({
  206. url: '/organizations/org-slug/searches/',
  207. method: 'POST',
  208. body: {},
  209. });
  210. render(<SavedIssueSearches {...defaultProps} />);
  211. renderGlobalModal();
  212. await userEvent.click(await screen.findByRole('button', {name: /add saved search/i}));
  213. const modal = screen.getByRole('dialog');
  214. await userEvent.type(
  215. within(modal).getByRole('textbox', {name: /name/i}),
  216. 'new saved search'
  217. );
  218. await userEvent.click(within(modal).getByRole('button', {name: /save/i}));
  219. await waitFor(() => {
  220. expect(mockSave).toHaveBeenCalledWith(
  221. expect.anything(),
  222. expect.objectContaining({
  223. data: expect.objectContaining({
  224. name: 'new saved search',
  225. query: 'is:unresolved',
  226. }),
  227. })
  228. );
  229. });
  230. // Modal should close
  231. await waitFor(() => {
  232. expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
  233. });
  234. });
  235. });