eventSearch.spec.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {GroupFixture} from 'sentry-fixture/group';
  2. import {LocationFixture} from 'sentry-fixture/locationFixture';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {RouterFixture} from 'sentry-fixture/routerFixture';
  6. import {TagsFixture} from 'sentry-fixture/tags';
  7. import {
  8. makeAllTheProviders,
  9. render,
  10. renderHook,
  11. screen,
  12. userEvent,
  13. } from 'sentry-test/reactTestingLibrary';
  14. import OrganizationStore from 'sentry/stores/organizationStore';
  15. import {
  16. EventSearch,
  17. useEventQuery,
  18. } from 'sentry/views/issueDetails/streamline/eventSearch';
  19. const mockHandleSearch = jest.fn();
  20. describe('EventSearch', () => {
  21. const organization = OrganizationFixture();
  22. const project = ProjectFixture({
  23. environments: ['production', 'staging', 'developement'],
  24. });
  25. const group = GroupFixture({id: 'group-id'});
  26. const defaultProps = {
  27. environments: project.environments,
  28. group,
  29. handleSearch: mockHandleSearch,
  30. query: '',
  31. };
  32. const [tagKey, tagValue] = ['user.email', 'leander.rodrigues@sentry.io'];
  33. let mockTagKeyQuery: jest.Mock;
  34. beforeEach(() => {
  35. OrganizationStore.onUpdate(organization, {replace: true});
  36. MockApiClient.clearMockResponses();
  37. MockApiClient.addMockResponse({
  38. url: `/organizations/${organization.slug}/issues/${group.id}/tags/`,
  39. body: TagsFixture(),
  40. method: 'GET',
  41. });
  42. mockTagKeyQuery = MockApiClient.addMockResponse({
  43. url: `/organizations/${organization.slug}/tags/${tagKey}/values/`,
  44. body: [
  45. {
  46. key: tagKey,
  47. name: tagValue,
  48. value: tagValue,
  49. },
  50. ],
  51. method: 'GET',
  52. });
  53. });
  54. it('handles basic inputs for tags', async function () {
  55. render(<EventSearch {...defaultProps} />);
  56. const search = screen.getByRole('combobox', {name: 'Add a search term'});
  57. expect(search).toBeInTheDocument();
  58. await userEvent.type(search, `${tagKey}:`);
  59. await userEvent.keyboard(`${tagValue}{enter}{enter}`);
  60. expect(mockTagKeyQuery).toHaveBeenCalled();
  61. expect(mockHandleSearch).toHaveBeenCalledWith(
  62. `${tagKey}:${tagValue}`,
  63. expect.anything()
  64. );
  65. });
  66. it('filters issue tokens from event queries', function () {
  67. const validQuery = `${tagKey}:${tagValue} device.family:[iphone,pixel]`;
  68. const {result: onlyIssueTokens} = renderHook(() => useEventQuery({group}), {
  69. wrapper: makeAllTheProviders({
  70. organization,
  71. router: RouterFixture({
  72. location: LocationFixture({
  73. query: {query: 'is:resolved assigned:[me,#issues] issue.priority:high'},
  74. }),
  75. }),
  76. }),
  77. });
  78. expect(onlyIssueTokens.current).toBe('');
  79. const {result: combinedTokens} = renderHook(() => useEventQuery({group}), {
  80. wrapper: makeAllTheProviders({
  81. organization,
  82. router: RouterFixture({
  83. location: LocationFixture({
  84. query: {query: `is:resolved assigned:[me,#issues] ${validQuery}`},
  85. }),
  86. }),
  87. }),
  88. });
  89. expect(combinedTokens.current).toBe(validQuery);
  90. const {result: onlyEventTokens} = renderHook(() => useEventQuery({group}), {
  91. wrapper: makeAllTheProviders({
  92. organization,
  93. router: RouterFixture({
  94. location: LocationFixture({
  95. query: {query: validQuery},
  96. }),
  97. }),
  98. }),
  99. });
  100. expect(onlyEventTokens.current).toBe(validQuery);
  101. const {result: unrecognizedFilterKey} = renderHook(() => useEventQuery({group}), {
  102. wrapper: makeAllTheProviders({
  103. organization,
  104. router: RouterFixture({
  105. location: LocationFixture({
  106. // This isn't in the TagsFixture or ISSUE_EVENT_PROPERTY_FIELDS
  107. query: {query: `${validQuery} organization.slug:sentry`},
  108. }),
  109. }),
  110. }),
  111. });
  112. expect(unrecognizedFilterKey.current).toBe(validQuery);
  113. });
  114. });