useWidgetBuilderState.spec.tsx 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {act, renderHook} from 'sentry-test/reactTestingLibrary';
  3. import type {Column} from 'sentry/utils/discover/fields';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import {useNavigate} from 'sentry/utils/useNavigate';
  6. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  7. import {WidgetBuilderProvider} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  8. import useWidgetBuilderState, {
  9. BuilderStateAction,
  10. serializeFields,
  11. } from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  12. import {FieldValueKind} from 'sentry/views/discover/table/types';
  13. jest.mock('sentry/utils/useLocation');
  14. jest.mock('sentry/utils/useNavigate');
  15. const mockedUsedLocation = jest.mocked(useLocation);
  16. const mockedUseNavigate = jest.mocked(useNavigate);
  17. describe('useWidgetBuilderState', () => {
  18. let mockNavigate!: jest.Mock;
  19. beforeEach(() => {
  20. mockNavigate = jest.fn();
  21. mockedUseNavigate.mockReturnValue(mockNavigate);
  22. jest.useFakeTimers();
  23. });
  24. afterEach(() => {
  25. jest.useRealTimers();
  26. jest.clearAllMocks();
  27. });
  28. it('returns the widget builder state from the query params', () => {
  29. mockedUsedLocation.mockReturnValue(
  30. LocationFixture({
  31. query: {
  32. title: 'test',
  33. description: 'lalala this is a description',
  34. },
  35. })
  36. );
  37. const {result} = renderHook(() => useWidgetBuilderState(), {
  38. wrapper: WidgetBuilderProvider,
  39. });
  40. expect(result.current.state.title).toBe('test');
  41. expect(result.current.state.description).toBe('lalala this is a description');
  42. });
  43. it('sets the new title and description in the query params', () => {
  44. const {result} = renderHook(() => useWidgetBuilderState(), {
  45. wrapper: WidgetBuilderProvider,
  46. });
  47. act(() => {
  48. result.current.dispatch({
  49. type: BuilderStateAction.SET_TITLE,
  50. payload: 'new title',
  51. });
  52. });
  53. act(() => {
  54. result.current.dispatch({
  55. type: BuilderStateAction.SET_DESCRIPTION,
  56. payload: 'new description',
  57. });
  58. });
  59. jest.runAllTimers();
  60. expect(mockNavigate).toHaveBeenCalledWith(
  61. expect.objectContaining({
  62. query: expect.objectContaining({title: 'new title'}),
  63. }),
  64. {replace: true}
  65. );
  66. expect(mockNavigate).toHaveBeenCalledWith(
  67. expect.objectContaining({
  68. query: expect.objectContaining({description: 'new description'}),
  69. }),
  70. {replace: true}
  71. );
  72. });
  73. describe('display type', () => {
  74. it('returns the display type from the query params', () => {
  75. mockedUsedLocation.mockReturnValue(
  76. LocationFixture({
  77. query: {displayType: DisplayType.AREA},
  78. })
  79. );
  80. const {result} = renderHook(() => useWidgetBuilderState(), {
  81. wrapper: WidgetBuilderProvider,
  82. });
  83. expect(result.current.state.displayType).toBe(DisplayType.AREA);
  84. });
  85. it('returns a default display type from the query params when the display type is not valid', () => {
  86. mockedUsedLocation.mockReturnValue(
  87. LocationFixture({
  88. query: {displayType: 'invalid'},
  89. })
  90. );
  91. const {result} = renderHook(() => useWidgetBuilderState(), {
  92. wrapper: WidgetBuilderProvider,
  93. });
  94. expect(result.current.state.displayType).toBe(DisplayType.TABLE);
  95. });
  96. it('sets the display type in the query params', () => {
  97. const {result} = renderHook(() => useWidgetBuilderState(), {
  98. wrapper: WidgetBuilderProvider,
  99. });
  100. act(() => {
  101. result.current.dispatch({
  102. type: BuilderStateAction.SET_DISPLAY_TYPE,
  103. payload: DisplayType.AREA,
  104. });
  105. });
  106. jest.runAllTimers();
  107. expect(mockNavigate).toHaveBeenCalledWith(
  108. expect.objectContaining({
  109. query: expect.objectContaining({displayType: DisplayType.AREA}),
  110. }),
  111. {replace: true}
  112. );
  113. });
  114. it('persists the values when going from timeseries to timeseries', () => {
  115. mockedUsedLocation.mockReturnValue(
  116. LocationFixture({
  117. query: {
  118. displayType: DisplayType.LINE,
  119. field: ['event.type'],
  120. yAxis: ['count()', 'count_unique(user)'],
  121. },
  122. })
  123. );
  124. const {result} = renderHook(() => useWidgetBuilderState(), {
  125. wrapper: WidgetBuilderProvider,
  126. });
  127. expect(result.current.state.displayType).toBe(DisplayType.LINE);
  128. expect(result.current.state.fields).toEqual([
  129. {field: 'event.type', alias: undefined, kind: 'field'},
  130. ]);
  131. expect(result.current.state.yAxis).toEqual([
  132. {
  133. function: ['count', '', undefined, undefined],
  134. alias: undefined,
  135. kind: 'function',
  136. },
  137. {
  138. function: ['count_unique', 'user', undefined, undefined],
  139. alias: undefined,
  140. kind: 'function',
  141. },
  142. ]);
  143. act(() => {
  144. result.current.dispatch({
  145. type: BuilderStateAction.SET_DISPLAY_TYPE,
  146. payload: DisplayType.AREA,
  147. });
  148. });
  149. expect(result.current.state.displayType).toBe(DisplayType.AREA);
  150. expect(result.current.state.fields).toEqual([
  151. {field: 'event.type', alias: undefined, kind: 'field'},
  152. ]);
  153. expect(result.current.state.yAxis).toEqual([
  154. {
  155. function: ['count', '', undefined, undefined],
  156. alias: undefined,
  157. kind: 'function',
  158. },
  159. {
  160. function: ['count_unique', 'user', undefined, undefined],
  161. alias: undefined,
  162. kind: 'function',
  163. },
  164. ]);
  165. });
  166. it('concatenates the values when going from timeseries to table', () => {
  167. mockedUsedLocation.mockReturnValue(
  168. LocationFixture({
  169. query: {
  170. displayType: DisplayType.LINE,
  171. field: ['event.type'],
  172. yAxis: ['count()', 'count_unique(user)'],
  173. },
  174. })
  175. );
  176. const {result} = renderHook(() => useWidgetBuilderState(), {
  177. wrapper: WidgetBuilderProvider,
  178. });
  179. expect(result.current.state.displayType).toBe(DisplayType.LINE);
  180. expect(result.current.state.fields).toEqual([
  181. {field: 'event.type', alias: undefined, kind: 'field'},
  182. ]);
  183. expect(result.current.state.yAxis).toEqual([
  184. {
  185. function: ['count', '', undefined, undefined],
  186. alias: undefined,
  187. kind: 'function',
  188. },
  189. {
  190. function: ['count_unique', 'user', undefined, undefined],
  191. alias: undefined,
  192. kind: 'function',
  193. },
  194. ]);
  195. act(() => {
  196. result.current.dispatch({
  197. type: BuilderStateAction.SET_DISPLAY_TYPE,
  198. payload: DisplayType.TABLE,
  199. });
  200. });
  201. expect(result.current.state.displayType).toBe(DisplayType.TABLE);
  202. expect(result.current.state.fields).toEqual([
  203. {field: 'event.type', alias: undefined, kind: 'field'},
  204. {
  205. function: ['count', '', undefined, undefined],
  206. alias: undefined,
  207. kind: 'function',
  208. },
  209. {
  210. function: ['count_unique', 'user', undefined, undefined],
  211. alias: undefined,
  212. kind: 'function',
  213. },
  214. ]);
  215. });
  216. it('separates the values when going from table to timeseries', () => {
  217. // remember, this takes up to 3 yAxes
  218. mockedUsedLocation.mockReturnValue(
  219. LocationFixture({
  220. query: {
  221. displayType: DisplayType.TABLE,
  222. field: [
  223. 'event.type',
  224. 'potato',
  225. 'count()',
  226. 'count_unique(user)',
  227. 'count_unique(potato)',
  228. 'count_unique(thisIsRemoved)',
  229. ],
  230. },
  231. })
  232. );
  233. const {result} = renderHook(() => useWidgetBuilderState(), {
  234. wrapper: WidgetBuilderProvider,
  235. });
  236. expect(result.current.state.displayType).toBe(DisplayType.TABLE);
  237. expect(result.current.state.fields).toEqual([
  238. {field: 'event.type', alias: undefined, kind: 'field'},
  239. {field: 'potato', alias: undefined, kind: 'field'},
  240. {
  241. function: ['count', '', undefined, undefined],
  242. alias: undefined,
  243. kind: 'function',
  244. },
  245. {
  246. function: ['count_unique', 'user', undefined, undefined],
  247. alias: undefined,
  248. kind: 'function',
  249. },
  250. {
  251. function: ['count_unique', 'potato', undefined, undefined],
  252. alias: undefined,
  253. kind: 'function',
  254. },
  255. {
  256. function: ['count_unique', 'thisIsRemoved', undefined, undefined],
  257. alias: undefined,
  258. kind: 'function',
  259. },
  260. ]);
  261. act(() => {
  262. result.current.dispatch({
  263. type: BuilderStateAction.SET_DISPLAY_TYPE,
  264. payload: DisplayType.LINE,
  265. });
  266. });
  267. expect(result.current.state.displayType).toBe(DisplayType.LINE);
  268. expect(result.current.state.fields).toEqual([
  269. {field: 'event.type', alias: undefined, kind: 'field'},
  270. {field: 'potato', alias: undefined, kind: 'field'},
  271. ]);
  272. expect(result.current.state.yAxis).toEqual([
  273. {
  274. function: ['count', '', undefined, undefined],
  275. alias: undefined,
  276. kind: 'function',
  277. },
  278. {
  279. function: ['count_unique', 'user', undefined, undefined],
  280. alias: undefined,
  281. kind: 'function',
  282. },
  283. {
  284. function: ['count_unique', 'potato', undefined, undefined],
  285. alias: undefined,
  286. kind: 'function',
  287. },
  288. ]);
  289. });
  290. it('does not duplicate fields when switching dataset in line chart then display type to table', () => {
  291. mockedUsedLocation.mockReturnValue(
  292. LocationFixture({
  293. query: {
  294. displayType: DisplayType.LINE,
  295. dataset: WidgetType.ERRORS,
  296. yAxis: ['count()'],
  297. },
  298. })
  299. );
  300. const {result} = renderHook(() => useWidgetBuilderState(), {
  301. wrapper: WidgetBuilderProvider,
  302. });
  303. expect(result.current.state.yAxis).toEqual([
  304. {
  305. function: ['count', '', undefined, undefined],
  306. alias: undefined,
  307. kind: 'function',
  308. },
  309. ]);
  310. act(() => {
  311. result.current.dispatch({
  312. type: BuilderStateAction.SET_DATASET,
  313. payload: WidgetType.TRANSACTIONS,
  314. });
  315. });
  316. expect(result.current.state.yAxis).toEqual([
  317. {
  318. function: ['count', '', undefined, undefined],
  319. alias: undefined,
  320. kind: 'function',
  321. },
  322. ]);
  323. expect(result.current.state.fields).toEqual([]);
  324. act(() => {
  325. result.current.dispatch({
  326. type: BuilderStateAction.SET_DISPLAY_TYPE,
  327. payload: DisplayType.TABLE,
  328. });
  329. });
  330. expect(result.current.state.fields).toEqual([
  331. {
  332. function: ['count', '', undefined, undefined],
  333. alias: undefined,
  334. kind: 'function',
  335. },
  336. ]);
  337. });
  338. it('does not duplicate fields when changing display from table to chart', () => {
  339. mockedUsedLocation.mockReturnValue(
  340. LocationFixture({
  341. query: {
  342. displayType: DisplayType.TABLE,
  343. dataset: WidgetType.ERRORS,
  344. field: ['count()'],
  345. },
  346. })
  347. );
  348. const {result} = renderHook(() => useWidgetBuilderState(), {
  349. wrapper: WidgetBuilderProvider,
  350. });
  351. expect(result.current.state.fields).toEqual([
  352. {
  353. function: ['count', '', undefined, undefined],
  354. alias: undefined,
  355. kind: 'function',
  356. },
  357. ]);
  358. act(() => {
  359. result.current.dispatch({
  360. type: BuilderStateAction.SET_DATASET,
  361. payload: WidgetType.SPANS,
  362. });
  363. });
  364. expect(result.current.state.fields).toEqual([
  365. {
  366. function: ['count', 'span.duration', undefined, undefined],
  367. alias: undefined,
  368. kind: 'function',
  369. },
  370. ]);
  371. expect(result.current.state.yAxis).toEqual([]);
  372. act(() => {
  373. result.current.dispatch({
  374. type: BuilderStateAction.SET_DISPLAY_TYPE,
  375. payload: DisplayType.LINE,
  376. });
  377. });
  378. expect(result.current.state.yAxis).toEqual([
  379. {
  380. function: ['count', 'span.duration', undefined, undefined],
  381. alias: undefined,
  382. kind: 'function',
  383. },
  384. ]);
  385. });
  386. it('does not duplicate fields when switching dataset in big number then display type to table', () => {
  387. mockedUsedLocation.mockReturnValue(
  388. LocationFixture({
  389. query: {
  390. displayType: DisplayType.BIG_NUMBER,
  391. dataset: WidgetType.ERRORS,
  392. field: ['count()'],
  393. },
  394. })
  395. );
  396. const {result} = renderHook(() => useWidgetBuilderState(), {
  397. wrapper: WidgetBuilderProvider,
  398. });
  399. expect(result.current.state.fields).toEqual([
  400. {
  401. function: ['count', '', undefined, undefined],
  402. alias: undefined,
  403. kind: 'function',
  404. },
  405. ]);
  406. act(() => {
  407. result.current.dispatch({
  408. type: BuilderStateAction.SET_DATASET,
  409. payload: WidgetType.TRANSACTIONS,
  410. });
  411. });
  412. expect(result.current.state.fields).toEqual([
  413. {
  414. function: ['count', '', undefined, undefined],
  415. alias: undefined,
  416. kind: 'function',
  417. },
  418. ]);
  419. expect(result.current.state.yAxis).toEqual([]);
  420. act(() => {
  421. result.current.dispatch({
  422. type: BuilderStateAction.SET_DISPLAY_TYPE,
  423. payload: DisplayType.TABLE,
  424. });
  425. });
  426. expect(result.current.state.fields).toEqual([
  427. {
  428. function: ['count', '', undefined, undefined],
  429. alias: undefined,
  430. kind: 'function',
  431. },
  432. ]);
  433. });
  434. it('sets the aggregate as fields when switching to big number', () => {
  435. mockedUsedLocation.mockReturnValue(
  436. LocationFixture({
  437. query: {
  438. displayType: DisplayType.TABLE,
  439. field: ['event.type', 'count()'],
  440. sort: ['-count()'],
  441. },
  442. })
  443. );
  444. const {result} = renderHook(() => useWidgetBuilderState(), {
  445. wrapper: WidgetBuilderProvider,
  446. });
  447. expect(result.current.state.fields).toEqual([
  448. {field: 'event.type', alias: undefined, kind: 'field'},
  449. {
  450. function: ['count', '', undefined, undefined],
  451. alias: undefined,
  452. kind: 'function',
  453. },
  454. ]);
  455. act(() => {
  456. result.current.dispatch({
  457. type: BuilderStateAction.SET_DISPLAY_TYPE,
  458. payload: DisplayType.BIG_NUMBER,
  459. });
  460. });
  461. expect(result.current.state.fields).toEqual([
  462. {
  463. function: ['count', '', undefined, undefined],
  464. alias: undefined,
  465. kind: 'function',
  466. },
  467. ]);
  468. expect(result.current.state.sort).toEqual([]);
  469. });
  470. it('selects the first filter when switching to big number', () => {
  471. mockedUsedLocation.mockReturnValue(
  472. LocationFixture({
  473. query: {
  474. field: ['event.type', 'count()', 'count_unique(user)'],
  475. query: ['event.type:test', 'event.type:test2'],
  476. },
  477. })
  478. );
  479. const {result} = renderHook(() => useWidgetBuilderState(), {
  480. wrapper: WidgetBuilderProvider,
  481. });
  482. expect(result.current.state.query).toEqual(['event.type:test', 'event.type:test2']);
  483. act(() => {
  484. result.current.dispatch({
  485. type: BuilderStateAction.SET_DISPLAY_TYPE,
  486. payload: DisplayType.BIG_NUMBER,
  487. });
  488. });
  489. expect(result.current.state.query).toEqual(['event.type:test']);
  490. });
  491. it('resets selectedAggregate when the display type is switched', () => {
  492. mockedUsedLocation.mockReturnValue(
  493. LocationFixture({query: {selectedAggregate: '0'}})
  494. );
  495. const {result} = renderHook(() => useWidgetBuilderState(), {
  496. wrapper: WidgetBuilderProvider,
  497. });
  498. expect(result.current.state.selectedAggregate).toBeUndefined();
  499. act(() => {
  500. result.current.dispatch({
  501. type: BuilderStateAction.SET_DISPLAY_TYPE,
  502. payload: DisplayType.TABLE,
  503. });
  504. });
  505. expect(result.current.state.selectedAggregate).toBeUndefined();
  506. });
  507. it('resets thresholds when the display type is switched', () => {
  508. mockedUsedLocation.mockReturnValue(
  509. LocationFixture({
  510. query: {
  511. dataset: WidgetType.ERRORS,
  512. displayType: DisplayType.BIG_NUMBER,
  513. thresholds: '{"max_values":{"max1":200,"max2":300},"unit":"milliseconds"}',
  514. },
  515. })
  516. );
  517. const {result} = renderHook(() => useWidgetBuilderState(), {
  518. wrapper: WidgetBuilderProvider,
  519. });
  520. expect(result.current.state.thresholds).toEqual({
  521. max_values: {max1: 200, max2: 300},
  522. unit: 'milliseconds',
  523. });
  524. act(() => {
  525. result.current.dispatch({
  526. type: BuilderStateAction.SET_DISPLAY_TYPE,
  527. payload: DisplayType.TABLE,
  528. });
  529. });
  530. expect(result.current.state.thresholds).toBeUndefined();
  531. });
  532. });
  533. describe('dataset', () => {
  534. it('returns the dataset from the query params', () => {
  535. mockedUsedLocation.mockReturnValue(
  536. LocationFixture({query: {dataset: WidgetType.ISSUE}})
  537. );
  538. const {result} = renderHook(() => useWidgetBuilderState(), {
  539. wrapper: WidgetBuilderProvider,
  540. });
  541. expect(result.current.state.dataset).toBe(WidgetType.ISSUE);
  542. });
  543. it('sets the dataset in the query params', () => {
  544. const {result} = renderHook(() => useWidgetBuilderState(), {
  545. wrapper: WidgetBuilderProvider,
  546. });
  547. act(() => {
  548. result.current.dispatch({
  549. type: BuilderStateAction.SET_DATASET,
  550. payload: WidgetType.METRICS,
  551. });
  552. });
  553. jest.runAllTimers();
  554. expect(mockNavigate).toHaveBeenCalledWith(
  555. expect.objectContaining({
  556. query: expect.objectContaining({dataset: WidgetType.METRICS}),
  557. }),
  558. {replace: true}
  559. );
  560. });
  561. it('returns errors as the default dataset', () => {
  562. mockedUsedLocation.mockReturnValue(LocationFixture({query: {dataset: 'invalid'}}));
  563. const {result} = renderHook(() => useWidgetBuilderState(), {
  564. wrapper: WidgetBuilderProvider,
  565. });
  566. expect(result.current.state.dataset).toBe(WidgetType.ERRORS);
  567. });
  568. it('resets the display type to table when the dataset is switched to issues', () => {
  569. mockedUsedLocation.mockReturnValue(
  570. LocationFixture({
  571. query: {dataset: WidgetType.TRANSACTIONS, displayType: DisplayType.LINE},
  572. })
  573. );
  574. const {result} = renderHook(() => useWidgetBuilderState(), {
  575. wrapper: WidgetBuilderProvider,
  576. });
  577. expect(result.current.state.displayType).toBe(DisplayType.LINE);
  578. act(() => {
  579. result.current.dispatch({
  580. type: BuilderStateAction.SET_DATASET,
  581. payload: WidgetType.ISSUE,
  582. });
  583. });
  584. expect(result.current.state.displayType).toBe(DisplayType.TABLE);
  585. });
  586. it('resets the fields, yAxis, query, and sort when the dataset is switched', () => {
  587. mockedUsedLocation.mockReturnValue(
  588. LocationFixture({
  589. query: {
  590. title: 'This title should persist',
  591. description: 'This description should persist',
  592. dataset: WidgetType.TRANSACTIONS,
  593. field: ['event.type', 'potato', 'count()'],
  594. yAxis: ['count()', 'count_unique(user)'],
  595. query: ['event.type = "test"'],
  596. sort: ['-testField'],
  597. },
  598. })
  599. );
  600. const {result} = renderHook(() => useWidgetBuilderState(), {
  601. wrapper: WidgetBuilderProvider,
  602. });
  603. act(() => {
  604. result.current.dispatch({
  605. type: BuilderStateAction.SET_DATASET,
  606. payload: WidgetType.SPANS,
  607. });
  608. });
  609. expect(result.current.state.title).toBe('This title should persist');
  610. expect(result.current.state.description).toBe('This description should persist');
  611. expect(result.current.state.fields).toEqual([
  612. {
  613. function: ['count', 'span.duration', undefined, undefined],
  614. alias: undefined,
  615. kind: 'function',
  616. },
  617. ]);
  618. expect(result.current.state.yAxis).toEqual([]);
  619. expect(result.current.state.query).toEqual(['']);
  620. expect(result.current.state.sort).toEqual([
  621. {
  622. field: 'count(span.duration)',
  623. kind: 'desc',
  624. },
  625. ]);
  626. });
  627. it('resets the yAxis when the dataset is switched from anything to issues', () => {
  628. mockedUsedLocation.mockReturnValue(
  629. LocationFixture({
  630. query: {
  631. dataset: WidgetType.TRANSACTIONS,
  632. yAxis: ['count()', 'count_unique(user)'],
  633. displayType: DisplayType.LINE,
  634. },
  635. })
  636. );
  637. const {result} = renderHook(() => useWidgetBuilderState(), {
  638. wrapper: WidgetBuilderProvider,
  639. });
  640. expect(result.current.state.yAxis).toEqual([
  641. {
  642. function: ['count', '', undefined, undefined],
  643. alias: undefined,
  644. kind: 'function',
  645. },
  646. {
  647. function: ['count_unique', 'user', undefined, undefined],
  648. alias: undefined,
  649. kind: 'function',
  650. },
  651. ]);
  652. act(() => {
  653. result.current.dispatch({
  654. type: BuilderStateAction.SET_DATASET,
  655. payload: WidgetType.ISSUE,
  656. });
  657. });
  658. expect(result.current.state.yAxis).toEqual([]);
  659. });
  660. it('resets the sort when the display type is switched and the sort is not in the new fields', () => {
  661. mockedUsedLocation.mockReturnValue(
  662. LocationFixture({
  663. query: {
  664. displayType: DisplayType.LINE,
  665. field: ['testField', 'testField2'],
  666. sort: ['-project.name'],
  667. },
  668. })
  669. );
  670. const {result} = renderHook(() => useWidgetBuilderState(), {
  671. wrapper: WidgetBuilderProvider,
  672. });
  673. expect(result.current.state.sort).toEqual([{field: 'project.name', kind: 'desc'}]);
  674. act(() => {
  675. result.current.dispatch({
  676. type: BuilderStateAction.SET_DISPLAY_TYPE,
  677. payload: DisplayType.TABLE,
  678. });
  679. });
  680. expect(result.current.state.sort).toEqual([
  681. {
  682. field: 'testField',
  683. kind: 'desc',
  684. },
  685. ]);
  686. });
  687. it('keeps sort when the sort is in the new fields', () => {
  688. mockedUsedLocation.mockReturnValue(
  689. LocationFixture({
  690. query: {
  691. displayType: DisplayType.LINE,
  692. field: ['testField', 'testField2'],
  693. sort: ['-testField'],
  694. },
  695. })
  696. );
  697. const {result} = renderHook(() => useWidgetBuilderState(), {
  698. wrapper: WidgetBuilderProvider,
  699. });
  700. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  701. act(() => {
  702. result.current.dispatch({
  703. type: BuilderStateAction.SET_DISPLAY_TYPE,
  704. payload: DisplayType.TABLE,
  705. });
  706. });
  707. expect(result.current.state.sort).toEqual([
  708. {
  709. field: 'testField',
  710. kind: 'desc',
  711. },
  712. ]);
  713. });
  714. it('resets selectedAggregate when the dataset is switched', () => {
  715. mockedUsedLocation.mockReturnValue(
  716. LocationFixture({
  717. query: {
  718. selectedAggregate: '0',
  719. displayType: DisplayType.BIG_NUMBER,
  720. field: ['count_unique(1)', 'count_unique(2)'],
  721. },
  722. })
  723. );
  724. const {result} = renderHook(() => useWidgetBuilderState(), {
  725. wrapper: WidgetBuilderProvider,
  726. });
  727. expect(result.current.state.selectedAggregate).toBe(0);
  728. act(() => {
  729. result.current.dispatch({
  730. type: BuilderStateAction.SET_DATASET,
  731. payload: WidgetType.SPANS,
  732. });
  733. });
  734. expect(result.current.state.selectedAggregate).toBeUndefined();
  735. });
  736. it('resets the sort when the dataset is switched for big number widgets', () => {
  737. mockedUsedLocation.mockReturnValue(
  738. LocationFixture({
  739. query: {
  740. dataset: WidgetType.ERRORS,
  741. displayType: DisplayType.BIG_NUMBER,
  742. sort: ['-testField'],
  743. },
  744. })
  745. );
  746. const {result} = renderHook(() => useWidgetBuilderState(), {
  747. wrapper: WidgetBuilderProvider,
  748. });
  749. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  750. act(() => {
  751. result.current.dispatch({
  752. type: BuilderStateAction.SET_DATASET,
  753. payload: WidgetType.TRANSACTIONS,
  754. });
  755. });
  756. expect(result.current.state.sort).toEqual([]);
  757. });
  758. it('resets thresholds when the dataset is switched', () => {
  759. mockedUsedLocation.mockReturnValue(
  760. LocationFixture({
  761. query: {
  762. dataset: WidgetType.ERRORS,
  763. displayType: DisplayType.BIG_NUMBER,
  764. thresholds: '{"max_values":{"max1":200,"max2":300},"unit":"milliseconds"}',
  765. },
  766. })
  767. );
  768. const {result} = renderHook(() => useWidgetBuilderState(), {
  769. wrapper: WidgetBuilderProvider,
  770. });
  771. expect(result.current.state.thresholds).toEqual({
  772. max_values: {max1: 200, max2: 300},
  773. unit: 'milliseconds',
  774. });
  775. act(() => {
  776. result.current.dispatch({
  777. type: BuilderStateAction.SET_DATASET,
  778. payload: WidgetType.TRANSACTIONS,
  779. });
  780. });
  781. expect(result.current.state.thresholds).toBeUndefined();
  782. });
  783. it('resets the legend alias when the dataset is switched', () => {
  784. mockedUsedLocation.mockReturnValue(
  785. LocationFixture({
  786. query: {
  787. dataset: WidgetType.ERRORS,
  788. displayType: DisplayType.LINE,
  789. legendAlias: ['test'],
  790. },
  791. })
  792. );
  793. const {result} = renderHook(() => useWidgetBuilderState(), {
  794. wrapper: WidgetBuilderProvider,
  795. });
  796. expect(result.current.state.legendAlias).toEqual(['test']);
  797. act(() => {
  798. result.current.dispatch({
  799. type: BuilderStateAction.SET_DATASET,
  800. payload: WidgetType.TRANSACTIONS,
  801. });
  802. });
  803. expect(result.current.state.legendAlias).toEqual([]);
  804. });
  805. });
  806. describe('fields', () => {
  807. it('returns the fields from the query params', () => {
  808. mockedUsedLocation.mockReturnValue(
  809. LocationFixture({query: {field: ['event.type', 'potato', 'count()']}})
  810. );
  811. const {result} = renderHook(() => useWidgetBuilderState(), {
  812. wrapper: WidgetBuilderProvider,
  813. });
  814. expect(result.current.state.fields).toEqual([
  815. {field: 'event.type', alias: undefined, kind: 'field'},
  816. {field: 'potato', alias: undefined, kind: 'field'},
  817. {
  818. alias: undefined,
  819. kind: 'function',
  820. function: ['count', '', undefined, undefined],
  821. },
  822. ]);
  823. });
  824. it('decodes both JSON formatted fields and non-JSON formatted fields', () => {
  825. mockedUsedLocation.mockReturnValue(
  826. LocationFixture({
  827. query: {
  828. field: [
  829. '{"field": "event.type", "alias": "test"}',
  830. 'p90(transaction.duration)',
  831. ],
  832. },
  833. })
  834. );
  835. const {result} = renderHook(() => useWidgetBuilderState(), {
  836. wrapper: WidgetBuilderProvider,
  837. });
  838. expect(result.current.state.fields).toEqual([
  839. {field: 'event.type', alias: 'test', kind: 'field'},
  840. {
  841. function: ['p90', 'transaction.duration', undefined, undefined],
  842. alias: undefined,
  843. kind: 'function',
  844. },
  845. ]);
  846. });
  847. it('encodes fields to JSON when they have aliases', () => {
  848. const fields = [
  849. {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
  850. {field: 'event.type', alias: undefined, kind: FieldValueKind.FIELD},
  851. ] as Column[];
  852. const encodedFields = serializeFields(fields);
  853. expect(encodedFields).toEqual([
  854. '{"field":"event.type","alias":"test"}',
  855. 'event.type',
  856. ]);
  857. });
  858. it('wipes the alias when the dataset is switched', () => {
  859. mockedUsedLocation.mockReturnValue(
  860. LocationFixture({
  861. query: {
  862. dataset: WidgetType.ERRORS,
  863. displayType: DisplayType.TABLE,
  864. field: ['{"field":"event.type","alias":"test"}'],
  865. },
  866. })
  867. );
  868. const {result} = renderHook(() => useWidgetBuilderState(), {
  869. wrapper: WidgetBuilderProvider,
  870. });
  871. expect(result.current.state.fields).toEqual([
  872. {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
  873. ]);
  874. act(() => {
  875. result.current.dispatch({
  876. type: BuilderStateAction.SET_DATASET,
  877. payload: WidgetType.TRANSACTIONS,
  878. });
  879. });
  880. expect(result.current.state.fields).toEqual([
  881. {
  882. function: ['count', '', undefined, undefined],
  883. alias: undefined,
  884. kind: 'function',
  885. },
  886. ]);
  887. });
  888. it('wipes the alias when the display type is switched', () => {
  889. mockedUsedLocation.mockReturnValue(
  890. LocationFixture({
  891. query: {
  892. displayType: DisplayType.TABLE,
  893. field: ['{"field":"count()","alias":"test"}'],
  894. },
  895. })
  896. );
  897. const {result} = renderHook(() => useWidgetBuilderState(), {
  898. wrapper: WidgetBuilderProvider,
  899. });
  900. expect(result.current.state.fields).toEqual([
  901. {function: ['count', '', undefined, undefined], alias: 'test', kind: 'function'},
  902. ]);
  903. act(() => {
  904. result.current.dispatch({
  905. type: BuilderStateAction.SET_DISPLAY_TYPE,
  906. payload: DisplayType.LINE,
  907. });
  908. });
  909. expect(result.current.state.yAxis).toEqual([
  910. {
  911. function: ['count', '', undefined, undefined],
  912. alias: undefined,
  913. kind: 'function',
  914. },
  915. ]);
  916. });
  917. it('resets the sort when the field that is being sorted is removed', () => {
  918. mockedUsedLocation.mockReturnValue(
  919. LocationFixture({
  920. query: {field: ['testField'], sort: ['-testField']},
  921. })
  922. );
  923. const {result} = renderHook(() => useWidgetBuilderState(), {
  924. wrapper: WidgetBuilderProvider,
  925. });
  926. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  927. act(() => {
  928. result.current.dispatch({
  929. type: BuilderStateAction.SET_FIELDS,
  930. payload: [{field: 'testField2', kind: FieldValueKind.FIELD}],
  931. });
  932. });
  933. expect(result.current.state.sort).toEqual([{field: 'testField2', kind: 'desc'}]);
  934. });
  935. it('modifies the sort when the field that is being sorted is modified', () => {
  936. mockedUsedLocation.mockReturnValue(
  937. LocationFixture({
  938. query: {field: ['testField', 'sortField'], sort: ['-sortField']},
  939. })
  940. );
  941. const {result} = renderHook(() => useWidgetBuilderState(), {
  942. wrapper: WidgetBuilderProvider,
  943. });
  944. expect(result.current.state.sort).toEqual([{field: 'sortField', kind: 'desc'}]);
  945. act(() => {
  946. result.current.dispatch({
  947. type: BuilderStateAction.SET_FIELDS,
  948. payload: [
  949. {field: 'testField', kind: FieldValueKind.FIELD},
  950. {field: 'newSortField', kind: FieldValueKind.FIELD},
  951. ],
  952. });
  953. });
  954. expect(result.current.state.sort).toEqual([{field: 'newSortField', kind: 'desc'}]);
  955. });
  956. it('does not reset the table sort for issue widgets', () => {
  957. mockedUsedLocation.mockReturnValue(
  958. LocationFixture({
  959. query: {
  960. dataset: WidgetType.ISSUE,
  961. field: ['testField'],
  962. sort: ['-notInFields'],
  963. },
  964. })
  965. );
  966. const {result} = renderHook(() => useWidgetBuilderState(), {
  967. wrapper: WidgetBuilderProvider,
  968. });
  969. expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
  970. act(() => {
  971. result.current.dispatch({
  972. type: BuilderStateAction.SET_FIELDS,
  973. payload: [{field: 'testField', kind: FieldValueKind.FIELD}],
  974. });
  975. });
  976. expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
  977. });
  978. it('adds a default sort when adding a grouping for a timeseries chart', () => {
  979. mockedUsedLocation.mockReturnValue(
  980. LocationFixture({
  981. query: {
  982. displayType: DisplayType.LINE,
  983. field: [],
  984. yAxis: ['count()'],
  985. },
  986. })
  987. );
  988. const {result} = renderHook(() => useWidgetBuilderState(), {
  989. wrapper: WidgetBuilderProvider,
  990. });
  991. expect(result.current.state.yAxis).toEqual([
  992. {function: ['count', '', undefined, undefined], kind: 'function'},
  993. ]);
  994. act(() => {
  995. result.current.dispatch({
  996. type: BuilderStateAction.SET_FIELDS,
  997. payload: [{field: 'browser.name', kind: FieldValueKind.FIELD}],
  998. });
  999. });
  1000. // The y-axis takes priority
  1001. expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
  1002. });
  1003. it('ensures that default sort is not an equation', () => {
  1004. mockedUsedLocation.mockReturnValue(
  1005. LocationFixture({
  1006. query: {
  1007. displayType: DisplayType.LINE,
  1008. field: [],
  1009. yAxis: ['equation|count()+1', 'count()'],
  1010. },
  1011. })
  1012. );
  1013. const {result} = renderHook(() => useWidgetBuilderState(), {
  1014. wrapper: WidgetBuilderProvider,
  1015. });
  1016. expect(result.current.state.sort).toEqual([]);
  1017. act(() => {
  1018. result.current.dispatch({
  1019. type: BuilderStateAction.SET_FIELDS,
  1020. payload: [{field: 'browser.name', kind: FieldValueKind.FIELD}],
  1021. });
  1022. });
  1023. expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
  1024. });
  1025. });
  1026. describe('yAxis', () => {
  1027. it('does not conflict with fields when setting the state', () => {
  1028. mockedUsedLocation.mockReturnValue(
  1029. LocationFixture({
  1030. query: {
  1031. field: ['event.type', 'potato', 'count()'],
  1032. yAxis: ['count()', 'count_unique(user)'],
  1033. },
  1034. })
  1035. );
  1036. const {result} = renderHook(() => useWidgetBuilderState(), {
  1037. wrapper: WidgetBuilderProvider,
  1038. });
  1039. expect(result.current.state.fields).toEqual([
  1040. {field: 'event.type', alias: undefined, kind: 'field'},
  1041. {field: 'potato', alias: undefined, kind: 'field'},
  1042. {
  1043. function: ['count', '', undefined, undefined],
  1044. alias: undefined,
  1045. kind: 'function',
  1046. },
  1047. ]);
  1048. expect(result.current.state.yAxis).toEqual([
  1049. {
  1050. function: ['count', '', undefined, undefined],
  1051. alias: undefined,
  1052. kind: 'function',
  1053. },
  1054. {
  1055. function: ['count_unique', 'user', undefined, undefined],
  1056. alias: undefined,
  1057. kind: 'function',
  1058. },
  1059. ]);
  1060. });
  1061. it('clears the sort when the y-axis changes and there is no grouping', () => {
  1062. mockedUsedLocation.mockReturnValue(
  1063. LocationFixture({
  1064. query: {
  1065. displayType: DisplayType.LINE,
  1066. field: [],
  1067. yAxis: ['count()'],
  1068. sort: ['-count()'],
  1069. },
  1070. })
  1071. );
  1072. const {result} = renderHook(() => useWidgetBuilderState(), {
  1073. wrapper: WidgetBuilderProvider,
  1074. });
  1075. expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
  1076. act(() => {
  1077. result.current.dispatch({
  1078. type: BuilderStateAction.SET_Y_AXIS,
  1079. payload: [
  1080. {function: ['count_unique', 'user', undefined, undefined], kind: 'function'},
  1081. ],
  1082. });
  1083. });
  1084. expect(result.current.state.sort).toEqual([]);
  1085. });
  1086. });
  1087. describe('sort', () => {
  1088. it('can decode and update sorts', () => {
  1089. mockedUsedLocation.mockReturnValue(
  1090. LocationFixture({
  1091. query: {
  1092. sort: ['-testField'],
  1093. },
  1094. })
  1095. );
  1096. const {result} = renderHook(() => useWidgetBuilderState(), {
  1097. wrapper: WidgetBuilderProvider,
  1098. });
  1099. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  1100. act(() => {
  1101. result.current.dispatch({
  1102. type: BuilderStateAction.SET_SORT,
  1103. payload: [{field: 'testField', kind: 'asc'}],
  1104. });
  1105. });
  1106. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'asc'}]);
  1107. });
  1108. });
  1109. describe('limit', () => {
  1110. it('can decode and update limit', () => {
  1111. mockedUsedLocation.mockReturnValue(
  1112. LocationFixture({
  1113. query: {
  1114. limit: '4',
  1115. },
  1116. })
  1117. );
  1118. const {result} = renderHook(() => useWidgetBuilderState(), {
  1119. wrapper: WidgetBuilderProvider,
  1120. });
  1121. expect(result.current.state.limit).toBe(4);
  1122. act(() => {
  1123. result.current.dispatch({
  1124. type: BuilderStateAction.SET_LIMIT,
  1125. payload: 10,
  1126. });
  1127. });
  1128. expect(result.current.state.limit).toBe(10);
  1129. });
  1130. });
  1131. describe('legendAlias', () => {
  1132. it('can decode and update legendAlias', () => {
  1133. mockedUsedLocation.mockReturnValue(
  1134. LocationFixture({
  1135. query: {
  1136. legendAlias: ['test', 'test2'],
  1137. },
  1138. })
  1139. );
  1140. const {result} = renderHook(() => useWidgetBuilderState(), {
  1141. wrapper: WidgetBuilderProvider,
  1142. });
  1143. expect(result.current.state.legendAlias).toEqual(['test', 'test2']);
  1144. act(() => {
  1145. result.current.dispatch({
  1146. type: BuilderStateAction.SET_LEGEND_ALIAS,
  1147. payload: ['test3', 'test4'],
  1148. });
  1149. });
  1150. expect(result.current.state.legendAlias).toEqual(['test3', 'test4']);
  1151. });
  1152. });
  1153. describe('selectedAggregate', () => {
  1154. it('can decode and update selectedAggregate', () => {
  1155. mockedUsedLocation.mockReturnValue(
  1156. LocationFixture({
  1157. query: {
  1158. selectedAggregate: '0',
  1159. displayType: DisplayType.BIG_NUMBER,
  1160. field: ['count()', 'count_unique(user)'],
  1161. },
  1162. })
  1163. );
  1164. const {result} = renderHook(() => useWidgetBuilderState(), {
  1165. wrapper: WidgetBuilderProvider,
  1166. });
  1167. expect(result.current.state.selectedAggregate).toBe(0);
  1168. act(() => {
  1169. result.current.dispatch({
  1170. type: BuilderStateAction.SET_SELECTED_AGGREGATE,
  1171. payload: 1,
  1172. });
  1173. });
  1174. expect(result.current.state.selectedAggregate).toBe(1);
  1175. });
  1176. it('can set selectedAggregate to undefined in the URL', () => {
  1177. mockedUsedLocation.mockReturnValue(
  1178. LocationFixture({
  1179. query: {
  1180. selectedAggregate: '0',
  1181. displayType: DisplayType.BIG_NUMBER,
  1182. field: ['count()', 'count_unique(user)'],
  1183. },
  1184. })
  1185. );
  1186. const {result} = renderHook(() => useWidgetBuilderState(), {
  1187. wrapper: WidgetBuilderProvider,
  1188. });
  1189. expect(result.current.state.selectedAggregate).toBe(0);
  1190. act(() => {
  1191. result.current.dispatch({
  1192. type: BuilderStateAction.SET_SELECTED_AGGREGATE,
  1193. payload: undefined,
  1194. });
  1195. });
  1196. // If selectedAggregate is undefined in the URL, then the widget builder state
  1197. // will set the selectedAggregate to the last aggregate
  1198. expect(result.current.state.selectedAggregate).toBe(1);
  1199. expect(mockNavigate).toHaveBeenCalledWith(
  1200. expect.objectContaining({
  1201. query: expect.objectContaining({selectedAggregate: undefined}),
  1202. }),
  1203. {replace: true}
  1204. );
  1205. });
  1206. });
  1207. });