releaseWidgetQueries.spec.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {Client} from 'sentry/api';
  4. import {
  5. DashboardFilterKeys,
  6. DisplayType,
  7. WidgetType,
  8. } from 'sentry/views/dashboardsV2/types';
  9. import ReleaseWidgetQueries from 'sentry/views/dashboardsV2/widgetCard/releaseWidgetQueries';
  10. describe('Dashboards > ReleaseWidgetQueries', function () {
  11. const {organization} = initializeOrg();
  12. const badMessage = 'Bad request data';
  13. const multipleQueryWidget = {
  14. title: 'Sessions vs. Users',
  15. interval: '5m',
  16. displayType: DisplayType.LINE,
  17. queries: [
  18. {
  19. conditions: '',
  20. fields: [`sum(session)`],
  21. aggregates: [`sum(session)`],
  22. columns: [],
  23. name: 'sessions',
  24. orderby: '',
  25. },
  26. {
  27. conditions: 'environment:prod',
  28. fields: [`sum(session)`],
  29. aggregates: [`sum(session)`],
  30. columns: [],
  31. name: 'users',
  32. orderby: '',
  33. },
  34. ],
  35. widgetType: WidgetType.RELEASE,
  36. };
  37. const singleQueryWidget = {
  38. title: 'Sessions',
  39. interval: '5m',
  40. displayType: DisplayType.LINE,
  41. queries: [
  42. {
  43. conditions: '',
  44. fields: [`count_unique(user)`],
  45. aggregates: [`count_unique(user)`],
  46. columns: [],
  47. name: 'sessions',
  48. orderby: '-count_unique(user)',
  49. },
  50. ],
  51. widgetType: WidgetType.RELEASE,
  52. };
  53. const selection = {
  54. projects: [1],
  55. environments: ['prod'],
  56. datetime: {
  57. period: '14d',
  58. start: null,
  59. end: null,
  60. utc: null,
  61. },
  62. };
  63. const api = new Client();
  64. afterEach(function () {
  65. MockApiClient.clearMockResponses();
  66. });
  67. it('can send chart requests', async function () {
  68. const mock = MockApiClient.addMockResponse({
  69. url: '/organizations/org-slug/metrics/data/',
  70. body: TestStubs.MetricsField({
  71. field: `sum(sentry.sessions.session)`,
  72. }),
  73. });
  74. const children = jest.fn(() => <div />);
  75. render(
  76. <ReleaseWidgetQueries
  77. api={api}
  78. widget={singleQueryWidget}
  79. organization={organization}
  80. selection={selection}
  81. >
  82. {children}
  83. </ReleaseWidgetQueries>
  84. );
  85. expect(mock).toHaveBeenCalledTimes(1);
  86. await waitFor(() =>
  87. expect(children).toHaveBeenLastCalledWith(
  88. expect.objectContaining({
  89. tableResults: undefined,
  90. timeseriesResults: [
  91. {
  92. data: expect.arrayContaining([
  93. {name: '2021-12-01T16:15:00Z', value: 443.6200417187068},
  94. {name: '2021-12-01T16:30:00Z', value: 471.7512262596214},
  95. {name: '2021-12-01T16:45:00Z', value: 632.5356294251225},
  96. {name: '2021-12-01T17:00:00Z', value: 538.6063865509535},
  97. ]),
  98. seriesName: 'sessions > sum(session)',
  99. },
  100. ],
  101. })
  102. )
  103. );
  104. });
  105. it('fetches release data when sorting on release for metrics api', async function () {
  106. const mockRelease = MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/releases/',
  108. body: [
  109. {
  110. version: 'be1ddfb18126dd2cbde26bfe75488503280e716e',
  111. },
  112. ],
  113. });
  114. const mock = MockApiClient.addMockResponse({
  115. url: '/organizations/org-slug/metrics/data/',
  116. body: TestStubs.MetricsSessionUserCountByStatusByRelease(),
  117. });
  118. const children = jest.fn(() => <div />);
  119. const queries = [
  120. {
  121. conditions: '',
  122. fields: [`count_unique(user)`],
  123. aggregates: [`count_unique(user)`],
  124. columns: ['release'],
  125. name: 'sessions',
  126. orderby: '-release',
  127. },
  128. ];
  129. render(
  130. <ReleaseWidgetQueries
  131. api={api}
  132. widget={{...singleQueryWidget, queries}}
  133. organization={organization}
  134. selection={selection}
  135. >
  136. {children}
  137. </ReleaseWidgetQueries>
  138. );
  139. await waitFor(() => expect(mockRelease).toHaveBeenCalledTimes(1));
  140. expect(mockRelease).toHaveBeenCalledWith(
  141. '/organizations/org-slug/releases/',
  142. expect.objectContaining({
  143. data: {
  144. environment: ['prod'],
  145. per_page: 50,
  146. project: [1],
  147. sort: 'date',
  148. },
  149. })
  150. );
  151. expect(mock).toHaveBeenCalledTimes(1);
  152. expect(mock).toHaveBeenCalledWith(
  153. '/organizations/org-slug/metrics/data/',
  154. expect.objectContaining({
  155. query: {
  156. environment: ['prod'],
  157. field: ['count_unique(sentry.sessions.user)'],
  158. groupBy: ['release'],
  159. includeSeries: 1,
  160. includeTotals: 1,
  161. interval: '12h',
  162. per_page: 100,
  163. project: [1],
  164. query: ' release:be1ddfb18126dd2cbde26bfe75488503280e716e',
  165. statsPeriod: '14d',
  166. },
  167. })
  168. );
  169. });
  170. it('calls session api when session.status is a group by', function () {
  171. const mock = MockApiClient.addMockResponse({
  172. url: '/organizations/org-slug/sessions/',
  173. body: TestStubs.MetricsField({
  174. field: `count_unique(user)`,
  175. }),
  176. });
  177. const children = jest.fn(() => <div />);
  178. const queries = [
  179. {
  180. conditions: '',
  181. fields: [`count_unique(user)`],
  182. aggregates: [`count_unique(user)`],
  183. columns: ['session.status'],
  184. name: 'sessions',
  185. orderby: '-count_unique(user)',
  186. },
  187. ];
  188. render(
  189. <ReleaseWidgetQueries
  190. api={api}
  191. widget={{...singleQueryWidget, queries}}
  192. organization={organization}
  193. selection={selection}
  194. >
  195. {children}
  196. </ReleaseWidgetQueries>
  197. );
  198. expect(mock).toHaveBeenCalledWith(
  199. '/organizations/org-slug/sessions/',
  200. expect.objectContaining({
  201. query: {
  202. environment: ['prod'],
  203. field: ['count_unique(user)'],
  204. groupBy: ['session.status'],
  205. interval: '30m',
  206. project: [1],
  207. statsPeriod: '14d',
  208. },
  209. })
  210. );
  211. });
  212. it('appends dashboard filters to releases request', async function () {
  213. const mock = MockApiClient.addMockResponse({
  214. url: '/organizations/org-slug/metrics/data/',
  215. body: TestStubs.MetricsSessionUserCountByStatusByRelease(),
  216. });
  217. render(
  218. <ReleaseWidgetQueries
  219. api={api}
  220. widget={singleQueryWidget}
  221. organization={organization}
  222. selection={selection}
  223. dashboardFilters={{[DashboardFilterKeys.RELEASE]: ['abc@1.3.0']}}
  224. >
  225. {() => <div data-test-id="child" />}
  226. </ReleaseWidgetQueries>
  227. );
  228. await screen.findByTestId('child');
  229. expect(mock).toHaveBeenCalledWith(
  230. '/organizations/org-slug/metrics/data/',
  231. expect.objectContaining({
  232. query: expect.objectContaining({
  233. query: ' release:abc@1.3.0 ',
  234. }),
  235. })
  236. );
  237. });
  238. it('strips injected sort columns', async function () {
  239. const mock = MockApiClient.addMockResponse({
  240. url: '/organizations/org-slug/metrics/data/',
  241. body: TestStubs.MetricsSessionUserCountByStatusByRelease(),
  242. });
  243. const children = jest.fn(() => <div />);
  244. const injectedOrderby = {
  245. title: 'Sessions',
  246. interval: '5m',
  247. displayType: DisplayType.LINE,
  248. queries: [
  249. {
  250. conditions: '',
  251. fields: [`sum(session)`],
  252. aggregates: [`sum(session)`],
  253. columns: [],
  254. name: 'sessions',
  255. orderby: '-count_unique(user)',
  256. },
  257. ],
  258. widgetType: WidgetType.RELEASE,
  259. };
  260. render(
  261. <ReleaseWidgetQueries
  262. api={api}
  263. widget={injectedOrderby}
  264. organization={organization}
  265. selection={selection}
  266. >
  267. {children}
  268. </ReleaseWidgetQueries>
  269. );
  270. expect(mock).toHaveBeenCalledTimes(1);
  271. await waitFor(() =>
  272. expect(children).toHaveBeenLastCalledWith(
  273. expect.objectContaining({
  274. errorMessage: undefined,
  275. loading: false,
  276. timeseriesResults: [
  277. {
  278. data: [
  279. {name: '2022-01-15T00:00:00Z', value: 0},
  280. {name: '2022-01-16T00:00:00Z', value: 0},
  281. {name: '2022-01-17T00:00:00Z', value: 0},
  282. {name: '2022-01-18T00:00:00Z', value: 0},
  283. {name: '2022-01-19T00:00:00Z', value: 0},
  284. {name: '2022-01-20T00:00:00Z', value: 0},
  285. {name: '2022-01-21T00:00:00Z', value: 0},
  286. {name: '2022-01-22T00:00:00Z', value: 0},
  287. {name: '2022-01-23T00:00:00Z', value: 0},
  288. {name: '2022-01-24T00:00:00Z', value: 23},
  289. {name: '2022-01-25T00:00:00Z', value: 11},
  290. {name: '2022-01-26T00:00:00Z', value: 0},
  291. {name: '2022-01-27T00:00:00Z', value: 0},
  292. {name: '2022-01-28T00:00:00Z', value: 0},
  293. ],
  294. seriesName: 'sessions > crashed, 1 : sum(session)',
  295. },
  296. {
  297. data: [
  298. {name: '2022-01-15T00:00:00Z', value: 1},
  299. {name: '2022-01-16T00:00:00Z', value: 0},
  300. {name: '2022-01-17T00:00:00Z', value: 0},
  301. {name: '2022-01-18T00:00:00Z', value: 0},
  302. {name: '2022-01-19T00:00:00Z', value: 0},
  303. {name: '2022-01-20T00:00:00Z', value: 0},
  304. {name: '2022-01-21T00:00:00Z', value: 0},
  305. {name: '2022-01-22T00:00:00Z', value: 0},
  306. {name: '2022-01-23T00:00:00Z', value: 0},
  307. {name: '2022-01-24T00:00:00Z', value: 0},
  308. {name: '2022-01-25T00:00:00Z', value: 0},
  309. {name: '2022-01-26T00:00:00Z', value: 0},
  310. {name: '2022-01-27T00:00:00Z', value: 0},
  311. {name: '2022-01-28T00:00:00Z', value: 0},
  312. ],
  313. seriesName: 'sessions > abnormal, 1 : sum(session)',
  314. },
  315. {
  316. data: [
  317. {name: '2022-01-15T00:00:00Z', value: 0},
  318. {name: '2022-01-16T00:00:00Z', value: 0},
  319. {name: '2022-01-17T00:00:00Z', value: 0},
  320. {name: '2022-01-18T00:00:00Z', value: 0},
  321. {name: '2022-01-19T00:00:00Z', value: 0},
  322. {name: '2022-01-20T00:00:00Z', value: 37},
  323. {name: '2022-01-21T00:00:00Z', value: 0},
  324. {name: '2022-01-22T00:00:00Z', value: 0},
  325. {name: '2022-01-23T00:00:00Z', value: 0},
  326. {name: '2022-01-24T00:00:00Z', value: 335},
  327. {name: '2022-01-25T00:00:00Z', value: 79},
  328. {name: '2022-01-26T00:00:00Z', value: 0},
  329. {name: '2022-01-27T00:00:00Z', value: 0},
  330. {name: '2022-01-28T00:00:00Z', value: 0},
  331. ],
  332. seriesName: 'sessions > errored, 1 : sum(session)',
  333. },
  334. {
  335. data: [
  336. {name: '2022-01-15T00:00:00Z', value: 0},
  337. {name: '2022-01-16T00:00:00Z', value: 0},
  338. {name: '2022-01-17T00:00:00Z', value: 0},
  339. {name: '2022-01-18T00:00:00Z', value: 0},
  340. {name: '2022-01-19T00:00:00Z', value: 0},
  341. {name: '2022-01-20T00:00:00Z', value: 2503},
  342. {name: '2022-01-21T00:00:00Z', value: 661},
  343. {name: '2022-01-22T00:00:00Z', value: 0},
  344. {name: '2022-01-23T00:00:00Z', value: 0},
  345. {name: '2022-01-24T00:00:00Z', value: 1464},
  346. {name: '2022-01-25T00:00:00Z', value: 430},
  347. {name: '2022-01-26T00:00:00Z', value: 0},
  348. {name: '2022-01-27T00:00:00Z', value: 0},
  349. {name: '2022-01-28T00:00:00Z', value: 0},
  350. ],
  351. seriesName: 'sessions > healthy, 1 : sum(session)',
  352. },
  353. {
  354. data: [
  355. {name: '2022-01-15T00:00:00Z', value: 1},
  356. {name: '2022-01-16T00:00:00Z', value: 0},
  357. {name: '2022-01-17T00:00:00Z', value: 0},
  358. {name: '2022-01-18T00:00:00Z', value: 0},
  359. {name: '2022-01-19T00:00:00Z', value: 0},
  360. {name: '2022-01-20T00:00:00Z', value: 0},
  361. {name: '2022-01-21T00:00:00Z', value: 0},
  362. {name: '2022-01-22T00:00:00Z', value: 0},
  363. {name: '2022-01-23T00:00:00Z', value: 0},
  364. {name: '2022-01-24T00:00:00Z', value: 23},
  365. {name: '2022-01-25T00:00:00Z', value: 11},
  366. {name: '2022-01-26T00:00:00Z', value: 0},
  367. {name: '2022-01-27T00:00:00Z', value: 0},
  368. {name: '2022-01-28T00:00:00Z', value: 0},
  369. ],
  370. seriesName: 'sessions > crashed, 2 : sum(session)',
  371. },
  372. {
  373. data: [
  374. {name: '2022-01-15T00:00:00Z', value: 1},
  375. {name: '2022-01-16T00:00:00Z', value: 0},
  376. {name: '2022-01-17T00:00:00Z', value: 0},
  377. {name: '2022-01-18T00:00:00Z', value: 0},
  378. {name: '2022-01-19T00:00:00Z', value: 0},
  379. {name: '2022-01-20T00:00:00Z', value: 0},
  380. {name: '2022-01-21T00:00:00Z', value: 0},
  381. {name: '2022-01-22T00:00:00Z', value: 0},
  382. {name: '2022-01-23T00:00:00Z', value: 0},
  383. {name: '2022-01-24T00:00:00Z', value: 0},
  384. {name: '2022-01-25T00:00:00Z', value: 0},
  385. {name: '2022-01-26T00:00:00Z', value: 0},
  386. {name: '2022-01-27T00:00:00Z', value: 0},
  387. {name: '2022-01-28T00:00:00Z', value: 0},
  388. ],
  389. seriesName: 'sessions > abnormal, 2 : sum(session)',
  390. },
  391. {
  392. data: [
  393. {name: '2022-01-15T00:00:00Z', value: 1},
  394. {name: '2022-01-16T00:00:00Z', value: 0},
  395. {name: '2022-01-17T00:00:00Z', value: 0},
  396. {name: '2022-01-18T00:00:00Z', value: 0},
  397. {name: '2022-01-19T00:00:00Z', value: 0},
  398. {name: '2022-01-20T00:00:00Z', value: 37},
  399. {name: '2022-01-21T00:00:00Z', value: 0},
  400. {name: '2022-01-22T00:00:00Z', value: 0},
  401. {name: '2022-01-23T00:00:00Z', value: 0},
  402. {name: '2022-01-24T00:00:00Z', value: 335},
  403. {name: '2022-01-25T00:00:00Z', value: 79},
  404. {name: '2022-01-26T00:00:00Z', value: 0},
  405. {name: '2022-01-27T00:00:00Z', value: 0},
  406. {name: '2022-01-28T00:00:00Z', value: 0},
  407. ],
  408. seriesName: 'sessions > errored, 2 : sum(session)',
  409. },
  410. {
  411. data: [
  412. {name: '2022-01-15T00:00:00Z', value: 1},
  413. {name: '2022-01-16T00:00:00Z', value: 0},
  414. {name: '2022-01-17T00:00:00Z', value: 0},
  415. {name: '2022-01-18T00:00:00Z', value: 0},
  416. {name: '2022-01-19T00:00:00Z', value: 0},
  417. {name: '2022-01-20T00:00:00Z', value: 2503},
  418. {name: '2022-01-21T00:00:00Z', value: 661},
  419. {name: '2022-01-22T00:00:00Z', value: 0},
  420. {name: '2022-01-23T00:00:00Z', value: 0},
  421. {name: '2022-01-24T00:00:00Z', value: 1464},
  422. {name: '2022-01-25T00:00:00Z', value: 430},
  423. {name: '2022-01-26T00:00:00Z', value: 0},
  424. {name: '2022-01-27T00:00:00Z', value: 0},
  425. {name: '2022-01-28T00:00:00Z', value: 0},
  426. ],
  427. seriesName: 'sessions > healthy, 2 : sum(session)',
  428. },
  429. ],
  430. })
  431. )
  432. );
  433. });
  434. it('can send table requests', async function () {
  435. const mock = MockApiClient.addMockResponse({
  436. url: '/organizations/org-slug/metrics/data/',
  437. body: TestStubs.MetricsSessionUserCountByStatusByRelease(),
  438. });
  439. const children = jest.fn(() => <div />);
  440. render(
  441. <ReleaseWidgetQueries
  442. api={api}
  443. widget={{...singleQueryWidget, displayType: DisplayType.TABLE}}
  444. organization={organization}
  445. selection={selection}
  446. >
  447. {children}
  448. </ReleaseWidgetQueries>
  449. );
  450. expect(mock).toHaveBeenCalledTimes(1);
  451. await waitFor(() =>
  452. expect(children).toHaveBeenLastCalledWith(
  453. expect.objectContaining({
  454. errorMessage: undefined,
  455. loading: false,
  456. tableResults: [
  457. {
  458. data: [
  459. {
  460. 'count_unique(user)': 1,
  461. id: '0',
  462. release: '1',
  463. 'session.status': 'crashed',
  464. 'sum(session)': 34,
  465. },
  466. {
  467. 'count_unique(user)': 1,
  468. id: '1',
  469. release: '1',
  470. 'session.status': 'abnormal',
  471. 'sum(session)': 1,
  472. },
  473. {
  474. 'count_unique(user)': 2,
  475. id: '2',
  476. release: '1',
  477. 'session.status': 'errored',
  478. 'sum(session)': 451,
  479. },
  480. {
  481. 'count_unique(user)': 3,
  482. id: '3',
  483. release: '1',
  484. 'session.status': 'healthy',
  485. 'sum(session)': 5058,
  486. },
  487. {
  488. 'count_unique(user)': 2,
  489. id: '4',
  490. release: '2',
  491. 'session.status': 'crashed',
  492. 'sum(session)': 35,
  493. },
  494. {
  495. 'count_unique(user)': 1,
  496. id: '5',
  497. release: '2',
  498. 'session.status': 'abnormal',
  499. 'sum(session)': 1,
  500. },
  501. {
  502. 'count_unique(user)': 1,
  503. id: '6',
  504. release: '2',
  505. 'session.status': 'errored',
  506. 'sum(session)': 452,
  507. },
  508. {
  509. 'count_unique(user)': 10,
  510. id: '7',
  511. release: '2',
  512. 'session.status': 'healthy',
  513. 'sum(session)': 5059,
  514. },
  515. ],
  516. meta: {
  517. 'count_unique(user)': 'integer',
  518. release: 'string',
  519. 'session.status': 'string',
  520. 'sum(session)': 'integer',
  521. },
  522. title: 'sessions',
  523. },
  524. ],
  525. timeseriesResults: undefined,
  526. })
  527. )
  528. );
  529. });
  530. it('can send big number requests', async function () {
  531. const mock = MockApiClient.addMockResponse({
  532. url: '/organizations/org-slug/metrics/data/',
  533. body: TestStubs.MetricsField({
  534. field: `count_unique(sentry.sessions.user)`,
  535. }),
  536. });
  537. const children = jest.fn(() => <div />);
  538. render(
  539. <ReleaseWidgetQueries
  540. api={api}
  541. widget={{...singleQueryWidget, displayType: DisplayType.BIG_NUMBER}}
  542. organization={organization}
  543. selection={selection}
  544. >
  545. {children}
  546. </ReleaseWidgetQueries>
  547. );
  548. expect(mock).toHaveBeenCalledTimes(1);
  549. expect(mock).toHaveBeenCalledWith(
  550. expect.anything(),
  551. expect.objectContaining({
  552. query: expect.objectContaining({
  553. field: ['count_unique(sentry.sessions.user)'],
  554. orderBy: '-count_unique(sentry.sessions.user)',
  555. }),
  556. })
  557. );
  558. await waitFor(() =>
  559. expect(children).toHaveBeenLastCalledWith(
  560. expect.objectContaining({
  561. loading: false,
  562. tableResults: [
  563. {
  564. data: [{'count_unique(user)': 51292.95404741901, id: '0'}],
  565. meta: {'count_unique(user)': 'integer'},
  566. title: 'sessions',
  567. },
  568. ],
  569. })
  570. )
  571. );
  572. });
  573. it('can send multiple API requests', function () {
  574. const metricsMock = MockApiClient.addMockResponse({
  575. url: '/organizations/org-slug/metrics/data/',
  576. body: TestStubs.SessionsField({
  577. field: `sum(sentry.sessions.session)`,
  578. }),
  579. match: [
  580. MockApiClient.matchQuery({
  581. field: [`sum(sentry.sessions.session)`],
  582. }),
  583. ],
  584. });
  585. render(
  586. <ReleaseWidgetQueries
  587. api={api}
  588. widget={multipleQueryWidget}
  589. organization={organization}
  590. selection={selection}
  591. >
  592. {() => <div data-test-id="child" />}
  593. </ReleaseWidgetQueries>
  594. );
  595. // Child should be rendered and 2 requests should be sent.
  596. expect(screen.getByTestId('child')).toBeInTheDocument();
  597. expect(metricsMock).toHaveBeenCalledTimes(2);
  598. expect(metricsMock).toHaveBeenNthCalledWith(
  599. 1,
  600. '/organizations/org-slug/metrics/data/',
  601. expect.objectContaining({
  602. query: {
  603. environment: ['prod'],
  604. field: ['sum(sentry.sessions.session)'],
  605. groupBy: [],
  606. interval: '30m',
  607. project: [1],
  608. statsPeriod: '14d',
  609. per_page: 1,
  610. includeSeries: 1,
  611. includeTotals: 0,
  612. },
  613. })
  614. );
  615. expect(metricsMock).toHaveBeenNthCalledWith(
  616. 2,
  617. '/organizations/org-slug/metrics/data/',
  618. expect.objectContaining({
  619. query: {
  620. environment: ['prod'],
  621. field: ['sum(sentry.sessions.session)'],
  622. groupBy: [],
  623. interval: '30m',
  624. project: [1],
  625. query: 'environment:prod',
  626. statsPeriod: '14d',
  627. per_page: 1,
  628. includeSeries: 1,
  629. includeTotals: 0,
  630. },
  631. })
  632. );
  633. });
  634. it('sets errorMessage when the first request fails', async function () {
  635. const failMock = MockApiClient.addMockResponse({
  636. url: '/organizations/org-slug/metrics/data/',
  637. statusCode: 400,
  638. body: {detail: badMessage},
  639. match: [
  640. MockApiClient.matchQuery({
  641. field: [`sum(sentry.sessions.session)`],
  642. }),
  643. ],
  644. });
  645. const children = jest.fn(() => <div data-test-id="child" />);
  646. render(
  647. <ReleaseWidgetQueries
  648. api={api}
  649. widget={multipleQueryWidget}
  650. organization={organization}
  651. selection={selection}
  652. >
  653. {children}
  654. </ReleaseWidgetQueries>
  655. );
  656. // Child should be rendered and 2 requests should be sent.
  657. expect(screen.getByTestId('child')).toBeInTheDocument();
  658. expect(failMock).toHaveBeenCalledTimes(2);
  659. await waitFor(() =>
  660. expect(children).toHaveBeenLastCalledWith(
  661. expect.objectContaining({errorMessage: badMessage})
  662. )
  663. );
  664. });
  665. it('adjusts interval based on date window', function () {
  666. const mock = MockApiClient.addMockResponse({
  667. url: '/organizations/org-slug/metrics/data/',
  668. body: TestStubs.SessionsField({
  669. field: `sum(sentry.sessions.session)`,
  670. }),
  671. });
  672. render(
  673. <ReleaseWidgetQueries
  674. api={api}
  675. widget={{...singleQueryWidget, interval: '1m'}}
  676. organization={organization}
  677. selection={{...selection, datetime: {...selection.datetime, period: '90d'}}}
  678. >
  679. {() => <div data-test-id="child" />}
  680. </ReleaseWidgetQueries>
  681. );
  682. expect(screen.getByTestId('child')).toBeInTheDocument();
  683. expect(mock).toHaveBeenCalledTimes(1);
  684. expect(mock).toHaveBeenCalledWith(
  685. expect.anything(),
  686. expect.objectContaining({
  687. query: expect.objectContaining({
  688. interval: '4h',
  689. statsPeriod: '90d',
  690. environment: ['prod'],
  691. project: [1],
  692. }),
  693. })
  694. );
  695. });
  696. it('does not re-fetch when renaming legend alias / adding falsy fields', () => {
  697. const mock = MockApiClient.addMockResponse({
  698. url: '/organizations/org-slug/metrics/data/',
  699. body: TestStubs.SessionsField({
  700. field: `sum(sentry.sessions.session)`,
  701. }),
  702. });
  703. const children = jest.fn(() => <div />);
  704. const {rerender} = render(
  705. <ReleaseWidgetQueries
  706. api={api}
  707. widget={singleQueryWidget}
  708. organization={organization}
  709. selection={selection}
  710. >
  711. {children}
  712. </ReleaseWidgetQueries>
  713. );
  714. expect(mock).toHaveBeenCalledTimes(1);
  715. rerender(
  716. <ReleaseWidgetQueries
  717. api={api}
  718. widget={{
  719. ...singleQueryWidget,
  720. queries: [
  721. {
  722. ...singleQueryWidget.queries[0],
  723. name: 'New Legend Alias',
  724. fields: [...singleQueryWidget.queries[0].fields, ''],
  725. },
  726. ],
  727. }}
  728. organization={organization}
  729. selection={selection}
  730. >
  731. {children}
  732. </ReleaseWidgetQueries>
  733. );
  734. // no additional request has been sent, the total count of requests is still 1
  735. expect(mock).toHaveBeenCalledTimes(1);
  736. });
  737. });