index.spec.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {useGroupBys} from 'sentry/views/explore/hooks/useGroupBys';
  5. import {useResultMode} from 'sentry/views/explore/hooks/useResultsMode';
  6. import {useSampleFields} from 'sentry/views/explore/hooks/useSampleFields';
  7. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  8. import {useVisualizes} from 'sentry/views/explore/hooks/useVisualizes';
  9. import {ExploreToolbar} from 'sentry/views/explore/toolbar';
  10. import {ChartType} from 'sentry/views/insights/common/components/chart';
  11. import {SpanTagsProvider} from '../contexts/spanTagsContext';
  12. describe('ExploreToolbar', function () {
  13. const organization = OrganizationFixture();
  14. beforeEach(function () {
  15. // without this the `CompactSelect` component errors with a bunch of async updates
  16. jest.spyOn(console, 'error').mockImplementation();
  17. MockApiClient.addMockResponse({
  18. url: `/organizations/${organization.slug}/spans/fields/`,
  19. method: 'GET',
  20. body: [],
  21. });
  22. });
  23. it('allows changing results mode', async function () {
  24. let resultMode, sampleFields, groupBys;
  25. function Component() {
  26. [resultMode] = useResultMode();
  27. [sampleFields] = useSampleFields();
  28. ({groupBys} = useGroupBys());
  29. return <ExploreToolbar />;
  30. }
  31. render(
  32. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  33. <Component />
  34. </SpanTagsProvider>,
  35. {disableRouterMocks: true}
  36. );
  37. const section = screen.getByTestId('section-result-mode');
  38. const samples = within(section).getByRole('radio', {name: 'Samples'});
  39. const aggregates = within(section).getByRole('radio', {name: 'Aggregates'});
  40. expect(samples).toBeChecked();
  41. expect(aggregates).not.toBeChecked();
  42. expect(resultMode).toEqual('samples');
  43. expect(sampleFields).toEqual([
  44. 'span_id',
  45. 'project',
  46. 'span.op',
  47. 'span.description',
  48. 'span.duration',
  49. 'timestamp',
  50. ]); // default
  51. await userEvent.click(aggregates);
  52. expect(samples).not.toBeChecked();
  53. expect(aggregates).toBeChecked();
  54. expect(resultMode).toEqual('aggregate');
  55. // Add a group by, and leave one unselected
  56. const groupBy = screen.getByTestId('section-group-by');
  57. await userEvent.click(within(groupBy).getByRole('button', {name: 'None'}));
  58. await userEvent.click(within(groupBy).getByRole('option', {name: 'release'}));
  59. expect(groupBys).toEqual(['release']);
  60. await userEvent.click(within(groupBy).getByRole('button', {name: 'Add Group'}));
  61. expect(groupBys).toEqual(['release', '']);
  62. await userEvent.click(samples);
  63. expect(samples).toBeChecked();
  64. expect(aggregates).not.toBeChecked();
  65. expect(resultMode).toEqual('samples');
  66. expect(sampleFields).toEqual([
  67. 'span_id',
  68. 'project',
  69. 'span.op',
  70. 'span.description',
  71. 'span.duration',
  72. 'timestamp',
  73. 'release',
  74. ]);
  75. });
  76. it('allows changing visualizes', async function () {
  77. let visualizes;
  78. function Component() {
  79. [visualizes] = useVisualizes();
  80. return <ExploreToolbar />;
  81. }
  82. render(
  83. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  84. <Component />
  85. </SpanTagsProvider>,
  86. {disableRouterMocks: true}
  87. );
  88. const section = screen.getByTestId('section-visualizes');
  89. // this is the default
  90. expect(visualizes).toEqual([
  91. {yAxes: ['count(span.duration)'], chartType: ChartType.LINE},
  92. ]);
  93. // try changing the field
  94. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  95. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  96. expect(visualizes).toEqual([
  97. {yAxes: ['count(span.self_time)'], chartType: ChartType.LINE},
  98. ]);
  99. // try changing the aggregate
  100. await userEvent.click(within(section).getByRole('button', {name: 'count'}));
  101. await userEvent.click(within(section).getByRole('option', {name: 'avg'}));
  102. expect(visualizes).toEqual([
  103. {yAxes: ['avg(span.self_time)'], chartType: ChartType.LINE},
  104. ]);
  105. // try adding an overlay
  106. await userEvent.click(within(section).getByRole('button', {name: 'Add Overlay'}));
  107. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  108. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  109. expect(visualizes).toEqual([
  110. {
  111. yAxes: ['avg(span.self_time)', 'count(span.self_time)'],
  112. chartType: ChartType.LINE,
  113. },
  114. ]);
  115. // try adding a new chart
  116. await userEvent.click(within(section).getByRole('button', {name: 'Add Chart'}));
  117. expect(visualizes).toEqual([
  118. {
  119. yAxes: ['avg(span.self_time)', 'count(span.self_time)'],
  120. chartType: ChartType.LINE,
  121. },
  122. {yAxes: ['count(span.duration)'], chartType: ChartType.LINE},
  123. ]);
  124. // delete first overlay
  125. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[0]);
  126. expect(visualizes).toEqual([
  127. {yAxes: ['count(span.self_time)'], chartType: ChartType.LINE},
  128. {yAxes: ['count(span.duration)'], chartType: ChartType.LINE},
  129. ]);
  130. // delete second chart
  131. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[1]);
  132. expect(visualizes).toEqual([
  133. {yAxes: ['count(span.self_time)'], chartType: ChartType.LINE},
  134. ]);
  135. // only one left so cant be deleted
  136. expect(within(section).getByLabelText('Remove Overlay')).toBeDisabled();
  137. });
  138. it('allows changing sort by', async function () {
  139. let sorts;
  140. function Component() {
  141. const [sampleFields] = useSampleFields();
  142. [sorts] = useSorts({fields: sampleFields});
  143. return <ExploreToolbar />;
  144. }
  145. render(
  146. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  147. <Component />
  148. </SpanTagsProvider>,
  149. {disableRouterMocks: true}
  150. );
  151. const section = screen.getByTestId('section-sort-by');
  152. // this is the default
  153. expect(within(section).getByRole('button', {name: 'timestamp'})).toBeInTheDocument();
  154. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  155. expect(sorts).toEqual([{field: 'timestamp', kind: 'desc'}]);
  156. // check the default field options
  157. const fields = [
  158. 'span_id',
  159. 'project',
  160. 'span.op',
  161. 'span.description',
  162. 'span.duration',
  163. 'timestamp',
  164. ];
  165. await userEvent.click(within(section).getByRole('button', {name: 'timestamp'}));
  166. const fieldOptions = await within(section).findAllByRole('option');
  167. expect(fieldOptions).toHaveLength(fields.length);
  168. fieldOptions.forEach((option, i) => {
  169. expect(option).toHaveTextContent(fields[i]);
  170. });
  171. // try changing the field
  172. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  173. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  174. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  175. expect(sorts).toEqual([{field: 'span.op', kind: 'desc'}]);
  176. // check the kind options
  177. await userEvent.click(within(section).getByRole('button', {name: 'Desc'}));
  178. const kindOptions = await within(section).findAllByRole('option');
  179. expect(kindOptions).toHaveLength(2);
  180. expect(kindOptions[0]).toHaveTextContent('Desc');
  181. expect(kindOptions[1]).toHaveTextContent('Asc');
  182. // try changing the kind
  183. await userEvent.click(within(section).getByRole('option', {name: 'Asc'}));
  184. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  185. expect(within(section).getByRole('button', {name: 'Asc'})).toBeInTheDocument();
  186. expect(sorts).toEqual([{field: 'span.op', kind: 'asc'}]);
  187. });
  188. it('allows changing group bys', async function () {
  189. let groupBys;
  190. function Component() {
  191. ({groupBys} = useGroupBys());
  192. return <ExploreToolbar />;
  193. }
  194. render(
  195. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  196. <Component />
  197. </SpanTagsProvider>,
  198. {disableRouterMocks: true}
  199. );
  200. const section = screen.getByTestId('section-group-by');
  201. expect(within(section).getByRole('button', {name: 'None'})).toBeInTheDocument();
  202. expect(groupBys).toEqual(['']);
  203. // disabled in the samples mode
  204. expect(within(section).getByRole('button', {name: 'None'})).toBeDisabled();
  205. // click the aggregates mode to enable
  206. await userEvent.click(
  207. within(screen.getByTestId('section-result-mode')).getByRole('radio', {
  208. name: 'Aggregates',
  209. })
  210. );
  211. expect(within(section).getByRole('button', {name: 'None'})).toBeEnabled();
  212. await userEvent.click(within(section).getByRole('button', {name: 'None'}));
  213. const groupByOptions1 = await within(section).findAllByRole('option');
  214. expect(groupByOptions1.length).toBeGreaterThan(0);
  215. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  216. expect(groupBys).toEqual(['span.op']);
  217. await userEvent.click(within(section).getByRole('button', {name: 'Add Group'}));
  218. expect(groupBys).toEqual(['span.op', '']);
  219. await userEvent.click(within(section).getByRole('button', {name: 'None'}));
  220. const groupByOptions2 = await within(section).findAllByRole('option');
  221. expect(groupByOptions2.length).toBeGreaterThan(0);
  222. await userEvent.click(
  223. within(section).getByRole('option', {name: 'span.description'})
  224. );
  225. expect(groupBys).toEqual(['span.op', 'span.description']);
  226. await userEvent.click(within(section).getAllByLabelText('Remove Column')[0]);
  227. expect(groupBys).toEqual(['span.description']);
  228. // only 1 left but it's not empty
  229. expect(within(section).getByLabelText('Remove Column')).toBeEnabled();
  230. await userEvent.click(within(section).getByLabelText('Remove Column'));
  231. expect(groupBys).toEqual(['']);
  232. // last one and it's empty
  233. expect(within(section).getByLabelText('Remove Column')).toBeDisabled();
  234. });
  235. });