savedIssueSearches.spec.tsx 8.7 KB

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