savedIssueSearches.spec.tsx 8.6 KB

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