actions.spec.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {makePinSearchAction} from 'sentry/components/smartSearchBar/actions';
  3. describe('SmartSearchBar', () => {
  4. describe('actions', function () {
  5. const organization = TestStubs.Organization({id: '123'});
  6. const api = new MockApiClient();
  7. let pinRequest, unpinRequest, location;
  8. beforeEach(function () {
  9. location = {
  10. pathname: `/organizations/${organization.slug}/recent-searches/`,
  11. query: {
  12. projectId: '0',
  13. },
  14. };
  15. pinRequest = MockApiClient.addMockResponse({
  16. url: `/organizations/${organization.slug}/pinned-searches/`,
  17. method: 'PUT',
  18. body: [],
  19. });
  20. unpinRequest = MockApiClient.addMockResponse({
  21. url: `/organizations/${organization.slug}/pinned-searches/`,
  22. method: 'DELETE',
  23. body: [],
  24. });
  25. MockApiClient.addMockResponse({
  26. url: `/organizations/${organization.slug}/recent-searches/`,
  27. method: 'POST',
  28. body: {},
  29. });
  30. });
  31. it('does not pin when query is empty', function () {
  32. const {makeAction} = makePinSearchAction({sort: '', location});
  33. const Action = makeAction({
  34. api,
  35. organization,
  36. query: '',
  37. savedSearchType: 0,
  38. }).Button;
  39. render(<Action />);
  40. userEvent.click(screen.getByRole('button'));
  41. expect(pinRequest).not.toHaveBeenCalled();
  42. });
  43. it('adds pins', function () {
  44. const {makeAction} = makePinSearchAction({sort: '', location});
  45. const Action = makeAction({
  46. api,
  47. organization,
  48. query: 'is:unresolved',
  49. savedSearchType: 0,
  50. }).Button;
  51. render(<Action />);
  52. userEvent.click(screen.getByRole('button'));
  53. expect(pinRequest).toHaveBeenCalled();
  54. expect(unpinRequest).not.toHaveBeenCalled();
  55. });
  56. it('removes pins', function () {
  57. const pinnedSearch = TestStubs.Search({isPinned: true});
  58. const {makeAction} = makePinSearchAction({pinnedSearch, sort: '', location});
  59. const Action = makeAction({
  60. api,
  61. organization,
  62. query: 'is:unresolved',
  63. savedSearchType: 0,
  64. }).Button;
  65. render(<Action />);
  66. userEvent.click(screen.getByRole('button'));
  67. expect(pinRequest).not.toHaveBeenCalled();
  68. expect(unpinRequest).toHaveBeenCalled();
  69. });
  70. });
  71. });