index.spec.tsx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {RouterFixture} from 'sentry-fixture/routerFixture';
  4. import {
  5. render,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. within,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
  12. import ProjectsStore from 'sentry/stores/projectsStore';
  13. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  14. import {
  15. PageParamsProvider,
  16. useExploreDataset,
  17. useExploreFields,
  18. useExploreGroupBys,
  19. useExploreMode,
  20. useExplorePageParams,
  21. useExploreSortBys,
  22. useExploreVisualizes,
  23. } from 'sentry/views/explore/contexts/pageParamsContext';
  24. import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
  25. import {ExploreToolbar} from 'sentry/views/explore/toolbar';
  26. import {ChartType} from 'sentry/views/insights/common/components/chart';
  27. import {SpanTagsProvider} from '../contexts/spanTagsContext';
  28. jest.mock('sentry/actionCreators/modal');
  29. describe('ExploreToolbar', function () {
  30. const organization = OrganizationFixture({
  31. features: ['alerts-eap', 'dashboards-eap', 'dashboards-edit'],
  32. });
  33. beforeEach(function () {
  34. // without this the `CompactSelect` component errors with a bunch of async updates
  35. jest.spyOn(console, 'error').mockImplementation();
  36. const project = ProjectFixture({
  37. id: '1',
  38. slug: 'proj-slug',
  39. organization,
  40. });
  41. ProjectsStore.loadInitialData([project]);
  42. MockApiClient.addMockResponse({
  43. url: `/organizations/${organization.slug}/spans/fields/`,
  44. method: 'GET',
  45. body: [],
  46. });
  47. });
  48. it('should not render dataset selector', function () {
  49. function Component() {
  50. return <ExploreToolbar />;
  51. }
  52. render(
  53. <PageParamsProvider>
  54. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  55. <Component />
  56. </SpanTagsProvider>
  57. </PageParamsProvider>,
  58. {disableRouterMocks: true}
  59. );
  60. const section = screen.queryByTestId('section-dataset');
  61. expect(section).not.toBeInTheDocument();
  62. });
  63. it('allows changing datasets', async function () {
  64. let dataset: any;
  65. function Component() {
  66. dataset = useExploreDataset();
  67. return <ExploreToolbar extras={['dataset toggle']} />;
  68. }
  69. render(
  70. <PageParamsProvider>
  71. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  72. <Component />
  73. </SpanTagsProvider>
  74. </PageParamsProvider>,
  75. {disableRouterMocks: true}
  76. );
  77. const section = screen.getByTestId('section-dataset');
  78. const eapSpans = within(section).getByRole('radio', {name: 'EAP Spans'});
  79. const rpcSpans = within(section).getByRole('radio', {name: 'EAP RPC Spans'});
  80. const indexedSpans = within(section).getByRole('radio', {name: 'Indexed Spans'});
  81. expect(eapSpans).toBeChecked();
  82. expect(rpcSpans).not.toBeChecked();
  83. expect(indexedSpans).not.toBeChecked();
  84. expect(dataset).toEqual(DiscoverDatasets.SPANS_EAP);
  85. await userEvent.click(rpcSpans);
  86. expect(eapSpans).not.toBeChecked();
  87. expect(rpcSpans).toBeChecked();
  88. expect(indexedSpans).not.toBeChecked();
  89. expect(dataset).toEqual(DiscoverDatasets.SPANS_EAP_RPC);
  90. await userEvent.click(indexedSpans);
  91. expect(eapSpans).not.toBeChecked();
  92. expect(rpcSpans).not.toBeChecked();
  93. expect(indexedSpans).toBeChecked();
  94. expect(dataset).toEqual(DiscoverDatasets.SPANS_INDEXED);
  95. });
  96. it('allows changing mode', async function () {
  97. let mode: any;
  98. function Component() {
  99. mode = useExploreMode();
  100. return <ExploreToolbar extras={['dataset toggle']} />;
  101. }
  102. render(
  103. <PageParamsProvider>
  104. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  105. <Component />
  106. </SpanTagsProvider>
  107. </PageParamsProvider>,
  108. {disableRouterMocks: true}
  109. );
  110. const section = screen.getByTestId('section-mode');
  111. const samples = within(section).getByRole('radio', {name: 'Samples'});
  112. const aggregates = within(section).getByRole('radio', {name: 'Aggregates'});
  113. expect(samples).toBeChecked();
  114. expect(aggregates).not.toBeChecked();
  115. expect(mode).toEqual(Mode.SAMPLES);
  116. await userEvent.click(aggregates);
  117. expect(samples).not.toBeChecked();
  118. expect(aggregates).toBeChecked();
  119. expect(mode).toEqual(Mode.AGGREGATE);
  120. await userEvent.click(samples);
  121. expect(samples).toBeChecked();
  122. expect(aggregates).not.toBeChecked();
  123. expect(mode).toEqual(Mode.SAMPLES);
  124. });
  125. it('inserts group bys from aggregate mode as fields in samples mode', async function () {
  126. let fields, groupBys;
  127. function Component() {
  128. fields = useExploreFields();
  129. groupBys = useExploreGroupBys();
  130. return <ExploreToolbar extras={['dataset toggle']} />;
  131. }
  132. render(
  133. <PageParamsProvider>
  134. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  135. <Component />
  136. </SpanTagsProvider>
  137. </PageParamsProvider>,
  138. {disableRouterMocks: true}
  139. );
  140. const section = screen.getByTestId('section-mode');
  141. const samples = within(section).getByRole('radio', {name: 'Samples'});
  142. const aggregates = within(section).getByRole('radio', {name: 'Aggregates'});
  143. expect(fields).toEqual([
  144. 'id',
  145. 'span.op',
  146. 'span.description',
  147. 'span.duration',
  148. 'transaction',
  149. 'timestamp',
  150. ]); // default
  151. // Add a group by, and leave one unselected
  152. await userEvent.click(aggregates);
  153. const groupBy = screen.getByTestId('section-group-by');
  154. await userEvent.click(within(groupBy).getByRole('button', {name: 'span.op'}));
  155. await userEvent.click(within(groupBy).getByRole('option', {name: 'release'}));
  156. expect(groupBys).toEqual(['release']);
  157. await userEvent.click(within(groupBy).getByRole('button', {name: 'Add Group'}));
  158. expect(groupBys).toEqual(['release', '']);
  159. await userEvent.click(samples);
  160. expect(fields).toEqual([
  161. 'id',
  162. 'span.op',
  163. 'span.description',
  164. 'span.duration',
  165. 'transaction',
  166. 'timestamp',
  167. 'release',
  168. ]); // default
  169. });
  170. it('allows changing visualizes', async function () {
  171. let visualizes: any;
  172. function Component() {
  173. visualizes = useExploreVisualizes();
  174. return <ExploreToolbar />;
  175. }
  176. render(
  177. <PageParamsProvider>
  178. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  179. <Component />
  180. </SpanTagsProvider>
  181. </PageParamsProvider>,
  182. {disableRouterMocks: true}
  183. );
  184. const section = screen.getByTestId('section-visualizes');
  185. // this is the default
  186. expect(visualizes).toEqual([
  187. {
  188. chartType: ChartType.LINE,
  189. label: 'A',
  190. yAxes: ['avg(span.duration)'],
  191. },
  192. ]);
  193. // try changing the field
  194. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  195. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  196. expect(visualizes).toEqual([
  197. {
  198. chartType: ChartType.LINE,
  199. label: 'A',
  200. yAxes: ['avg(span.self_time)'],
  201. },
  202. ]);
  203. // try changing the aggregate
  204. await userEvent.click(within(section).getByRole('button', {name: 'avg'}));
  205. await userEvent.click(within(section).getByRole('option', {name: 'count'}));
  206. expect(visualizes).toEqual([
  207. {
  208. chartType: ChartType.LINE,
  209. label: 'A',
  210. yAxes: ['count(span.self_time)'],
  211. },
  212. ]);
  213. // try adding an overlay
  214. await userEvent.click(within(section).getByRole('button', {name: 'Add Series'}));
  215. await userEvent.click(within(section).getByRole('button', {name: 'span.duration'}));
  216. await userEvent.click(within(section).getByRole('option', {name: 'span.self_time'}));
  217. expect(visualizes).toEqual([
  218. {
  219. chartType: ChartType.LINE,
  220. label: 'A',
  221. yAxes: ['count(span.self_time)', 'avg(span.self_time)'],
  222. },
  223. ]);
  224. // try adding a new chart
  225. await userEvent.click(within(section).getByRole('button', {name: 'Add Chart'}));
  226. expect(visualizes).toEqual([
  227. {
  228. chartType: ChartType.LINE,
  229. label: 'A',
  230. yAxes: ['count(span.self_time)', 'avg(span.self_time)'],
  231. },
  232. {
  233. chartType: ChartType.LINE,
  234. label: 'B',
  235. yAxes: ['avg(span.duration)'],
  236. },
  237. ]);
  238. // delete first overlay
  239. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[0]!);
  240. expect(visualizes).toEqual([
  241. {
  242. chartType: ChartType.LINE,
  243. label: 'A',
  244. yAxes: ['avg(span.self_time)'],
  245. },
  246. {
  247. chartType: ChartType.LINE,
  248. label: 'B',
  249. yAxes: ['avg(span.duration)'],
  250. },
  251. ]);
  252. // delete second chart
  253. await userEvent.click(within(section).getAllByLabelText('Remove Overlay')[1]!);
  254. expect(visualizes).toEqual([
  255. {
  256. chartType: ChartType.LINE,
  257. label: 'A',
  258. yAxes: ['avg(span.self_time)'],
  259. },
  260. ]);
  261. // only one left so cant be deleted
  262. expect(within(section).getByLabelText('Remove Overlay')).toBeDisabled();
  263. });
  264. it('allows changing group bys', async function () {
  265. let groupBys: any;
  266. function Component() {
  267. groupBys = useExploreGroupBys();
  268. return <ExploreToolbar />;
  269. }
  270. render(
  271. <PageParamsProvider>
  272. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  273. <Component />
  274. </SpanTagsProvider>
  275. </PageParamsProvider>,
  276. {disableRouterMocks: true}
  277. );
  278. expect(screen.queryByTestId('section-group-by')).not.toBeInTheDocument();
  279. // click the aggregates mode to enable
  280. await userEvent.click(
  281. within(screen.getByTestId('section-mode')).getByRole('radio', {
  282. name: 'Aggregates',
  283. })
  284. );
  285. const section = screen.getByTestId('section-group-by');
  286. expect(within(section).getByRole('button', {name: 'span.op'})).toBeEnabled();
  287. await userEvent.click(within(section).getByRole('button', {name: 'span.op'}));
  288. const groupByOptions1 = await within(section).findAllByRole('option');
  289. expect(groupByOptions1.length).toBeGreaterThan(0);
  290. await userEvent.click(within(section).getByRole('option', {name: 'project'}));
  291. expect(groupBys).toEqual(['project']);
  292. await userEvent.click(within(section).getByRole('button', {name: 'Add Group'}));
  293. expect(groupBys).toEqual(['project', '']);
  294. await userEvent.click(within(section).getByRole('button', {name: 'None'}));
  295. const groupByOptions2 = await within(section).findAllByRole('option');
  296. expect(groupByOptions2.length).toBeGreaterThan(0);
  297. await userEvent.click(
  298. within(section).getByRole('option', {name: 'span.description'})
  299. );
  300. expect(groupBys).toEqual(['project', 'span.description']);
  301. await userEvent.click(within(section).getAllByLabelText('Remove Column')[0]!);
  302. expect(groupBys).toEqual(['span.description']);
  303. // only 1 left but it's not empty
  304. expect(within(section).getByLabelText('Remove Column')).toBeEnabled();
  305. await userEvent.click(within(section).getByLabelText('Remove Column'));
  306. expect(groupBys).toEqual(['']);
  307. // last one and it's empty
  308. expect(within(section).getByLabelText('Remove Column')).toBeDisabled();
  309. });
  310. it('allows changing sort by', async function () {
  311. let sortBys: any;
  312. function Component() {
  313. sortBys = useExploreSortBys();
  314. return <ExploreToolbar />;
  315. }
  316. render(
  317. <PageParamsProvider>
  318. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  319. <Component />
  320. </SpanTagsProvider>
  321. </PageParamsProvider>,
  322. {disableRouterMocks: true}
  323. );
  324. const section = screen.getByTestId('section-sort-by');
  325. // this is the default
  326. expect(within(section).getByRole('button', {name: 'timestamp'})).toBeInTheDocument();
  327. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  328. expect(sortBys).toEqual([{field: 'timestamp', kind: 'desc'}]);
  329. // check the default field options
  330. const fields = [
  331. 'id',
  332. 'span.description',
  333. 'span.duration',
  334. 'span.op',
  335. 'timestamp',
  336. 'transaction',
  337. ];
  338. await userEvent.click(within(section).getByRole('button', {name: 'timestamp'}));
  339. const fieldOptions = await within(section).findAllByRole('option');
  340. expect(fieldOptions).toHaveLength(fields.length);
  341. fieldOptions.forEach((option, i) => {
  342. expect(option).toHaveTextContent(fields[i]!);
  343. });
  344. // try changing the field
  345. await userEvent.click(within(section).getByRole('option', {name: 'span.op'}));
  346. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  347. expect(within(section).getByRole('button', {name: 'Desc'})).toBeInTheDocument();
  348. expect(sortBys).toEqual([{field: 'span.op', kind: 'desc'}]);
  349. // check the kind options
  350. await userEvent.click(within(section).getByRole('button', {name: 'Desc'}));
  351. const kindOptions = await within(section).findAllByRole('option');
  352. expect(kindOptions).toHaveLength(2);
  353. expect(kindOptions[0]).toHaveTextContent('Desc');
  354. expect(kindOptions[1]).toHaveTextContent('Asc');
  355. // try changing the kind
  356. await userEvent.click(within(section).getByRole('option', {name: 'Asc'}));
  357. expect(within(section).getByRole('button', {name: 'span.op'})).toBeInTheDocument();
  358. expect(within(section).getByRole('button', {name: 'Asc'})).toBeInTheDocument();
  359. expect(sortBys).toEqual([{field: 'span.op', kind: 'asc'}]);
  360. });
  361. it('takes you to suggested query', async function () {
  362. let pageParams: any;
  363. function Component() {
  364. pageParams = useExplorePageParams();
  365. return <ExploreToolbar />;
  366. }
  367. render(
  368. <PageParamsProvider>
  369. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  370. <Component />
  371. </SpanTagsProvider>
  372. </PageParamsProvider>,
  373. {disableRouterMocks: true}
  374. );
  375. const section = screen.getByTestId('section-suggested-queries');
  376. await userEvent.click(within(section).getByText('Slowest Ops'));
  377. expect(pageParams).toEqual(
  378. expect.objectContaining({
  379. fields: [
  380. 'id',
  381. 'project',
  382. 'span.op',
  383. 'span.description',
  384. 'span.duration',
  385. 'timestamp',
  386. ],
  387. groupBys: ['span.op'],
  388. mode: Mode.AGGREGATE,
  389. query: '',
  390. sortBys: [{field: 'avg(span.duration)', kind: 'desc'}],
  391. visualizes: [
  392. {
  393. chartType: ChartType.LINE,
  394. label: 'A',
  395. yAxes: ['avg(span.duration)'],
  396. },
  397. {
  398. chartType: ChartType.LINE,
  399. label: 'B',
  400. yAxes: ['p50(span.duration)'],
  401. },
  402. ],
  403. })
  404. );
  405. });
  406. it('opens the right alert', async function () {
  407. const router = RouterFixture({
  408. location: {
  409. pathname: '/traces/',
  410. query: {
  411. visualize: encodeURIComponent('{"chartType":1,"yAxes":["avg(span.duration)"]}'),
  412. },
  413. },
  414. });
  415. function Component() {
  416. return <ExploreToolbar />;
  417. }
  418. render(
  419. <PageParamsProvider>
  420. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  421. <Component />
  422. </SpanTagsProvider>
  423. </PageParamsProvider>,
  424. {router, organization}
  425. );
  426. const section = screen.getByTestId('section-save-as');
  427. await userEvent.click(within(section).getByText(/Save as/));
  428. await userEvent.hover(within(section).getByText('An Alert for'));
  429. await userEvent.click(screen.getByText('avg(span.duration)'));
  430. expect(router.push).toHaveBeenCalledWith({
  431. pathname: '/organizations/org-slug/alerts/new/metric/',
  432. query: expect.objectContaining({
  433. aggregate: 'avg(span.duration)',
  434. dataset: 'events_analytics_platform',
  435. }),
  436. });
  437. });
  438. it('add to dashboard options correctly', async function () {
  439. const router = RouterFixture({
  440. location: {
  441. pathname: '/traces/',
  442. query: {
  443. visualize: encodeURIComponent('{"chartType":1,"yAxes":["avg(span.duration)"]}'),
  444. },
  445. },
  446. });
  447. function Component() {
  448. return <ExploreToolbar />;
  449. }
  450. render(
  451. <PageParamsProvider>
  452. <SpanTagsProvider dataset={DiscoverDatasets.SPANS_EAP} enabled>
  453. <Component />
  454. </SpanTagsProvider>
  455. </PageParamsProvider>,
  456. {router, organization}
  457. );
  458. const section = screen.getByTestId('section-save-as');
  459. await userEvent.click(within(section).getByText(/Save as/));
  460. await userEvent.click(within(section).getByText('A Dashboard widget'));
  461. await waitFor(() => {
  462. expect(openAddToDashboardModal).toHaveBeenCalledWith(
  463. expect.objectContaining({
  464. widget: expect.objectContaining({
  465. displayType: 'line',
  466. queries: [
  467. {
  468. aggregates: ['avg(span.duration)'],
  469. columns: [],
  470. conditions: '',
  471. fields: ['avg(span.duration)'],
  472. name: '',
  473. orderby: '-timestamp',
  474. },
  475. ],
  476. title: 'Custom Widget',
  477. widgetType: 'spans',
  478. }),
  479. widgetAsQueryParams: expect.objectContaining({
  480. dataset: 'spans',
  481. defaultTableColumns: [
  482. 'id',
  483. 'span.op',
  484. 'span.description',
  485. 'span.duration',
  486. 'transaction',
  487. 'timestamp',
  488. ],
  489. defaultTitle: 'Custom Widget',
  490. defaultWidgetQuery:
  491. 'name=&aggregates=avg(span.duration)&columns=&fields=avg(span.duration)&conditions=&orderby=-timestamp',
  492. displayType: 'line',
  493. end: undefined,
  494. field: [
  495. 'id',
  496. 'span.op',
  497. 'span.description',
  498. 'span.duration',
  499. 'transaction',
  500. 'timestamp',
  501. ],
  502. limit: undefined,
  503. source: 'traceExplorer',
  504. start: undefined,
  505. statsPeriod: '14d',
  506. }),
  507. })
  508. );
  509. });
  510. });
  511. });