searchBar.spec.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {TagsFixture} from 'sentry-fixture/tags';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import TagStore from 'sentry/stores/tagStore';
  5. import type {Tag, TagValue} from 'sentry/types/group';
  6. import {IsFieldValues} from 'sentry/utils/fields';
  7. import IssueListSearchBar from 'sentry/views/issueList/searchBar';
  8. describe('IssueListSearchBar', function () {
  9. const {organization} = initializeOrg();
  10. beforeEach(function () {
  11. TagStore.reset();
  12. TagStore.loadTagsSuccess(TagsFixture());
  13. MockApiClient.addMockResponse({
  14. url: '/organizations/org-slug/recent-searches/',
  15. method: 'GET',
  16. body: [],
  17. });
  18. });
  19. afterEach(function () {
  20. MockApiClient.clearMockResponses();
  21. });
  22. describe('Tags and Fields', function () {
  23. const defaultProps = {
  24. organization,
  25. query: '',
  26. statsPeriod: '7d',
  27. onSearch: jest.fn(),
  28. };
  29. it('displays the correct options for the is tag', async function () {
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/tags/',
  32. body: [],
  33. });
  34. render(<IssueListSearchBar {...defaultProps} />);
  35. await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
  36. await userEvent.paste('is:', {delay: null});
  37. await userEvent.click(
  38. await screen.findByRole('button', {name: 'Edit value for filter: is'})
  39. );
  40. Object.values(IsFieldValues).forEach(value => {
  41. expect(screen.getByRole('option', {name: value})).toBeInTheDocument();
  42. });
  43. });
  44. it('displays the correct options under Event Tags', async function () {
  45. MockApiClient.addMockResponse({
  46. url: '/organizations/org-slug/tags/',
  47. body: [{key: 'someTag', name: 'Some Tag'}],
  48. });
  49. render(<IssueListSearchBar {...defaultProps} />);
  50. await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
  51. await userEvent.click(await screen.findByRole('button', {name: 'Event Tags'}));
  52. expect(await screen.findByRole('option', {name: 'someTag'})).toBeInTheDocument();
  53. });
  54. it('displays tags in the has filter', async function () {
  55. MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/tags/',
  57. body: [{key: 'someTag', name: 'Some Tag'}],
  58. });
  59. render(<IssueListSearchBar {...defaultProps} />);
  60. await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
  61. await userEvent.paste('has:', {delay: null});
  62. await userEvent.click(
  63. await screen.findByRole('button', {name: 'Edit value for filter: has'})
  64. );
  65. expect(await screen.findByRole('option', {name: 'someTag'})).toBeInTheDocument();
  66. });
  67. it('displays conflicting tags', async function () {
  68. MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/tags/',
  70. body: [{key: 'message', name: 'message'}],
  71. });
  72. render(<IssueListSearchBar {...defaultProps} />);
  73. await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
  74. // Should display `message` and `tags[message]` as separate options
  75. expect(await screen.findByRole('option', {name: 'message'})).toBeInTheDocument();
  76. expect(
  77. await screen.findByRole('option', {name: 'tags[message]'})
  78. ).toBeInTheDocument();
  79. });
  80. });
  81. describe('Tag Values', function () {
  82. const newDefaultProps = {
  83. organization,
  84. query: '',
  85. statsPeriod: '7d',
  86. onSearch: jest.fn(),
  87. };
  88. it('displays the correct tag values for a key', async () => {
  89. const tagKey = 'random';
  90. const tagValue = 'randomValue';
  91. const tagValueResponse: TagValue[] = [
  92. {
  93. name: tagValue,
  94. value: tagValue,
  95. count: 1,
  96. firstSeen: '2021-01-01T00:00:00Z',
  97. lastSeen: '2021-01-01T00:00:00Z',
  98. email: 'a@sentry.io',
  99. username: 'a',
  100. id: '1',
  101. ip_address: '1',
  102. },
  103. ];
  104. const tag: Tag = {
  105. key: tagKey,
  106. name: tagKey,
  107. };
  108. MockApiClient.addMockResponse({
  109. url: '/organizations/org-slug/tags/',
  110. method: 'GET',
  111. body: [tag],
  112. });
  113. const tagValueMock = MockApiClient.addMockResponse({
  114. url: `/organizations/org-slug/tags/${tagKey}/values/`,
  115. method: 'GET',
  116. body: tagValueResponse,
  117. });
  118. render(<IssueListSearchBar {...newDefaultProps} />);
  119. await userEvent.click(screen.getByRole('combobox', {name: 'Add a search term'}));
  120. await userEvent.paste(tagKey, {delay: null});
  121. await userEvent.click(screen.getByRole('option', {name: tagKey}));
  122. expect(await screen.findByRole('option', {name: tagValue})).toBeInTheDocument();
  123. await waitFor(() => {
  124. // Expected twice since we make one request for values in events dataset
  125. // and another for values in IssuePlatform dataset.
  126. expect(tagValueMock).toHaveBeenCalledTimes(2);
  127. });
  128. });
  129. });
  130. });