index.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. {
  92. chartType: ChartType.LINE,
  93. label: 'A',
  94. yAxes: ['count(span.duration)'],
  95. },
  96. ]);
  97. // try changing the field
  98. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  99. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  100. expect(visualizes).toEqual([
  101. {
  102. chartType: ChartType.LINE,
  103. label: 'A',
  104. yAxes: ['count(span.self_time)'],
  105. },
  106. ]);
  107. // try changing the aggregate
  108. await userEvent.click(within(section).getByRole('button', {name: 'count'}));
  109. await userEvent.click(within(section).getByRole('option', {name: 'avg'}));
  110. expect(visualizes).toEqual([
  111. {
  112. chartType: ChartType.LINE,
  113. label: 'A',
  114. yAxes: ['avg(span.self_time)'],
  115. },
  116. ]);
  117. // try adding an overlay
  118. await userEvent.click(within(section).getByRole('button', {name: 'Add Data'}));
  119. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  120. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  121. expect(visualizes).toEqual([
  122. {
  123. chartType: ChartType.LINE,
  124. label: 'A',
  125. yAxes: ['avg(span.self_time)', 'count(span.self_time)'],
  126. },
  127. ]);
  128. // try adding a new chart
  129. await userEvent.click(within(section).getByRole('button', {name: 'Add Chart'}));
  130. expect(visualizes).toEqual([
  131. {
  132. chartType: ChartType.LINE,
  133. label: 'A',
  134. yAxes: ['avg(span.self_time)', 'count(span.self_time)'],
  135. },
  136. {
  137. chartType: ChartType.LINE,
  138. label: 'B',
  139. yAxes: ['count(span.duration)'],
  140. },
  141. ]);
  142. // delete first overlay
  143. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[0]);
  144. expect(visualizes).toEqual([
  145. {
  146. chartType: ChartType.LINE,
  147. label: 'A',
  148. yAxes: ['count(span.self_time)'],
  149. },
  150. {
  151. chartType: ChartType.LINE,
  152. label: 'B',
  153. yAxes: ['count(span.duration)'],
  154. },
  155. ]);
  156. // delete second chart
  157. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[1]);
  158. expect(visualizes).toEqual([
  159. {
  160. chartType: ChartType.LINE,
  161. label: 'A',
  162. yAxes: ['count(span.self_time)'],
  163. },
  164. ]);
  165. // only one left so cant be deleted
  166. expect(within(section).getByLabelText('Remove Overlay')).toBeDisabled();
  167. });
  168. it('allows changing sort by', async function () {
  169. let sorts;
  170. function Component() {
  171. const [sampleFields] = useSampleFields();
  172. [sorts] = useSorts({fields: sampleFields});
  173. return <ExploreToolbar />;
  174. }
  175. render(
  176. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  177. <Component />
  178. </SpanTagsProvider>,
  179. {disableRouterMocks: true}
  180. );
  181. const section = screen.getByTestId('section-sort-by');
  182. // this is the default
  183. expect(within(section).getByRole('button', {name: 'timestamp'})).toBeInTheDocument();
  184. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  185. expect(sorts).toEqual([{field: 'timestamp', kind: 'desc'}]);
  186. // check the default field options
  187. const fields = [
  188. 'span_id',
  189. 'project',
  190. 'span.op',
  191. 'span.description',
  192. 'span.duration',
  193. 'timestamp',
  194. ];
  195. await userEvent.click(within(section).getByRole('button', {name: 'timestamp'}));
  196. const fieldOptions = await within(section).findAllByRole('option');
  197. expect(fieldOptions).toHaveLength(fields.length);
  198. fieldOptions.forEach((option, i) => {
  199. expect(option).toHaveTextContent(fields[i]);
  200. });
  201. // try changing the field
  202. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  203. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  204. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  205. expect(sorts).toEqual([{field: 'span.op', kind: 'desc'}]);
  206. // check the kind options
  207. await userEvent.click(within(section).getByRole('button', {name: 'Desc'}));
  208. const kindOptions = await within(section).findAllByRole('option');
  209. expect(kindOptions).toHaveLength(2);
  210. expect(kindOptions[0]).toHaveTextContent('Desc');
  211. expect(kindOptions[1]).toHaveTextContent('Asc');
  212. // try changing the kind
  213. await userEvent.click(within(section).getByRole('option', {name: 'Asc'}));
  214. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  215. expect(within(section).getByRole('button', {name: 'Asc'})).toBeInTheDocument();
  216. expect(sorts).toEqual([{field: 'span.op', kind: 'asc'}]);
  217. });
  218. it('allows changing group bys', async function () {
  219. let groupBys;
  220. function Component() {
  221. ({groupBys} = useGroupBys());
  222. return <ExploreToolbar />;
  223. }
  224. render(
  225. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP}>
  226. <Component />
  227. </SpanTagsProvider>,
  228. {disableRouterMocks: true}
  229. );
  230. const section = screen.getByTestId('section-group-by');
  231. expect(within(section).getByRole('button', {name: 'None'})).toBeInTheDocument();
  232. expect(groupBys).toEqual(['']);
  233. // disabled in the samples mode
  234. expect(within(section).getByRole('button', {name: 'None'})).toBeDisabled();
  235. // click the aggregates mode to enable
  236. await userEvent.click(
  237. within(screen.getByTestId('section-result-mode')).getByRole('radio', {
  238. name: 'Aggregates',
  239. })
  240. );
  241. expect(within(section).getByRole('button', {name: 'None'})).toBeEnabled();
  242. await userEvent.click(within(section).getByRole('button', {name: 'None'}));
  243. const groupByOptions1 = await within(section).findAllByRole('option');
  244. expect(groupByOptions1.length).toBeGreaterThan(0);
  245. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  246. expect(groupBys).toEqual(['span.op']);
  247. await userEvent.click(within(section).getByRole('button', {name: 'Add Group'}));
  248. expect(groupBys).toEqual(['span.op', '']);
  249. await userEvent.click(within(section).getByRole('button', {name: 'None'}));
  250. const groupByOptions2 = await within(section).findAllByRole('option');
  251. expect(groupByOptions2.length).toBeGreaterThan(0);
  252. await userEvent.click(
  253. within(section).getByRole('option', {name: 'span.description'})
  254. );
  255. expect(groupBys).toEqual(['span.op', 'span.description']);
  256. await userEvent.click(within(section).getAllByLabelText('Remove Column')[0]);
  257. expect(groupBys).toEqual(['span.description']);
  258. // only 1 left but it's not empty
  259. expect(within(section).getByLabelText('Remove Column')).toBeEnabled();
  260. await userEvent.click(within(section).getByLabelText('Remove Column'));
  261. expect(groupBys).toEqual(['']);
  262. // last one and it's empty
  263. expect(within(section).getByLabelText('Remove Column')).toBeDisabled();
  264. });
  265. });