issueWidgetQueriesForm.spec.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import IssueWidgetQueriesForm from 'sentry/components/dashboards/issueWidgetQueriesForm';
  4. import {Organization} from 'sentry/types/organization';
  5. import {FieldValueKind} from 'sentry/views/eventsV2/table/types';
  6. import {generateFieldOptions} from 'sentry/views/eventsV2/utils';
  7. function renderComponent(
  8. organization?: Organization,
  9. routerContext?: ReturnType<typeof initializeOrg>['routerContext']
  10. ) {
  11. const onChangeHandler = jest.fn();
  12. const testOrganization = organization || TestStubs.Organization();
  13. MockApiClient.clearMockResponses();
  14. MockApiClient.addMockResponse({
  15. url: '/organizations/org-slug/recent-searches/',
  16. body: [
  17. {
  18. id: '5718466',
  19. organizationId: '1',
  20. type: 0,
  21. query: 'assigned:#visibility level:error',
  22. lastSeen: '2021-12-01T20:46:36.972966Z',
  23. dateCreated: '2021-12-01T20:46:36.975384Z',
  24. },
  25. ],
  26. });
  27. MockApiClient.addMockResponse({
  28. url: '/organizations/org-slug/recent-searches/',
  29. method: 'POST',
  30. });
  31. const tagsMock = MockApiClient.addMockResponse({
  32. url: '/organizations/org-slug/tags/event.type/values/',
  33. method: 'GET',
  34. body: [
  35. {
  36. key: 'event.type',
  37. name: 'default',
  38. value: 'default',
  39. count: 128467,
  40. lastSeen: '2021-12-10T16:37:00Z',
  41. firstSeen: '2021-12-09T16:37:02Z',
  42. },
  43. {
  44. key: 'event.type',
  45. name: 'error',
  46. value: 'error',
  47. count: 50257,
  48. lastSeen: '2021-12-10T16:36:51Z',
  49. firstSeen: '2021-12-09T16:37:07Z',
  50. },
  51. ],
  52. });
  53. const fieldOptions = {
  54. 'field:issue': {
  55. label: 'issue',
  56. value: {
  57. kind: FieldValueKind.FIELD,
  58. meta: {
  59. name: 'issue',
  60. dataType: 'string',
  61. },
  62. },
  63. },
  64. 'field:assignee': {
  65. label: 'assignee',
  66. value: {
  67. kind: FieldValueKind.FIELD,
  68. meta: {
  69. name: 'assignee',
  70. dataType: 'string',
  71. },
  72. },
  73. },
  74. };
  75. const container = render(
  76. <IssueWidgetQueriesForm
  77. organization={testOrganization}
  78. selection={{
  79. projects: [1],
  80. environments: ['prod'],
  81. datetime: {
  82. period: '14d',
  83. start: null,
  84. end: null,
  85. utc: false,
  86. },
  87. }}
  88. query={{
  89. conditions: 'assigned:',
  90. fields: ['issue', 'assignee'],
  91. columns: ['issue', 'assignee'],
  92. aggregates: ['issue', 'assignee'],
  93. name: '',
  94. orderby: 'date',
  95. }}
  96. onChange={onChangeHandler}
  97. fieldOptions={fieldOptions as ReturnType<typeof generateFieldOptions>}
  98. />,
  99. {context: routerContext}
  100. );
  101. return {container, organization: testOrganization, tagsMock, onChangeHandler};
  102. }
  103. describe('IssueWidgetQueriesForm', function () {
  104. const {organization, routerContext} = initializeOrg({
  105. router: {orgId: 'orgId'},
  106. } as Parameters<typeof initializeOrg>[0]);
  107. it('fetches tag values when when focused on a lhs tag condition', async function () {
  108. const {tagsMock} = renderComponent(organization, routerContext);
  109. userEvent.type(screen.getAllByText('assigned:')[1], 'event.type:');
  110. await tick();
  111. expect(tagsMock).toHaveBeenCalled();
  112. expect(screen.getByText('default')).toBeInTheDocument();
  113. expect(screen.getAllByText('error')[0]).toBeInTheDocument();
  114. });
  115. it('only calls onChange once when selecting a value from the autocomplete dropdown', async function () {
  116. const {onChangeHandler} = renderComponent(organization, routerContext);
  117. userEvent.click(screen.getAllByText('assigned:')[1]);
  118. await tick();
  119. expect(screen.getByText('Recent Searches')).toBeInTheDocument();
  120. expect(screen.getByText('#visibility')).toBeInTheDocument();
  121. userEvent.click(screen.getByText('#visibility'));
  122. await tick();
  123. expect(onChangeHandler).toHaveBeenCalledTimes(1);
  124. });
  125. it('renders Widget Query Fields selector', function () {
  126. renderComponent(organization, routerContext);
  127. expect(screen.getByText('Columns')).toBeInTheDocument();
  128. expect(screen.getByText('issue')).toBeInTheDocument();
  129. expect(screen.getByText('assignee')).toBeInTheDocument();
  130. });
  131. it('renders Widget Query Sort selector', function () {
  132. renderComponent(organization, routerContext);
  133. expect(screen.getByText('Sort by')).toBeInTheDocument();
  134. expect(screen.getByText('Last Seen')).toBeInTheDocument();
  135. });
  136. it('calls on change handler when changing sort', function () {
  137. const {onChangeHandler} = renderComponent(organization, routerContext);
  138. userEvent.click(screen.getByText('Last Seen'));
  139. userEvent.click(screen.getByText('First Seen'));
  140. expect(onChangeHandler).toHaveBeenCalledTimes(1);
  141. expect(onChangeHandler).toHaveBeenCalledWith(
  142. expect.objectContaining({orderby: 'new'})
  143. );
  144. });
  145. });