actions.spec.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 {Action} = makePinSearchAction({sort: ''});
  33. render(
  34. <Action
  35. api={api}
  36. organization={organization}
  37. query=""
  38. savedSearchType={0}
  39. location={location}
  40. />
  41. );
  42. userEvent.click(screen.getByRole('button'));
  43. expect(pinRequest).not.toHaveBeenCalled();
  44. });
  45. it('adds pins', function () {
  46. const {Action} = makePinSearchAction({sort: ''});
  47. render(
  48. <Action
  49. api={api}
  50. organization={organization}
  51. query="is:unresolved"
  52. savedSearchType={0}
  53. location={location}
  54. />
  55. );
  56. userEvent.click(screen.getByRole('button'));
  57. expect(pinRequest).toHaveBeenCalled();
  58. expect(unpinRequest).not.toHaveBeenCalled();
  59. });
  60. it('removes pins', function () {
  61. const pinnedSearch = TestStubs.Search({isPinned: true});
  62. const {Action} = makePinSearchAction({pinnedSearch, sort: ''});
  63. render(
  64. <Action
  65. api={api}
  66. organization={organization}
  67. query="is:unresolved"
  68. savedSearchType={0}
  69. location={location}
  70. />
  71. );
  72. userEvent.click(screen.getByRole('button'));
  73. expect(pinRequest).not.toHaveBeenCalled();
  74. expect(unpinRequest).toHaveBeenCalled();
  75. });
  76. });
  77. });