useWidgetBuilderState.spec.tsx 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. });
  784. describe('fields', () => {
  785. it('returns the fields from the query params', () => {
  786. mockedUsedLocation.mockReturnValue(
  787. LocationFixture({query: {field: ['event.type', 'potato', 'count()']}})
  788. );
  789. const {result} = renderHook(() => useWidgetBuilderState(), {
  790. wrapper: WidgetBuilderProvider,
  791. });
  792. expect(result.current.state.fields).toEqual([
  793. {field: 'event.type', alias: undefined, kind: 'field'},
  794. {field: 'potato', alias: undefined, kind: 'field'},
  795. {
  796. alias: undefined,
  797. kind: 'function',
  798. function: ['count', '', undefined, undefined],
  799. },
  800. ]);
  801. });
  802. it('decodes both JSON formatted fields and non-JSON formatted fields', () => {
  803. mockedUsedLocation.mockReturnValue(
  804. LocationFixture({
  805. query: {
  806. field: [
  807. '{"field": "event.type", "alias": "test"}',
  808. 'p90(transaction.duration)',
  809. ],
  810. },
  811. })
  812. );
  813. const {result} = renderHook(() => useWidgetBuilderState(), {
  814. wrapper: WidgetBuilderProvider,
  815. });
  816. expect(result.current.state.fields).toEqual([
  817. {field: 'event.type', alias: 'test', kind: 'field'},
  818. {
  819. function: ['p90', 'transaction.duration', undefined, undefined],
  820. alias: undefined,
  821. kind: 'function',
  822. },
  823. ]);
  824. });
  825. it('encodes fields to JSON when they have aliases', () => {
  826. const fields = [
  827. {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
  828. {field: 'event.type', alias: undefined, kind: FieldValueKind.FIELD},
  829. ] as Column[];
  830. const encodedFields = serializeFields(fields);
  831. expect(encodedFields).toEqual([
  832. '{"field":"event.type","alias":"test"}',
  833. 'event.type',
  834. ]);
  835. });
  836. it('wipes the alias when the dataset is switched', () => {
  837. mockedUsedLocation.mockReturnValue(
  838. LocationFixture({
  839. query: {
  840. dataset: WidgetType.ERRORS,
  841. displayType: DisplayType.TABLE,
  842. field: ['{"field":"event.type","alias":"test"}'],
  843. },
  844. })
  845. );
  846. const {result} = renderHook(() => useWidgetBuilderState(), {
  847. wrapper: WidgetBuilderProvider,
  848. });
  849. expect(result.current.state.fields).toEqual([
  850. {field: 'event.type', alias: 'test', kind: FieldValueKind.FIELD},
  851. ]);
  852. act(() => {
  853. result.current.dispatch({
  854. type: BuilderStateAction.SET_DATASET,
  855. payload: WidgetType.TRANSACTIONS,
  856. });
  857. });
  858. expect(result.current.state.fields).toEqual([
  859. {
  860. function: ['count', '', undefined, undefined],
  861. alias: undefined,
  862. kind: 'function',
  863. },
  864. ]);
  865. });
  866. it('wipes the alias when the display type is switched', () => {
  867. mockedUsedLocation.mockReturnValue(
  868. LocationFixture({
  869. query: {
  870. displayType: DisplayType.TABLE,
  871. field: ['{"field":"count()","alias":"test"}'],
  872. },
  873. })
  874. );
  875. const {result} = renderHook(() => useWidgetBuilderState(), {
  876. wrapper: WidgetBuilderProvider,
  877. });
  878. expect(result.current.state.fields).toEqual([
  879. {function: ['count', '', undefined, undefined], alias: 'test', kind: 'function'},
  880. ]);
  881. act(() => {
  882. result.current.dispatch({
  883. type: BuilderStateAction.SET_DISPLAY_TYPE,
  884. payload: DisplayType.LINE,
  885. });
  886. });
  887. expect(result.current.state.yAxis).toEqual([
  888. {
  889. function: ['count', '', undefined, undefined],
  890. alias: undefined,
  891. kind: 'function',
  892. },
  893. ]);
  894. });
  895. it('resets the sort when the field that is being sorted is removed', () => {
  896. mockedUsedLocation.mockReturnValue(
  897. LocationFixture({
  898. query: {field: ['testField'], sort: ['-testField']},
  899. })
  900. );
  901. const {result} = renderHook(() => useWidgetBuilderState(), {
  902. wrapper: WidgetBuilderProvider,
  903. });
  904. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  905. act(() => {
  906. result.current.dispatch({
  907. type: BuilderStateAction.SET_FIELDS,
  908. payload: [{field: 'testField2', kind: FieldValueKind.FIELD}],
  909. });
  910. });
  911. expect(result.current.state.sort).toEqual([{field: 'testField2', kind: 'desc'}]);
  912. });
  913. it('modifies the sort when the field that is being sorted is modified', () => {
  914. mockedUsedLocation.mockReturnValue(
  915. LocationFixture({
  916. query: {field: ['testField', 'sortField'], sort: ['-sortField']},
  917. })
  918. );
  919. const {result} = renderHook(() => useWidgetBuilderState(), {
  920. wrapper: WidgetBuilderProvider,
  921. });
  922. expect(result.current.state.sort).toEqual([{field: 'sortField', kind: 'desc'}]);
  923. act(() => {
  924. result.current.dispatch({
  925. type: BuilderStateAction.SET_FIELDS,
  926. payload: [
  927. {field: 'testField', kind: FieldValueKind.FIELD},
  928. {field: 'newSortField', kind: FieldValueKind.FIELD},
  929. ],
  930. });
  931. });
  932. expect(result.current.state.sort).toEqual([{field: 'newSortField', kind: 'desc'}]);
  933. });
  934. it('does not reset the table sort for issue widgets', () => {
  935. mockedUsedLocation.mockReturnValue(
  936. LocationFixture({
  937. query: {
  938. dataset: WidgetType.ISSUE,
  939. field: ['testField'],
  940. sort: ['-notInFields'],
  941. },
  942. })
  943. );
  944. const {result} = renderHook(() => useWidgetBuilderState(), {
  945. wrapper: WidgetBuilderProvider,
  946. });
  947. expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
  948. act(() => {
  949. result.current.dispatch({
  950. type: BuilderStateAction.SET_FIELDS,
  951. payload: [{field: 'testField', kind: FieldValueKind.FIELD}],
  952. });
  953. });
  954. expect(result.current.state.sort).toEqual([{field: 'notInFields', kind: 'desc'}]);
  955. });
  956. it('adds a default sort when adding a grouping for a timeseries chart', () => {
  957. mockedUsedLocation.mockReturnValue(
  958. LocationFixture({
  959. query: {
  960. displayType: DisplayType.LINE,
  961. field: [],
  962. yAxis: ['count()'],
  963. },
  964. })
  965. );
  966. const {result} = renderHook(() => useWidgetBuilderState(), {
  967. wrapper: WidgetBuilderProvider,
  968. });
  969. expect(result.current.state.yAxis).toEqual([
  970. {function: ['count', '', undefined, undefined], kind: 'function'},
  971. ]);
  972. act(() => {
  973. result.current.dispatch({
  974. type: BuilderStateAction.SET_FIELDS,
  975. payload: [{field: 'browser.name', kind: FieldValueKind.FIELD}],
  976. });
  977. });
  978. // The y-axis takes priority
  979. expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
  980. });
  981. });
  982. describe('yAxis', () => {
  983. it('does not conflict with fields when setting the state', () => {
  984. mockedUsedLocation.mockReturnValue(
  985. LocationFixture({
  986. query: {
  987. field: ['event.type', 'potato', 'count()'],
  988. yAxis: ['count()', 'count_unique(user)'],
  989. },
  990. })
  991. );
  992. const {result} = renderHook(() => useWidgetBuilderState(), {
  993. wrapper: WidgetBuilderProvider,
  994. });
  995. expect(result.current.state.fields).toEqual([
  996. {field: 'event.type', alias: undefined, kind: 'field'},
  997. {field: 'potato', alias: undefined, kind: 'field'},
  998. {
  999. function: ['count', '', undefined, undefined],
  1000. alias: undefined,
  1001. kind: 'function',
  1002. },
  1003. ]);
  1004. expect(result.current.state.yAxis).toEqual([
  1005. {
  1006. function: ['count', '', undefined, undefined],
  1007. alias: undefined,
  1008. kind: 'function',
  1009. },
  1010. {
  1011. function: ['count_unique', 'user', undefined, undefined],
  1012. alias: undefined,
  1013. kind: 'function',
  1014. },
  1015. ]);
  1016. });
  1017. it('clears the sort when the y-axis changes and there is no grouping', () => {
  1018. mockedUsedLocation.mockReturnValue(
  1019. LocationFixture({
  1020. query: {
  1021. displayType: DisplayType.LINE,
  1022. field: [],
  1023. yAxis: ['count()'],
  1024. sort: ['-count()'],
  1025. },
  1026. })
  1027. );
  1028. const {result} = renderHook(() => useWidgetBuilderState(), {
  1029. wrapper: WidgetBuilderProvider,
  1030. });
  1031. expect(result.current.state.sort).toEqual([{field: 'count()', kind: 'desc'}]);
  1032. act(() => {
  1033. result.current.dispatch({
  1034. type: BuilderStateAction.SET_Y_AXIS,
  1035. payload: [
  1036. {function: ['count_unique', 'user', undefined, undefined], kind: 'function'},
  1037. ],
  1038. });
  1039. });
  1040. expect(result.current.state.sort).toEqual([]);
  1041. });
  1042. });
  1043. describe('sort', () => {
  1044. it('can decode and update sorts', () => {
  1045. mockedUsedLocation.mockReturnValue(
  1046. LocationFixture({
  1047. query: {
  1048. sort: ['-testField'],
  1049. },
  1050. })
  1051. );
  1052. const {result} = renderHook(() => useWidgetBuilderState(), {
  1053. wrapper: WidgetBuilderProvider,
  1054. });
  1055. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'desc'}]);
  1056. act(() => {
  1057. result.current.dispatch({
  1058. type: BuilderStateAction.SET_SORT,
  1059. payload: [{field: 'testField', kind: 'asc'}],
  1060. });
  1061. });
  1062. expect(result.current.state.sort).toEqual([{field: 'testField', kind: 'asc'}]);
  1063. });
  1064. });
  1065. describe('limit', () => {
  1066. it('can decode and update limit', () => {
  1067. mockedUsedLocation.mockReturnValue(
  1068. LocationFixture({
  1069. query: {
  1070. limit: '4',
  1071. },
  1072. })
  1073. );
  1074. const {result} = renderHook(() => useWidgetBuilderState(), {
  1075. wrapper: WidgetBuilderProvider,
  1076. });
  1077. expect(result.current.state.limit).toBe(4);
  1078. act(() => {
  1079. result.current.dispatch({
  1080. type: BuilderStateAction.SET_LIMIT,
  1081. payload: 10,
  1082. });
  1083. });
  1084. expect(result.current.state.limit).toBe(10);
  1085. });
  1086. });
  1087. describe('legendAlias', () => {
  1088. it('can decode and update legendAlias', () => {
  1089. mockedUsedLocation.mockReturnValue(
  1090. LocationFixture({
  1091. query: {
  1092. legendAlias: ['test', 'test2'],
  1093. },
  1094. })
  1095. );
  1096. const {result} = renderHook(() => useWidgetBuilderState(), {
  1097. wrapper: WidgetBuilderProvider,
  1098. });
  1099. expect(result.current.state.legendAlias).toEqual(['test', 'test2']);
  1100. act(() => {
  1101. result.current.dispatch({
  1102. type: BuilderStateAction.SET_LEGEND_ALIAS,
  1103. payload: ['test3', 'test4'],
  1104. });
  1105. });
  1106. expect(result.current.state.legendAlias).toEqual(['test3', 'test4']);
  1107. });
  1108. });
  1109. describe('selectedAggregate', () => {
  1110. it('can decode and update selectedAggregate', () => {
  1111. mockedUsedLocation.mockReturnValue(
  1112. LocationFixture({
  1113. query: {
  1114. selectedAggregate: '0',
  1115. displayType: DisplayType.BIG_NUMBER,
  1116. field: ['count()', 'count_unique(user)'],
  1117. },
  1118. })
  1119. );
  1120. const {result} = renderHook(() => useWidgetBuilderState(), {
  1121. wrapper: WidgetBuilderProvider,
  1122. });
  1123. expect(result.current.state.selectedAggregate).toBe(0);
  1124. act(() => {
  1125. result.current.dispatch({
  1126. type: BuilderStateAction.SET_SELECTED_AGGREGATE,
  1127. payload: 1,
  1128. });
  1129. });
  1130. expect(result.current.state.selectedAggregate).toBe(1);
  1131. });
  1132. it('can set selectedAggregate to undefined in the URL', () => {
  1133. mockedUsedLocation.mockReturnValue(
  1134. LocationFixture({
  1135. query: {
  1136. selectedAggregate: '0',
  1137. displayType: DisplayType.BIG_NUMBER,
  1138. field: ['count()', 'count_unique(user)'],
  1139. },
  1140. })
  1141. );
  1142. const {result} = renderHook(() => useWidgetBuilderState(), {
  1143. wrapper: WidgetBuilderProvider,
  1144. });
  1145. expect(result.current.state.selectedAggregate).toBe(0);
  1146. act(() => {
  1147. result.current.dispatch({
  1148. type: BuilderStateAction.SET_SELECTED_AGGREGATE,
  1149. payload: undefined,
  1150. });
  1151. });
  1152. // If selectedAggregate is undefined in the URL, then the widget builder state
  1153. // will set the selectedAggregate to the last aggregate
  1154. expect(result.current.state.selectedAggregate).toBe(1);
  1155. expect(mockNavigate).toHaveBeenCalledWith(
  1156. expect.objectContaining({
  1157. query: expect.objectContaining({selectedAggregate: undefined}),
  1158. }),
  1159. {replace: true}
  1160. );
  1161. });
  1162. });
  1163. });