index.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import {act, render} from 'sentry-test/reactTestingLibrary';
  2. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  3. import {
  4. PageParamsProvider,
  5. useExplorePageParams,
  6. useSetExploreDataset,
  7. useSetExploreFields,
  8. useSetExploreGroupBys,
  9. useSetExploreMode,
  10. useSetExplorePageParams,
  11. useSetExploreQuery,
  12. useSetExploreSortBys,
  13. useSetExploreVisualizes,
  14. } from 'sentry/views/explore/contexts/pageParamsContext';
  15. import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
  16. import {ChartType} from 'sentry/views/insights/common/components/chart';
  17. describe('PageParamsProvider', function () {
  18. let pageParams: ReturnType<typeof useExplorePageParams>;
  19. let setPageParams: ReturnType<typeof useSetExplorePageParams>;
  20. let setDataset: ReturnType<typeof useSetExploreDataset>;
  21. let setFields: ReturnType<typeof useSetExploreFields>;
  22. let setGroupBys: ReturnType<typeof useSetExploreGroupBys>;
  23. let setMode: ReturnType<typeof useSetExploreMode>;
  24. let setQuery: ReturnType<typeof useSetExploreQuery>;
  25. let setSortBys: ReturnType<typeof useSetExploreSortBys>;
  26. let setVisualizes: ReturnType<typeof useSetExploreVisualizes>;
  27. function Component() {
  28. pageParams = useExplorePageParams();
  29. setPageParams = useSetExplorePageParams();
  30. setDataset = useSetExploreDataset();
  31. setFields = useSetExploreFields();
  32. setGroupBys = useSetExploreGroupBys();
  33. setMode = useSetExploreMode();
  34. setQuery = useSetExploreQuery();
  35. setSortBys = useSetExploreSortBys();
  36. setVisualizes = useSetExploreVisualizes();
  37. return <br />;
  38. }
  39. function renderTestComponent(defaultPageParams?: any) {
  40. render(
  41. <PageParamsProvider>
  42. <Component />
  43. </PageParamsProvider>,
  44. {disableRouterMocks: true}
  45. );
  46. act(() =>
  47. setPageParams({
  48. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  49. fields: ['id', 'timestamp'],
  50. groupBys: ['span.op'],
  51. mode: Mode.AGGREGATE,
  52. query: '',
  53. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  54. visualizes: [
  55. {
  56. chartType: ChartType.AREA,
  57. label: 'A',
  58. yAxes: ['count(span.self_time)'],
  59. },
  60. ],
  61. ...defaultPageParams,
  62. })
  63. );
  64. }
  65. it('has expected default', function () {
  66. render(
  67. <PageParamsProvider>
  68. <Component />
  69. </PageParamsProvider>,
  70. {disableRouterMocks: true}
  71. );
  72. expect(pageParams).toEqual({
  73. dataset: undefined,
  74. fields: [
  75. 'id',
  76. 'span.op',
  77. 'span.description',
  78. 'span.duration',
  79. 'transaction',
  80. 'timestamp',
  81. ],
  82. groupBys: ['span.op'],
  83. mode: Mode.SAMPLES,
  84. query: '',
  85. sortBys: [{field: 'timestamp', kind: 'desc'}],
  86. visualizes: [
  87. {
  88. chartType: ChartType.LINE,
  89. label: 'A',
  90. yAxes: ['avg(span.duration)'],
  91. },
  92. ],
  93. });
  94. });
  95. it('correctly updates dataset', function () {
  96. renderTestComponent();
  97. act(() => setDataset(DiscoverDatasets.SPANS_EAP));
  98. expect(pageParams).toEqual({
  99. dataset: DiscoverDatasets.SPANS_EAP,
  100. fields: ['id', 'timestamp'],
  101. groupBys: ['span.op'],
  102. mode: Mode.AGGREGATE,
  103. query: '',
  104. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  105. visualizes: [
  106. {
  107. chartType: ChartType.AREA,
  108. label: 'A',
  109. yAxes: ['count(span.self_time)'],
  110. },
  111. ],
  112. });
  113. });
  114. it('correctly updates fields', function () {
  115. renderTestComponent();
  116. act(() => setFields(['id', 'span.op', 'timestamp']));
  117. expect(pageParams).toEqual({
  118. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  119. fields: ['id', 'span.op', 'timestamp'],
  120. groupBys: ['span.op'],
  121. mode: Mode.AGGREGATE,
  122. query: '',
  123. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  124. visualizes: [
  125. {
  126. chartType: ChartType.AREA,
  127. label: 'A',
  128. yAxes: ['count(span.self_time)'],
  129. },
  130. ],
  131. });
  132. });
  133. it('correctly updates groupBys', function () {
  134. renderTestComponent();
  135. act(() => setGroupBys(['browser.name', 'sdk.name']));
  136. expect(pageParams).toEqual({
  137. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  138. fields: ['id', 'timestamp'],
  139. groupBys: ['browser.name', 'sdk.name'],
  140. mode: Mode.AGGREGATE,
  141. query: '',
  142. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  143. visualizes: [
  144. {
  145. chartType: ChartType.AREA,
  146. label: 'A',
  147. yAxes: ['count(span.self_time)'],
  148. },
  149. ],
  150. });
  151. });
  152. it('correctly gives default for empty groupBys', function () {
  153. renderTestComponent();
  154. act(() => setGroupBys([]));
  155. expect(pageParams).toEqual({
  156. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  157. fields: ['id', 'timestamp'],
  158. groupBys: ['span.op'],
  159. mode: Mode.AGGREGATE,
  160. query: '',
  161. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  162. visualizes: [
  163. {
  164. chartType: ChartType.AREA,
  165. label: 'A',
  166. yAxes: ['count(span.self_time)'],
  167. },
  168. ],
  169. });
  170. });
  171. it('permits ungrouped', function () {
  172. renderTestComponent();
  173. act(() => setGroupBys(['']));
  174. expect(pageParams).toEqual({
  175. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  176. fields: ['id', 'timestamp'],
  177. groupBys: [''],
  178. mode: Mode.AGGREGATE,
  179. query: '',
  180. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  181. visualizes: [
  182. {
  183. chartType: ChartType.AREA,
  184. label: 'A',
  185. yAxes: ['count(span.self_time)'],
  186. },
  187. ],
  188. });
  189. });
  190. it('correctly updates mode from samples to aggregates', function () {
  191. renderTestComponent({mode: Mode.SAMPLES});
  192. act(() => setMode(Mode.AGGREGATE));
  193. expect(pageParams).toEqual({
  194. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  195. fields: ['id', 'timestamp'],
  196. groupBys: ['span.op'],
  197. mode: Mode.AGGREGATE,
  198. query: '',
  199. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  200. visualizes: [
  201. {
  202. chartType: ChartType.AREA,
  203. label: 'A',
  204. yAxes: ['count(span.self_time)'],
  205. },
  206. ],
  207. });
  208. });
  209. it('correctly updates mode from aggregates to sample without group bys', function () {
  210. renderTestComponent({mode: Mode.AGGREGATE, groupBys: [''], sortBys: null});
  211. act(() => setMode(Mode.SAMPLES));
  212. expect(pageParams).toEqual({
  213. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  214. fields: ['id', 'timestamp'],
  215. groupBys: [''],
  216. mode: Mode.SAMPLES,
  217. query: '',
  218. sortBys: [{field: 'timestamp', kind: 'desc'}],
  219. visualizes: [
  220. {
  221. chartType: ChartType.AREA,
  222. label: 'A',
  223. yAxes: ['count(span.self_time)'],
  224. },
  225. ],
  226. });
  227. });
  228. it('correctly updates mode from aggregates to sample with group bys', function () {
  229. renderTestComponent({
  230. mode: Mode.AGGREGATE,
  231. sortBys: null,
  232. fields: ['id', 'sdk.name', 'sdk.version', 'timestamp'],
  233. groupBys: ['sdk.name', 'sdk.version', 'span.op', ''],
  234. });
  235. act(() => setMode(Mode.SAMPLES));
  236. expect(pageParams).toEqual({
  237. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  238. fields: ['id', 'sdk.name', 'sdk.version', 'timestamp', 'span.op'],
  239. groupBys: ['sdk.name', 'sdk.version', 'span.op', ''],
  240. mode: Mode.SAMPLES,
  241. query: '',
  242. sortBys: [{field: 'timestamp', kind: 'desc'}],
  243. visualizes: [
  244. {
  245. chartType: ChartType.AREA,
  246. label: 'A',
  247. yAxes: ['count(span.self_time)'],
  248. },
  249. ],
  250. });
  251. });
  252. it('correctly updates query', function () {
  253. renderTestComponent();
  254. act(() => setQuery('foo:bar'));
  255. expect(pageParams).toEqual({
  256. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  257. fields: ['id', 'timestamp'],
  258. groupBys: ['span.op'],
  259. mode: Mode.AGGREGATE,
  260. query: 'foo:bar',
  261. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  262. visualizes: [
  263. {
  264. chartType: ChartType.AREA,
  265. label: 'A',
  266. yAxes: ['count(span.self_time)'],
  267. },
  268. ],
  269. });
  270. });
  271. it('correctly updates sort bys in samples mode with known field', function () {
  272. renderTestComponent({mode: Mode.SAMPLES});
  273. act(() => setSortBys([{field: 'id', kind: 'desc'}]));
  274. expect(pageParams).toEqual({
  275. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  276. fields: ['id', 'timestamp'],
  277. groupBys: ['span.op'],
  278. mode: Mode.SAMPLES,
  279. query: '',
  280. sortBys: [{field: 'id', kind: 'desc'}],
  281. visualizes: [
  282. {
  283. chartType: ChartType.AREA,
  284. label: 'A',
  285. yAxes: ['count(span.self_time)'],
  286. },
  287. ],
  288. });
  289. });
  290. it('correctly updates sort bys in samples mode with unknown field', function () {
  291. renderTestComponent({mode: Mode.SAMPLES});
  292. act(() => setSortBys([{field: 'span.op', kind: 'desc'}]));
  293. expect(pageParams).toEqual({
  294. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  295. fields: ['id', 'timestamp'],
  296. groupBys: ['span.op'],
  297. mode: Mode.SAMPLES,
  298. query: '',
  299. sortBys: [{field: 'timestamp', kind: 'desc'}],
  300. visualizes: [
  301. {
  302. chartType: ChartType.AREA,
  303. label: 'A',
  304. yAxes: ['count(span.self_time)'],
  305. },
  306. ],
  307. });
  308. });
  309. it('correctly updates sort bys in aggregates mode with known y axis', function () {
  310. renderTestComponent({
  311. mode: Mode.AGGREGATE,
  312. visualizes: [
  313. {
  314. chartType: ChartType.AREA,
  315. label: 'A',
  316. yAxes: ['min(span.self_time)', 'max(span.duration)'],
  317. },
  318. ],
  319. });
  320. act(() => setSortBys([{field: 'max(span.duration)', kind: 'desc'}]));
  321. expect(pageParams).toEqual({
  322. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  323. fields: ['id', 'timestamp'],
  324. groupBys: ['span.op'],
  325. mode: Mode.AGGREGATE,
  326. query: '',
  327. sortBys: [{field: 'max(span.duration)', kind: 'desc'}],
  328. visualizes: [
  329. {
  330. chartType: ChartType.AREA,
  331. label: 'A',
  332. yAxes: ['min(span.self_time)', 'max(span.duration)'],
  333. },
  334. ],
  335. });
  336. });
  337. it('correctly updates sort bys in aggregates mode with unknown y axis', function () {
  338. renderTestComponent({
  339. mode: Mode.AGGREGATE,
  340. visualizes: [
  341. {
  342. chartType: ChartType.AREA,
  343. label: 'A',
  344. yAxes: ['min(span.self_time)', 'max(span.duration)'],
  345. },
  346. ],
  347. });
  348. act(() => setSortBys([{field: 'avg(span.duration)', kind: 'desc'}]));
  349. expect(pageParams).toEqual({
  350. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  351. fields: ['id', 'timestamp'],
  352. groupBys: ['span.op'],
  353. mode: Mode.AGGREGATE,
  354. query: '',
  355. sortBys: [{field: 'min(span.self_time)', kind: 'desc'}],
  356. visualizes: [
  357. {
  358. chartType: ChartType.AREA,
  359. label: 'A',
  360. yAxes: ['min(span.self_time)', 'max(span.duration)'],
  361. },
  362. ],
  363. });
  364. });
  365. it('correctly updates sort bys in aggregates mode with known group by', function () {
  366. renderTestComponent({mode: Mode.AGGREGATE, groupBys: ['sdk.name']});
  367. act(() => setSortBys([{field: 'sdk.name', kind: 'desc'}]));
  368. expect(pageParams).toEqual({
  369. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  370. fields: ['id', 'timestamp'],
  371. groupBys: ['sdk.name'],
  372. mode: Mode.AGGREGATE,
  373. query: '',
  374. sortBys: [{field: 'sdk.name', kind: 'desc'}],
  375. visualizes: [
  376. {
  377. chartType: ChartType.AREA,
  378. label: 'A',
  379. yAxes: ['count(span.self_time)'],
  380. },
  381. ],
  382. });
  383. });
  384. it('correctly updates sort bys in aggregates mode with unknown group by', function () {
  385. renderTestComponent({mode: Mode.AGGREGATE, groupBys: ['sdk.name']});
  386. act(() => setSortBys([{field: 'sdk.version', kind: 'desc'}]));
  387. expect(pageParams).toEqual({
  388. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  389. fields: ['id', 'timestamp'],
  390. groupBys: ['sdk.name'],
  391. mode: Mode.AGGREGATE,
  392. query: '',
  393. sortBys: [{field: 'count(span.self_time)', kind: 'desc'}],
  394. visualizes: [
  395. {
  396. chartType: ChartType.AREA,
  397. label: 'A',
  398. yAxes: ['count(span.self_time)'],
  399. },
  400. ],
  401. });
  402. });
  403. it('correctly updates visualizes with labels', function () {
  404. renderTestComponent();
  405. act(() =>
  406. setVisualizes([
  407. {
  408. chartType: ChartType.AREA,
  409. yAxes: ['count(span.self_time)'],
  410. },
  411. {
  412. chartType: ChartType.LINE,
  413. yAxes: ['avg(span.duration)', 'avg(span.self_time)'],
  414. },
  415. ])
  416. );
  417. expect(pageParams).toEqual({
  418. dataset: DiscoverDatasets.SPANS_EAP_RPC,
  419. fields: ['id', 'timestamp'],
  420. groupBys: ['span.op'],
  421. mode: Mode.AGGREGATE,
  422. query: '',
  423. sortBys: [{field: 'count(span.self_time)', kind: 'asc'}],
  424. visualizes: [
  425. {
  426. chartType: ChartType.AREA,
  427. label: 'A',
  428. yAxes: ['count(span.self_time)'],
  429. },
  430. {
  431. chartType: ChartType.LINE,
  432. label: 'B',
  433. yAxes: ['avg(span.duration)', 'avg(span.self_time)'],
  434. },
  435. ],
  436. });
  437. });
  438. });