visualize.spec.tsx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. it('does not allow for deleting the only field or aggregate when there is an equation', async () => {
  836. render(
  837. <WidgetBuilderProvider>
  838. <Visualize />
  839. </WidgetBuilderProvider>,
  840. {
  841. organization,
  842. router: RouterFixture({
  843. location: LocationFixture({
  844. query: {
  845. dataset: WidgetType.TRANSACTIONS,
  846. field: ['equation|count()+1', 'count()'],
  847. },
  848. }),
  849. }),
  850. }
  851. );
  852. const removeButtons = await screen.findAllByRole('button', {name: 'Remove field'});
  853. expect(removeButtons[0]).toBeEnabled();
  854. expect(removeButtons[1]).toBeDisabled();
  855. });
  856. describe('spans', () => {
  857. beforeEach(() => {
  858. jest.mocked(useSpanTags).mockImplementation((type?: 'string' | 'number') => {
  859. if (type === 'number') {
  860. return {
  861. 'span.duration': {
  862. key: 'span.duration',
  863. name: 'span.duration',
  864. kind: 'measurement',
  865. },
  866. 'tags[anotherNumericTag,number]': {
  867. key: 'anotherNumericTag',
  868. name: 'anotherNumericTag',
  869. kind: 'measurement',
  870. },
  871. } as TagCollection;
  872. }
  873. return {
  874. 'span.description': {
  875. key: 'span.description',
  876. name: 'span.description',
  877. kind: 'tag',
  878. },
  879. } as TagCollection;
  880. });
  881. });
  882. it('shows numeric tags as primary options for chart widgets', async () => {
  883. render(
  884. <WidgetBuilderProvider>
  885. <Visualize />
  886. </WidgetBuilderProvider>,
  887. {
  888. organization,
  889. router: RouterFixture({
  890. location: LocationFixture({
  891. query: {
  892. dataset: WidgetType.SPANS,
  893. displayType: DisplayType.LINE,
  894. yAxis: ['p90(span.duration)'],
  895. },
  896. }),
  897. }),
  898. }
  899. );
  900. await userEvent.click(
  901. await screen.findByRole('button', {name: 'Column Selection'})
  902. );
  903. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  904. expect(within(listbox).getByText('anotherNumericTag')).toBeInTheDocument();
  905. expect(within(listbox).queryByText('span.description')).not.toBeInTheDocument();
  906. });
  907. it('shows the correct aggregate options', async () => {
  908. render(
  909. <WidgetBuilderProvider>
  910. <Visualize />
  911. </WidgetBuilderProvider>,
  912. {
  913. organization,
  914. router: RouterFixture({
  915. location: LocationFixture({
  916. query: {
  917. dataset: WidgetType.SPANS,
  918. displayType: DisplayType.LINE,
  919. yAxis: ['count(span.duration)'],
  920. },
  921. }),
  922. }),
  923. }
  924. );
  925. await userEvent.click(
  926. await screen.findByRole('button', {name: 'Aggregate Selection'})
  927. );
  928. // count has two entries because it's already selected
  929. // and in the dropdown
  930. expect(screen.getAllByText('count')).toHaveLength(2);
  931. expect(screen.getByText('avg')).toBeInTheDocument();
  932. expect(screen.getByText('max')).toBeInTheDocument();
  933. expect(screen.getByText('min')).toBeInTheDocument();
  934. expect(screen.getByText('p50')).toBeInTheDocument();
  935. expect(screen.getByText('p75')).toBeInTheDocument();
  936. expect(screen.getByText('p90')).toBeInTheDocument();
  937. expect(screen.getByText('p95')).toBeInTheDocument();
  938. expect(screen.getByText('p99')).toBeInTheDocument();
  939. expect(screen.getByText('p100')).toBeInTheDocument();
  940. expect(screen.getByText('sum')).toBeInTheDocument();
  941. });
  942. it('shows the correct column options for the aggregate field type', async () => {
  943. render(
  944. <WidgetBuilderProvider>
  945. <Visualize />
  946. </WidgetBuilderProvider>,
  947. {
  948. organization,
  949. router: RouterFixture({
  950. location: LocationFixture({
  951. query: {
  952. dataset: WidgetType.SPANS,
  953. displayType: DisplayType.TABLE,
  954. field: ['p90(span.duration)'],
  955. },
  956. }),
  957. }),
  958. }
  959. );
  960. await userEvent.click(
  961. await screen.findByRole('button', {name: 'Column Selection'})
  962. );
  963. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  964. expect(within(listbox).getByText('span.duration')).toBeInTheDocument();
  965. expect(within(listbox).getByText('anotherNumericTag')).toBeInTheDocument();
  966. });
  967. it('shows the correct column options for the non-aggregate field type', async () => {
  968. render(
  969. <WidgetBuilderProvider>
  970. <Visualize />
  971. </WidgetBuilderProvider>,
  972. {
  973. organization,
  974. router: RouterFixture({
  975. location: LocationFixture({
  976. query: {
  977. dataset: WidgetType.SPANS,
  978. displayType: DisplayType.TABLE,
  979. field: ['span.duration'],
  980. },
  981. }),
  982. }),
  983. }
  984. );
  985. await userEvent.click(
  986. await screen.findByRole('button', {name: 'Column Selection'})
  987. );
  988. const listbox = await screen.findByRole('listbox', {name: 'Column Selection'});
  989. expect(within(listbox).getByText('span.duration')).toBeInTheDocument();
  990. expect(within(listbox).getByText('span.description')).toBeInTheDocument();
  991. });
  992. });
  993. });