widgetQueriesForm.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import WidgetQueriesForm from 'sentry/components/dashboards/widgetQueriesForm';
  4. import {SessionField} from 'sentry/types';
  5. import {DisplayType, WidgetQuery, WidgetType} from 'sentry/views/dashboardsV2/types';
  6. import {generateFieldOptions} from 'sentry/views/eventsV2/utils';
  7. describe('WidgetQueriesForm', function () {
  8. const {organization} = initializeOrg({
  9. router: {orgId: 'orgId'},
  10. } as Parameters<typeof initializeOrg>[0]);
  11. const queries: WidgetQuery[] = [
  12. {
  13. conditions: 'event.type:',
  14. fields: ['count()', 'release'],
  15. columns: ['release'],
  16. aggregates: ['count()'],
  17. name: '',
  18. orderby: '-count()',
  19. },
  20. ];
  21. const onChangeHandler = jest.fn((_, newQuery) => {
  22. queries[0] = newQuery;
  23. });
  24. function TestComponent(props: Partial<WidgetQueriesForm['props']>) {
  25. return (
  26. <WidgetQueriesForm
  27. displayType={DisplayType.TABLE}
  28. organization={organization}
  29. selection={{
  30. projects: [1],
  31. environments: ['prod'],
  32. datetime: {
  33. period: '14d',
  34. start: null,
  35. end: null,
  36. utc: false,
  37. },
  38. }}
  39. queries={queries}
  40. onChange={onChangeHandler}
  41. fieldOptions={
  42. {
  43. 'function:count': {
  44. label: 'count()',
  45. value: {
  46. kind: 'function',
  47. meta: {
  48. name: 'count',
  49. parameters: [],
  50. },
  51. },
  52. },
  53. 'function:count_unique': {
  54. label: 'count_unique()',
  55. value: {
  56. kind: 'function',
  57. meta: {
  58. name: 'count_unique',
  59. parameters: [],
  60. },
  61. },
  62. },
  63. } as ReturnType<typeof generateFieldOptions>
  64. }
  65. canAddSearchConditions
  66. handleAddSearchConditions={jest.fn()}
  67. handleDeleteQuery={jest.fn()}
  68. widgetType={WidgetType.DISCOVER}
  69. {...props}
  70. />
  71. );
  72. }
  73. beforeEach(() => {
  74. MockApiClient.clearMockResponses();
  75. MockApiClient.addMockResponse({
  76. url: '/organizations/org-slug/recent-searches/',
  77. body: [
  78. {
  79. id: '5718466',
  80. organizationId: '1',
  81. type: 0,
  82. query: 'event.type:transaction',
  83. lastSeen: '2021-12-01T20:46:36.972966Z',
  84. dateCreated: '2021-12-01T20:46:36.975384Z',
  85. },
  86. ],
  87. });
  88. MockApiClient.addMockResponse({
  89. url: '/organizations/org-slug/recent-searches/',
  90. method: 'POST',
  91. });
  92. });
  93. it('only calls onChange once when selecting a value from the autocomplete dropdown', async function () {
  94. render(<TestComponent />);
  95. userEvent.click(screen.getByRole('textbox', {name: 'Search events'}));
  96. expect(await screen.findByText('Recent Searches')).toBeInTheDocument();
  97. userEvent.click(screen.getByText('transaction'));
  98. expect(screen.getByText('event.type:transaction')).toBeInTheDocument();
  99. expect(onChangeHandler).toHaveBeenCalledTimes(1);
  100. });
  101. it('changes orderby to the new field', function () {
  102. const {rerender} = render(<TestComponent />);
  103. userEvent.click(screen.getByText('count()'));
  104. userEvent.click(screen.getByText('count_unique()'));
  105. rerender(<TestComponent />);
  106. expect(screen.getByText('count_unique() desc')).toBeInTheDocument();
  107. });
  108. it('does not show project and environment tags in orderby', function () {
  109. const field = `sum(${SessionField.SESSION})`;
  110. render(
  111. <TestComponent
  112. widgetType={WidgetType.RELEASE}
  113. queries={[
  114. {
  115. conditions: '',
  116. fields: [field, 'release'],
  117. columns: ['release'],
  118. aggregates: [field],
  119. name: '',
  120. orderby: field,
  121. },
  122. ]}
  123. />,
  124. {organization}
  125. );
  126. userEvent.click(screen.getByText('sum(session) asc'));
  127. expect(screen.getByText('sum(session) desc')).toBeInTheDocument();
  128. expect(screen.queryByText('project asc')).not.toBeInTheDocument();
  129. expect(screen.queryByText('environment asc')).not.toBeInTheDocument();
  130. });
  131. });