visualize.spec.tsx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {RouterFixture} from 'sentry-fixture/routerFixture';
  4. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  5. import type {TagCollection} from 'sentry/types/group';
  6. import useCustomMeasurements from 'sentry/utils/useCustomMeasurements';
  7. import {useNavigate} from 'sentry/utils/useNavigate';
  8. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  9. import Visualize from 'sentry/views/dashboards/widgetBuilder/components/visualize';
  10. import {WidgetBuilderProvider} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  11. import {useSpanTags} from 'sentry/views/explore/contexts/spanTagsContext';
  12. jest.mock('sentry/utils/useCustomMeasurements');
  13. jest.mock('sentry/views/explore/contexts/spanTagsContext');
  14. jest.mock('sentry/utils/useNavigate');
  15. describe('Visualize', () => {
  16. let organization!: ReturnType<typeof OrganizationFixture>;
  17. let mockNavigate!: jest.Mock;
  18. beforeEach(() => {
  19. organization = OrganizationFixture({
  20. features: ['dashboards-widget-builder-redesign', 'performance-view'],
  21. });
  22. jest.mocked(useCustomMeasurements).mockReturnValue({
  23. customMeasurements: {},
  24. });
  25. jest.mocked(useSpanTags).mockReturnValue({});
  26. mockNavigate = jest.fn();
  27. jest.mocked(useNavigate).mockReturnValue(mockNavigate);
  28. });
  29. it('renders basic aggregates correctly from the URL params', async () => {
  30. render(
  31. <WidgetBuilderProvider>
  32. <Visualize />
  33. </WidgetBuilderProvider>,
  34. {
  35. organization,
  36. router: RouterFixture({
  37. location: LocationFixture({
  38. query: {
  39. yAxis: ['p90(transaction.duration)', 'max(spans.db)'],
  40. dataset: WidgetType.TRANSACTIONS,
  41. displayType: DisplayType.LINE,
  42. },
  43. }),
  44. }),
  45. }
  46. );
  47. expect(await screen.findByText('+ Add Series')).toBeInTheDocument();
  48. const p90FieldRow = (await screen.findByText('p90')).closest(
  49. '[data-testid="field-bar"]'
  50. ) as HTMLElement;
  51. expect(p90FieldRow).toBeInTheDocument();
  52. expect(within(p90FieldRow).getByText('transaction.duration')).toBeInTheDocument();
  53. const maxFieldRow = (await screen.findByText('max')).closest(
  54. '[data-testid="field-bar"]'
  55. ) as HTMLElement;
  56. expect(maxFieldRow).toBeInTheDocument();
  57. expect(within(maxFieldRow).getByText('spans.db')).toBeInTheDocument();
  58. });
  59. it('allows adding and deleting series', async () => {
  60. render(
  61. <WidgetBuilderProvider>
  62. <Visualize />
  63. </WidgetBuilderProvider>,
  64. {
  65. organization,
  66. router: RouterFixture({
  67. location: LocationFixture({
  68. query: {
  69. yAxis: ['max(spans.db)'],
  70. dataset: WidgetType.TRANSACTIONS,
  71. displayType: DisplayType.LINE,
  72. },
  73. }),
  74. }),
  75. }
  76. );
  77. expect(await screen.findByRole('button', {name: 'Remove field'})).toBeDisabled();
  78. await userEvent.click(screen.getByRole('button', {name: 'Add Series'}));
  79. expect(screen.queryAllByRole('button', {name: 'Remove field'})[0]).toBeEnabled();
  80. await userEvent.click(screen.queryAllByRole('button', {name: 'Remove field'})[0]!);
  81. expect(screen.queryAllByRole('button', {name: 'Remove field'})[0]).toBeDisabled();
  82. });
  83. it('removes the column selection when the aggregate has no parameters', async () => {
  84. render(
  85. <WidgetBuilderProvider>
  86. <Visualize />
  87. </WidgetBuilderProvider>,
  88. {
  89. organization,
  90. router: RouterFixture({
  91. location: LocationFixture({
  92. query: {
  93. yAxis: ['max(spans.db)'],
  94. dataset: WidgetType.TRANSACTIONS,
  95. displayType: DisplayType.LINE,
  96. },
  97. }),
  98. }),
  99. }
  100. );
  101. expect(screen.getByRole('button', {name: 'Column Selection'})).toBeEnabled();
  102. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  103. await userEvent.click(screen.getByRole('option', {name: 'count'}));
  104. expect(
  105. screen.queryByRole('button', {name: 'Column Selection'})
  106. ).not.toBeInTheDocument();
  107. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toBeEnabled();
  108. });
  109. it('adds the default value for the column selection when the aggregate has parameters', async () => {
  110. render(
  111. <WidgetBuilderProvider>
  112. <Visualize />
  113. </WidgetBuilderProvider>,
  114. {
  115. organization,
  116. router: RouterFixture({
  117. location: LocationFixture({
  118. query: {
  119. yAxis: ['count()'],
  120. dataset: WidgetType.TRANSACTIONS,
  121. displayType: DisplayType.LINE,
  122. },
  123. }),
  124. }),
  125. }
  126. );
  127. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  128. await userEvent.click(screen.getByRole('option', {name: 'p95'}));
  129. expect(screen.getByRole('button', {name: 'Column Selection'})).toBeEnabled();
  130. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  131. 'transaction.duration'
  132. );
  133. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  134. 'p95'
  135. );
  136. });
  137. it('maintains the selected aggregate when the column selection is changed and there are parameters', async () => {
  138. render(
  139. <WidgetBuilderProvider>
  140. <Visualize />
  141. </WidgetBuilderProvider>,
  142. {
  143. organization,
  144. router: RouterFixture({
  145. location: LocationFixture({
  146. query: {
  147. yAxis: ['max(spans.db)'],
  148. dataset: WidgetType.TRANSACTIONS,
  149. displayType: DisplayType.LINE,
  150. },
  151. }),
  152. }),
  153. }
  154. );
  155. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  156. await userEvent.click(screen.getByRole('option', {name: 'p95'}));
  157. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  158. 'spans.db'
  159. );
  160. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  161. 'p95'
  162. );
  163. });
  164. it('allows adding equations', async () => {
  165. render(
  166. <WidgetBuilderProvider>
  167. <Visualize />
  168. </WidgetBuilderProvider>,
  169. {
  170. organization,
  171. router: RouterFixture({
  172. location: LocationFixture({
  173. query: {
  174. yAxis: ['count()'],
  175. dataset: WidgetType.TRANSACTIONS,
  176. displayType: DisplayType.LINE,
  177. },
  178. }),
  179. }),
  180. }
  181. );
  182. await userEvent.click(screen.getByRole('button', {name: 'Add Equation'}));
  183. expect(screen.getByLabelText('Equation')).toBeInTheDocument();
  184. await userEvent.click(screen.getByLabelText('Equation'));
  185. // Check the menu items
  186. const headers = screen.getAllByRole('banner');
  187. expect(headers[0]).toHaveTextContent('Fields');
  188. expect(headers[1]).toHaveTextContent('Operators');
  189. expect(screen.getByRole('listitem', {name: 'count()'})).toBeInTheDocument();
  190. // Make a selection and type in the equation
  191. await userEvent.click(screen.getByRole('listitem', {name: 'count()'}));
  192. await userEvent.type(screen.getByLabelText('Equation'), '* 2');
  193. expect(screen.getByLabelText('Equation')).toHaveValue('count() * 2');
  194. });
  195. it('renders a field without an aggregate in tables', async () => {
  196. render(
  197. <WidgetBuilderProvider>
  198. <Visualize />
  199. </WidgetBuilderProvider>,
  200. {
  201. organization,
  202. router: RouterFixture({
  203. location: LocationFixture({
  204. query: {
  205. field: ['transaction.duration'],
  206. dataset: WidgetType.TRANSACTIONS,
  207. displayType: DisplayType.TABLE,
  208. },
  209. }),
  210. }),
  211. }
  212. );
  213. expect(
  214. await screen.findByRole('button', {name: 'Column Selection'})
  215. ).toHaveTextContent('transaction.duration');
  216. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  217. 'None'
  218. );
  219. });
  220. it('allows setting a field without an aggregate in tables', async () => {
  221. render(
  222. <WidgetBuilderProvider>
  223. <Visualize />
  224. </WidgetBuilderProvider>,
  225. {
  226. organization,
  227. router: RouterFixture({
  228. location: LocationFixture({
  229. query: {
  230. field: ['count()'],
  231. dataset: WidgetType.TRANSACTIONS,
  232. displayType: DisplayType.TABLE,
  233. },
  234. }),
  235. }),
  236. }
  237. );
  238. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  239. await userEvent.click(screen.getByRole('option', {name: 'None'}));
  240. await userEvent.click(screen.getByRole('button', {name: 'Column Selection'}));
  241. await userEvent.click(screen.getByRole('option', {name: 'transaction.duration'}));
  242. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  243. 'transaction.duration'
  244. );
  245. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  246. 'None'
  247. );
  248. });
  249. it('allows setting an equation in tables', async () => {
  250. render(
  251. <WidgetBuilderProvider>
  252. <Visualize />
  253. </WidgetBuilderProvider>,
  254. {
  255. organization,
  256. router: RouterFixture({
  257. location: LocationFixture({
  258. query: {
  259. field: ['count()'],
  260. dataset: WidgetType.TRANSACTIONS,
  261. displayType: DisplayType.TABLE,
  262. },
  263. }),
  264. }),
  265. }
  266. );
  267. await userEvent.click(screen.getByRole('button', {name: 'Add Equation'}));
  268. await userEvent.click(screen.getByLabelText('Equation'));
  269. await userEvent.click(screen.getByRole('listitem', {name: 'count()'}));
  270. await userEvent.type(screen.getByLabelText('Equation'), '* 2');
  271. expect(screen.getByLabelText('Equation')).toHaveValue('count() * 2');
  272. });
  273. it('maintains the selected column when switching from field to aggregate', async () => {
  274. render(
  275. <WidgetBuilderProvider>
  276. <Visualize />
  277. </WidgetBuilderProvider>,
  278. {
  279. organization,
  280. router: RouterFixture({
  281. location: LocationFixture({
  282. query: {
  283. yAxis: ['transaction.duration'],
  284. dataset: WidgetType.TRANSACTIONS,
  285. displayType: DisplayType.LINE,
  286. },
  287. }),
  288. }),
  289. }
  290. );
  291. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  292. await userEvent.click(screen.getByRole('option', {name: 'p95'}));
  293. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  294. 'transaction.duration'
  295. );
  296. });
  297. it('maintains the selected column when switching from aggregate to aggregate', async () => {
  298. render(
  299. <WidgetBuilderProvider>
  300. <Visualize />
  301. </WidgetBuilderProvider>,
  302. {
  303. organization,
  304. router: RouterFixture({
  305. location: LocationFixture({
  306. query: {
  307. field: ['max(spans.db)'],
  308. dataset: WidgetType.TRANSACTIONS,
  309. displayType: DisplayType.TABLE,
  310. },
  311. }),
  312. }),
  313. }
  314. );
  315. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  316. await userEvent.click(screen.getByRole('option', {name: 'p95'}));
  317. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  318. 'spans.db'
  319. );
  320. });
  321. it('properly transitions between aggregates of higher to no parameter count', async () => {
  322. render(
  323. <WidgetBuilderProvider>
  324. <Visualize />
  325. </WidgetBuilderProvider>,
  326. {
  327. organization,
  328. router: RouterFixture({
  329. location: LocationFixture({
  330. query: {
  331. field: ['count_if(transaction.duration,equals,testValue)'],
  332. dataset: WidgetType.TRANSACTIONS,
  333. displayType: DisplayType.TABLE,
  334. },
  335. }),
  336. }),
  337. }
  338. );
  339. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  340. await userEvent.click(screen.getByRole('option', {name: 'count'}));
  341. expect(
  342. screen.queryByRole('button', {name: 'Column Selection'})
  343. ).not.toBeInTheDocument();
  344. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  345. 'count'
  346. );
  347. expect(mockNavigate).toHaveBeenCalledWith(
  348. expect.objectContaining({
  349. query: expect.objectContaining({
  350. field: ['count()'],
  351. }),
  352. }),
  353. {replace: true}
  354. );
  355. });
  356. it('properly transitions between aggregates of higher to lower parameter count', async () => {
  357. render(
  358. <WidgetBuilderProvider>
  359. <Visualize />
  360. </WidgetBuilderProvider>,
  361. {
  362. organization,
  363. router: RouterFixture({
  364. location: LocationFixture({
  365. query: {
  366. field: ['count_if(transaction.duration,equals,testValue)'],
  367. dataset: WidgetType.TRANSACTIONS,
  368. displayType: DisplayType.TABLE,
  369. },
  370. }),
  371. }),
  372. }
  373. );
  374. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  375. await userEvent.click(screen.getByRole('option', {name: 'count_miserable'}));
  376. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  377. 'user'
  378. );
  379. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  380. 'count_miserable'
  381. );
  382. expect(screen.getByDisplayValue('300')).toBeInTheDocument();
  383. expect(mockNavigate).toHaveBeenCalledWith(
  384. expect.objectContaining({
  385. query: expect.objectContaining({
  386. field: ['count_miserable(user,300)'],
  387. }),
  388. }),
  389. {replace: true}
  390. );
  391. });
  392. it('adds the default value for an aggregate with 2 parameters', async () => {
  393. render(
  394. <WidgetBuilderProvider>
  395. <Visualize />
  396. </WidgetBuilderProvider>,
  397. {
  398. organization,
  399. router: RouterFixture({
  400. location: LocationFixture({
  401. query: {
  402. field: ['transaction.duration'],
  403. dataset: WidgetType.TRANSACTIONS,
  404. displayType: DisplayType.TABLE,
  405. },
  406. }),
  407. }),
  408. }
  409. );
  410. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  411. await userEvent.click(screen.getByRole('option', {name: 'count_miserable'}));
  412. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  413. 'user'
  414. );
  415. });
  416. it('adds the default values for an aggregate with 3 parameters', async () => {
  417. render(
  418. <WidgetBuilderProvider>
  419. <Visualize />
  420. </WidgetBuilderProvider>,
  421. {
  422. organization,
  423. router: RouterFixture({
  424. location: LocationFixture({
  425. query: {
  426. field: ['transaction'],
  427. dataset: WidgetType.TRANSACTIONS,
  428. displayType: DisplayType.TABLE,
  429. },
  430. }),
  431. }),
  432. }
  433. );
  434. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  435. await userEvent.click(screen.getByRole('option', {name: 'count_if'}));
  436. expect(screen.getByRole('button', {name: 'Column Selection'})).toHaveTextContent(
  437. 'transaction'
  438. );
  439. expect(screen.getByRole('button', {name: 'Aggregate Selection'})).toHaveTextContent(
  440. 'count_if'
  441. );
  442. expect(screen.getByText('is equal to')).toBeInTheDocument();
  443. expect(screen.getByDisplayValue('300')).toBeInTheDocument();
  444. });
  445. it('disables the aggregate selection when there is only one aggregate option', async () => {
  446. render(
  447. <WidgetBuilderProvider>
  448. <Visualize />
  449. </WidgetBuilderProvider>,
  450. {
  451. organization,
  452. router: RouterFixture({
  453. location: LocationFixture({
  454. query: {
  455. dataset: WidgetType.ISSUE,
  456. field: ['issue.id'],
  457. displayType: DisplayType.TABLE,
  458. },
  459. }),
  460. }),
  461. }
  462. );
  463. expect(
  464. await screen.findByRole('button', {name: 'Aggregate Selection'})
  465. ).toBeDisabled();
  466. });
  467. it('does not show the legend alias input for chart widgets', async () => {
  468. render(
  469. <WidgetBuilderProvider>
  470. <Visualize />
  471. </WidgetBuilderProvider>,
  472. {
  473. organization,
  474. router: RouterFixture({
  475. location: LocationFixture({
  476. query: {
  477. dataset: WidgetType.TRANSACTIONS,
  478. displayType: DisplayType.LINE,
  479. yAxis: ['p90(transaction.duration)'],
  480. },
  481. }),
  482. }),
  483. }
  484. );
  485. expect(
  486. await screen.findByRole('button', {name: 'Column Selection'})
  487. ).toHaveTextContent('transaction.duration');
  488. expect(
  489. await screen.findByRole('button', {name: 'Aggregate Selection'})
  490. ).toHaveTextContent('p90');
  491. expect(screen.queryByLabelText('Legend Alias')).not.toBeInTheDocument();
  492. });
  493. it('does not show the legend alias input for big number widgets', async () => {
  494. render(
  495. <WidgetBuilderProvider>
  496. <Visualize />
  497. </WidgetBuilderProvider>,
  498. {
  499. organization,
  500. router: RouterFixture({
  501. location: LocationFixture({
  502. query: {
  503. dataset: WidgetType.TRANSACTIONS,
  504. displayType: DisplayType.BIG_NUMBER,
  505. field: ['count()'],
  506. },
  507. }),
  508. }),
  509. }
  510. );
  511. expect(
  512. await screen.findByRole('button', {name: 'Aggregate Selection'})
  513. ).toHaveTextContent('count');
  514. expect(screen.queryByLabelText('Legend Alias')).not.toBeInTheDocument();
  515. });
  516. it('does not allow for selecting individual fields in big number widgets', async () => {
  517. render(
  518. <WidgetBuilderProvider>
  519. <Visualize />
  520. </WidgetBuilderProvider>,
  521. {
  522. organization,
  523. router: RouterFixture({
  524. location: LocationFixture({
  525. query: {
  526. dataset: WidgetType.TRANSACTIONS,
  527. displayType: DisplayType.BIG_NUMBER,
  528. field: ['count()'],
  529. },
  530. }),
  531. }),
  532. }
  533. );
  534. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  535. // Being unable to choose "None" in the aggregate selection means that the
  536. // individual field is not allowed, i.e. only aggregates appear.
  537. expect(screen.queryByRole('option', {name: 'None'})).not.toBeInTheDocument();
  538. });
  539. it('updates only the selected field', async () => {
  540. render(
  541. <WidgetBuilderProvider>
  542. <Visualize />
  543. </WidgetBuilderProvider>,
  544. {
  545. organization,
  546. router: RouterFixture({
  547. location: LocationFixture({
  548. query: {
  549. dataset: WidgetType.TRANSACTIONS,
  550. displayType: DisplayType.TABLE,
  551. field: ['count_unique(user)'],
  552. },
  553. }),
  554. }),
  555. }
  556. );
  557. expect(await screen.findByLabelText('Aggregate Selection')).toHaveTextContent(
  558. 'count_unique'
  559. );
  560. expect(screen.getByLabelText('Column Selection')).toHaveTextContent('user');
  561. // Add 3 fields
  562. await userEvent.click(screen.getByRole('button', {name: 'Add Field'}));
  563. await userEvent.click(screen.getByRole('button', {name: 'Add Field'}));
  564. await userEvent.click(screen.getByRole('button', {name: 'Add Field'}));
  565. // count() is the default aggregate when adding a field
  566. expect(screen.getAllByText('count')).toHaveLength(3);
  567. // Change the last field
  568. await userEvent.click(screen.getAllByText('count')[2]!);
  569. await userEvent.click(screen.getByRole('option', {name: 'epm'}));
  570. // The other fields should not be affected
  571. expect(screen.getByText('count_unique')).toBeInTheDocument();
  572. expect(screen.getAllByText('count')).toHaveLength(2);
  573. expect(screen.getAllByText('epm')).toHaveLength(1);
  574. });
  575. it('shows appropriate error messages for non-chart widget queries', async () => {
  576. render(
  577. <WidgetBuilderProvider>
  578. <Visualize
  579. error={{
  580. queries: [
  581. {
  582. fields: ['this field has an error'],
  583. aggregates: ['this aggregate has an error'],
  584. },
  585. ],
  586. }}
  587. />
  588. </WidgetBuilderProvider>,
  589. {
  590. organization,
  591. router: RouterFixture({
  592. location: LocationFixture({
  593. query: {
  594. dataset: WidgetType.TRANSACTIONS,
  595. displayType: DisplayType.BIG_NUMBER,
  596. },
  597. }),
  598. }),
  599. }
  600. );
  601. expect(await screen.findByText('this field has an error')).toBeInTheDocument();
  602. expect(screen.queryByText('this aggregate has an error')).not.toBeInTheDocument();
  603. });
  604. it('shows appropriate error messages for chart type widget queries', async () => {
  605. render(
  606. <WidgetBuilderProvider>
  607. <Visualize
  608. error={{
  609. queries: [
  610. {
  611. fields: ['this field has an error'],
  612. aggregates: ['this aggregate has an error'],
  613. },
  614. ],
  615. }}
  616. />
  617. </WidgetBuilderProvider>,
  618. {
  619. organization,
  620. router: RouterFixture({
  621. location: LocationFixture({
  622. query: {
  623. dataset: WidgetType.TRANSACTIONS,
  624. displayType: DisplayType.LINE,
  625. },
  626. }),
  627. }),
  628. }
  629. );
  630. expect(await screen.findByText('this aggregate has an error')).toBeInTheDocument();
  631. expect(screen.queryByText('this field has an error')).not.toBeInTheDocument();
  632. });
  633. it('shows radio buttons for big number widgets when there are multiple aggregates', async () => {
  634. render(
  635. <WidgetBuilderProvider>
  636. <Visualize />
  637. </WidgetBuilderProvider>,
  638. {
  639. organization,
  640. router: RouterFixture({
  641. location: LocationFixture({
  642. query: {
  643. dataset: WidgetType.TRANSACTIONS,
  644. displayType: DisplayType.BIG_NUMBER,
  645. field: ['count_unique(user)'],
  646. },
  647. }),
  648. }),
  649. }
  650. );
  651. expect(await screen.findByLabelText('Aggregate Selection')).toHaveTextContent(
  652. 'count_unique'
  653. );
  654. await userEvent.click(screen.getByRole('button', {name: 'Add Field'}));
  655. // There are two radio buttons, one for each aggregate, and the last one is selected
  656. expect(screen.getAllByLabelText('aggregate-selector')).toHaveLength(2);
  657. expect(screen.getByRole('radio', {name: 'field1'})).toBeChecked();
  658. });
  659. it('shifts the selected aggregate up when it is the last one and removed', async () => {
  660. render(
  661. <WidgetBuilderProvider>
  662. <Visualize />
  663. </WidgetBuilderProvider>,
  664. {
  665. organization,
  666. router: RouterFixture({
  667. location: LocationFixture({
  668. query: {
  669. dataset: WidgetType.TRANSACTIONS,
  670. displayType: DisplayType.BIG_NUMBER,
  671. field: ['count_unique(1)', 'count_unique(2)', 'count_unique(3)'],
  672. selectedAggregate: '2',
  673. },
  674. }),
  675. }),
  676. }
  677. );
  678. expect(await screen.findByRole('radio', {name: 'field2'})).toBeChecked();
  679. await userEvent.click(screen.getAllByRole('button', {name: 'Remove field'})[2]!);
  680. // The second field is now selected, but the URL param for selectedAggregate
  681. // is cleared, so the last field is selected
  682. expect(await screen.findByRole('radio', {name: 'field1'})).toBeChecked();
  683. expect(mockNavigate).toHaveBeenCalledWith(
  684. expect.objectContaining({
  685. query: expect.objectContaining({
  686. selectedAggregate: undefined,
  687. }),
  688. }),
  689. {replace: true}
  690. );
  691. });
  692. it('only shows the relevant options for the release dataset', async () => {
  693. render(
  694. <WidgetBuilderProvider>
  695. <Visualize />
  696. </WidgetBuilderProvider>,
  697. {
  698. organization,
  699. router: RouterFixture({
  700. location: LocationFixture({
  701. query: {
  702. dataset: WidgetType.RELEASE,
  703. field: ['crash_free_rate(session)'],
  704. },
  705. }),
  706. }),
  707. }
  708. );
  709. expect(
  710. await screen.findByRole('button', {name: 'Column Selection'})
  711. ).toHaveTextContent('session');
  712. await userEvent.click(screen.getByRole('button', {name: 'Column Selection'}));
  713. const listbox = await screen.findAllByRole('option');
  714. expect(listbox).toHaveLength(2);
  715. expect(listbox[0]).toHaveTextContent('session');
  716. expect(listbox[1]).toHaveTextContent('user');
  717. });
  718. it('clears out the field when the selected aggregate has no parameters', async () => {
  719. render(
  720. <WidgetBuilderProvider>
  721. <Visualize />
  722. </WidgetBuilderProvider>,
  723. {
  724. organization,
  725. router: RouterFixture({
  726. location: LocationFixture({
  727. query: {
  728. dataset: WidgetType.TRANSACTIONS,
  729. field: ['transaction.duration'],
  730. },
  731. }),
  732. }),
  733. }
  734. );
  735. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  736. await userEvent.click(screen.getByRole('option', {name: 'count'}));
  737. expect(
  738. screen.queryByRole('button', {name: 'Column Selection'})
  739. ).not.toBeInTheDocument();
  740. });
  741. it('uses the provided value for a value parameter field', async () => {
  742. render(
  743. <WidgetBuilderProvider>
  744. <Visualize />
  745. </WidgetBuilderProvider>,
  746. {
  747. organization,
  748. router: RouterFixture({
  749. location: LocationFixture({
  750. query: {
  751. dataset: WidgetType.TRANSACTIONS,
  752. field: ['count_if(transaction.duration,equals,300)'],
  753. },
  754. }),
  755. }),
  756. }
  757. );
  758. // Simulate clearing and typing a new value from the user
  759. await userEvent.type(
  760. screen.getByDisplayValue('300'),
  761. '{backspace}{backspace}{backspace}400'
  762. );
  763. // Unfocus the field
  764. await userEvent.tab();
  765. expect(await screen.findByDisplayValue('400')).toBeInTheDocument();
  766. });
  767. it('restricts deleting the last aggregate in release health widgets', async () => {
  768. render(
  769. <WidgetBuilderProvider>
  770. <Visualize />
  771. </WidgetBuilderProvider>,
  772. {
  773. organization,
  774. router: RouterFixture({
  775. location: LocationFixture({
  776. query: {
  777. dataset: WidgetType.RELEASE,
  778. field: ['crash_free_rate(session)', 'environment'],
  779. },
  780. }),
  781. }),
  782. }
  783. );
  784. const removeButtons = await screen.findAllByRole('button', {name: 'Remove field'});
  785. expect(removeButtons).toHaveLength(2);
  786. expect(removeButtons[0]).toBeDisabled();
  787. expect(removeButtons[1]).toBeEnabled();
  788. });
  789. it('shows a text box and removes the column selection for apdex', async () => {
  790. render(
  791. <WidgetBuilderProvider>
  792. <Visualize />
  793. </WidgetBuilderProvider>,
  794. {
  795. organization,
  796. router: RouterFixture({
  797. location: LocationFixture({
  798. query: {
  799. dataset: WidgetType.TRANSACTIONS,
  800. field: ['apdex(3000)'],
  801. },
  802. }),
  803. }),
  804. }
  805. );
  806. expect(
  807. await screen.findByRole('button', {name: 'Aggregate Selection'})
  808. ).toHaveTextContent('apdex');
  809. expect(screen.getByRole('textbox', {name: 'Numeric Input'})).toHaveValue('3000');
  810. expect(
  811. screen.queryByRole('button', {name: 'Column Selection'})
  812. ).not.toBeInTheDocument();
  813. });
  814. it('resets the text box value when swapping between apdex and user_misery', async () => {
  815. render(
  816. <WidgetBuilderProvider>
  817. <Visualize />
  818. </WidgetBuilderProvider>,
  819. {
  820. organization,
  821. router: RouterFixture({
  822. location: LocationFixture({
  823. query: {
  824. dataset: WidgetType.TRANSACTIONS,
  825. field: ['apdex(9999)'],
  826. },
  827. }),
  828. }),
  829. }
  830. );
  831. await userEvent.click(screen.getByRole('button', {name: 'Aggregate Selection'}));
  832. await userEvent.click(screen.getByRole('option', {name: 'user_misery'}));
  833. expect(screen.getByRole('textbox', {name: 'Numeric Input'})).toHaveValue('300');
  834. });
  835. describe('spans', () => {
  836. beforeEach(() => {
  837. jest.mocked(useSpanTags).mockImplementation((type?: 'string' | 'number') => {
  838. if (type === 'number') {
  839. return {
  840. 'span.duration': {
  841. key: 'span.duration',
  842. name: 'span.duration',
  843. kind: 'measurement',
  844. },
  845. 'tags[anotherNumericTag,number]': {
  846. key: 'anotherNumericTag',
  847. name: 'anotherNumericTag',
  848. kind: 'measurement',
  849. },
  850. } as TagCollection;
  851. }
  852. return {
  853. 'span.description': {
  854. key: 'span.description',
  855. name: 'span.description',
  856. kind: 'tag',
  857. },
  858. } as TagCollection;
  859. });
  860. });
  861. it('shows numeric tags as primary options for chart widgets', async () => {
  862. render(
  863. <WidgetBuilderProvider>
  864. <Visualize />
  865. </WidgetBuilderProvider>,
  866. {
  867. organization,
  868. router: RouterFixture({
  869. location: LocationFixture({
  870. query: {
  871. dataset: WidgetType.SPANS,
  872. displayType: DisplayType.LINE,
  873. yAxis: ['p90(span.duration)'],
  874. },
  875. }),
  876. }),
  877. }
  878. );
  879. await userEvent.click(
  880. await screen.findByRole('button', {name: 'Column Selection'})
  881. );
  882. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  883. expect(within(listbox).getByText('anotherNumericTag')).toBeInTheDocument();
  884. expect(within(listbox).queryByText('span.description')).not.toBeInTheDocument();
  885. });
  886. it('shows the correct aggregate options', async () => {
  887. render(
  888. <WidgetBuilderProvider>
  889. <Visualize />
  890. </WidgetBuilderProvider>,
  891. {
  892. organization,
  893. router: RouterFixture({
  894. location: LocationFixture({
  895. query: {
  896. dataset: WidgetType.SPANS,
  897. displayType: DisplayType.LINE,
  898. yAxis: ['count(span.duration)'],
  899. },
  900. }),
  901. }),
  902. }
  903. );
  904. await userEvent.click(
  905. await screen.findByRole('button', {name: 'Aggregate Selection'})
  906. );
  907. // count has two entries because it's already selected
  908. // and in the dropdown
  909. expect(screen.getAllByText('count')).toHaveLength(2);
  910. expect(screen.getByText('avg')).toBeInTheDocument();
  911. expect(screen.getByText('max')).toBeInTheDocument();
  912. expect(screen.getByText('min')).toBeInTheDocument();
  913. expect(screen.getByText('p50')).toBeInTheDocument();
  914. expect(screen.getByText('p75')).toBeInTheDocument();
  915. expect(screen.getByText('p90')).toBeInTheDocument();
  916. expect(screen.getByText('p95')).toBeInTheDocument();
  917. expect(screen.getByText('p99')).toBeInTheDocument();
  918. expect(screen.getByText('p100')).toBeInTheDocument();
  919. expect(screen.getByText('sum')).toBeInTheDocument();
  920. });
  921. it('shows the correct column options for the aggregate field type', async () => {
  922. render(
  923. <WidgetBuilderProvider>
  924. <Visualize />
  925. </WidgetBuilderProvider>,
  926. {
  927. organization,
  928. router: RouterFixture({
  929. location: LocationFixture({
  930. query: {
  931. dataset: WidgetType.SPANS,
  932. displayType: DisplayType.TABLE,
  933. field: ['p90(span.duration)'],
  934. },
  935. }),
  936. }),
  937. }
  938. );
  939. await userEvent.click(
  940. await screen.findByRole('button', {name: 'Column Selection'})
  941. );
  942. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  943. expect(within(listbox).getByText('span.duration')).toBeInTheDocument();
  944. expect(within(listbox).getByText('anotherNumericTag')).toBeInTheDocument();
  945. });
  946. it('shows the correct column options for the non-aggregate field type', async () => {
  947. render(
  948. <WidgetBuilderProvider>
  949. <Visualize />
  950. </WidgetBuilderProvider>,
  951. {
  952. organization,
  953. router: RouterFixture({
  954. location: LocationFixture({
  955. query: {
  956. dataset: WidgetType.SPANS,
  957. displayType: DisplayType.TABLE,
  958. field: ['span.duration'],
  959. },
  960. }),
  961. }),
  962. }
  963. );
  964. await userEvent.click(
  965. await screen.findByRole('button', {name: 'Column Selection'})
  966. );
  967. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  968. expect(within(listbox).getByText('span.duration')).toBeInTheDocument();
  969. expect(within(listbox).getByText('span.description')).toBeInTheDocument();
  970. });
  971. });
  972. });