savedIssueSearches.spec.tsx 8.5 KB

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