eventSearch.spec.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(
  69. () => useEventQuery({groupId: group.id}),
  70. {
  71. wrapper: makeAllTheProviders({
  72. organization,
  73. router: RouterFixture({
  74. location: LocationFixture({
  75. query: {query: 'is:resolved assigned:[me,#issues] issue.priority:high'},
  76. }),
  77. }),
  78. }),
  79. }
  80. );
  81. expect(onlyIssueTokens.current).toBe('');
  82. const {result: combinedTokens} = renderHook(
  83. () => useEventQuery({groupId: group.id}),
  84. {
  85. wrapper: makeAllTheProviders({
  86. organization,
  87. router: RouterFixture({
  88. location: LocationFixture({
  89. query: {query: `is:resolved assigned:[me,#issues] ${validQuery}`},
  90. }),
  91. }),
  92. }),
  93. }
  94. );
  95. expect(combinedTokens.current).toBe(validQuery);
  96. const {result: onlyEventTokens} = renderHook(
  97. () => useEventQuery({groupId: group.id}),
  98. {
  99. wrapper: makeAllTheProviders({
  100. organization,
  101. router: RouterFixture({
  102. location: LocationFixture({
  103. query: {query: validQuery},
  104. }),
  105. }),
  106. }),
  107. }
  108. );
  109. expect(onlyEventTokens.current).toBe(validQuery);
  110. const {result: unrecognizedFilterKey} = renderHook(
  111. () => useEventQuery({groupId: group.id}),
  112. {
  113. wrapper: makeAllTheProviders({
  114. organization,
  115. router: RouterFixture({
  116. location: LocationFixture({
  117. // This isn't in the TagsFixture or ISSUE_EVENT_PROPERTY_FIELDS
  118. query: {query: `${validQuery} organization.slug:sentry`},
  119. }),
  120. }),
  121. }),
  122. }
  123. );
  124. expect(unrecognizedFilterKey.current).toBe(validQuery);
  125. });
  126. });