savedIssueSearches.spec.tsx 8.6 KB

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