releaseWidgetQueries.spec.tsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import {
  4. DashboardFilterKeys,
  5. DisplayType,
  6. WidgetType,
  7. } from 'sentry/views/dashboards/types';
  8. import ReleaseWidgetQueries from 'sentry/views/dashboards/widgetCard/releaseWidgetQueries';
  9. describe('Dashboards > ReleaseWidgetQueries', function () {
  10. const {organization} = initializeOrg();
  11. const badMessage = 'Bad request data';
  12. const multipleQueryWidget = {
  13. title: 'Sessions vs. Users',
  14. interval: '5m',
  15. displayType: DisplayType.LINE,
  16. queries: [
  17. {
  18. conditions: '',
  19. fields: [`sum(session)`],
  20. aggregates: [`sum(session)`],
  21. columns: [],
  22. name: 'sessions',
  23. orderby: '',
  24. },
  25. {
  26. conditions: 'environment:prod',
  27. fields: [`sum(session)`],
  28. aggregates: [`sum(session)`],
  29. columns: [],
  30. name: 'users',
  31. orderby: '',
  32. },
  33. ],
  34. widgetType: WidgetType.RELEASE,
  35. };
  36. const singleQueryWidget = {
  37. title: 'Sessions',
  38. interval: '5m',
  39. displayType: DisplayType.LINE,
  40. queries: [
  41. {
  42. conditions: '',
  43. fields: [`count_unique(user)`],
  44. aggregates: [`count_unique(user)`],
  45. columns: [],
  46. name: 'sessions',
  47. orderby: '-count_unique(user)',
  48. },
  49. ],
  50. widgetType: WidgetType.RELEASE,
  51. };
  52. const selection = {
  53. projects: [1],
  54. environments: ['prod'],
  55. datetime: {
  56. period: '14d',
  57. start: null,
  58. end: null,
  59. utc: null,
  60. },
  61. };
  62. const api = new MockApiClient();
  63. afterEach(function () {
  64. MockApiClient.clearMockResponses();
  65. });
  66. it('can send chart requests', async function () {
  67. jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
  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: expect.objectContaining({
  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. jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
  436. const mock = MockApiClient.addMockResponse({
  437. url: '/organizations/org-slug/metrics/data/',
  438. body: TestStubs.MetricsSessionUserCountByStatusByRelease(),
  439. });
  440. const children = jest.fn(() => <div />);
  441. render(
  442. <ReleaseWidgetQueries
  443. api={api}
  444. widget={{...singleQueryWidget, displayType: DisplayType.TABLE}}
  445. organization={organization}
  446. selection={selection}
  447. >
  448. {children}
  449. </ReleaseWidgetQueries>
  450. );
  451. expect(mock).toHaveBeenCalledTimes(1);
  452. await waitFor(() =>
  453. expect(children).toHaveBeenLastCalledWith(
  454. expect.objectContaining({
  455. errorMessage: undefined,
  456. loading: false,
  457. tableResults: [
  458. {
  459. data: [
  460. {
  461. 'count_unique(user)': 1,
  462. id: '0',
  463. release: '1',
  464. 'session.status': 'crashed',
  465. 'sum(session)': 34,
  466. },
  467. {
  468. 'count_unique(user)': 1,
  469. id: '1',
  470. release: '1',
  471. 'session.status': 'abnormal',
  472. 'sum(session)': 1,
  473. },
  474. {
  475. 'count_unique(user)': 2,
  476. id: '2',
  477. release: '1',
  478. 'session.status': 'errored',
  479. 'sum(session)': 451,
  480. },
  481. {
  482. 'count_unique(user)': 3,
  483. id: '3',
  484. release: '1',
  485. 'session.status': 'healthy',
  486. 'sum(session)': 5058,
  487. },
  488. {
  489. 'count_unique(user)': 2,
  490. id: '4',
  491. release: '2',
  492. 'session.status': 'crashed',
  493. 'sum(session)': 35,
  494. },
  495. {
  496. 'count_unique(user)': 1,
  497. id: '5',
  498. release: '2',
  499. 'session.status': 'abnormal',
  500. 'sum(session)': 1,
  501. },
  502. {
  503. 'count_unique(user)': 1,
  504. id: '6',
  505. release: '2',
  506. 'session.status': 'errored',
  507. 'sum(session)': 452,
  508. },
  509. {
  510. 'count_unique(user)': 10,
  511. id: '7',
  512. release: '2',
  513. 'session.status': 'healthy',
  514. 'sum(session)': 5059,
  515. },
  516. ],
  517. meta: {
  518. 'count_unique(user)': 'integer',
  519. release: 'string',
  520. 'session.status': 'string',
  521. 'sum(session)': 'integer',
  522. },
  523. title: 'sessions',
  524. },
  525. ],
  526. timeseriesResults: undefined,
  527. })
  528. )
  529. );
  530. });
  531. it('can send big number requests', async function () {
  532. jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
  533. const mock = MockApiClient.addMockResponse({
  534. url: '/organizations/org-slug/metrics/data/',
  535. body: TestStubs.MetricsField({
  536. field: `count_unique(sentry.sessions.user)`,
  537. }),
  538. });
  539. const children = jest.fn(() => <div />);
  540. render(
  541. <ReleaseWidgetQueries
  542. api={api}
  543. widget={{...singleQueryWidget, displayType: DisplayType.BIG_NUMBER}}
  544. organization={organization}
  545. selection={selection}
  546. >
  547. {children}
  548. </ReleaseWidgetQueries>
  549. );
  550. expect(mock).toHaveBeenCalledTimes(1);
  551. expect(mock).toHaveBeenCalledWith(
  552. expect.anything(),
  553. expect.objectContaining({
  554. query: expect.objectContaining({
  555. field: ['count_unique(sentry.sessions.user)'],
  556. orderBy: '-count_unique(sentry.sessions.user)',
  557. }),
  558. })
  559. );
  560. await waitFor(() =>
  561. expect(children).toHaveBeenLastCalledWith(
  562. expect.objectContaining({
  563. loading: false,
  564. tableResults: [
  565. {
  566. data: [{'count_unique(user)': 51292.95404741901, id: '0'}],
  567. meta: {'count_unique(user)': 'integer'},
  568. title: 'sessions',
  569. },
  570. ],
  571. })
  572. )
  573. );
  574. });
  575. it('can send multiple API requests', function () {
  576. jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
  577. const metricsMock = MockApiClient.addMockResponse({
  578. url: '/organizations/org-slug/metrics/data/',
  579. body: TestStubs.SessionsField({
  580. field: `sum(sentry.sessions.session)`,
  581. }),
  582. match: [
  583. MockApiClient.matchQuery({
  584. field: [`sum(sentry.sessions.session)`],
  585. }),
  586. ],
  587. });
  588. render(
  589. <ReleaseWidgetQueries
  590. api={api}
  591. widget={multipleQueryWidget}
  592. organization={organization}
  593. selection={selection}
  594. >
  595. {() => <div data-test-id="child" />}
  596. </ReleaseWidgetQueries>
  597. );
  598. // Child should be rendered and 2 requests should be sent.
  599. expect(screen.getByTestId('child')).toBeInTheDocument();
  600. expect(metricsMock).toHaveBeenCalledTimes(2);
  601. expect(metricsMock).toHaveBeenNthCalledWith(
  602. 1,
  603. '/organizations/org-slug/metrics/data/',
  604. expect.objectContaining({
  605. query: {
  606. environment: ['prod'],
  607. field: ['sum(sentry.sessions.session)'],
  608. groupBy: [],
  609. interval: '30m',
  610. project: [1],
  611. statsPeriod: '14d',
  612. per_page: 1,
  613. includeSeries: 1,
  614. includeTotals: 0,
  615. },
  616. })
  617. );
  618. expect(metricsMock).toHaveBeenNthCalledWith(
  619. 2,
  620. '/organizations/org-slug/metrics/data/',
  621. expect.objectContaining({
  622. query: {
  623. environment: ['prod'],
  624. field: ['sum(sentry.sessions.session)'],
  625. groupBy: [],
  626. interval: '30m',
  627. project: [1],
  628. query: 'environment:prod',
  629. statsPeriod: '14d',
  630. per_page: 1,
  631. includeSeries: 1,
  632. includeTotals: 0,
  633. },
  634. })
  635. );
  636. });
  637. it('sets errorMessage when the first request fails', async function () {
  638. const failMock = MockApiClient.addMockResponse({
  639. url: '/organizations/org-slug/metrics/data/',
  640. statusCode: 400,
  641. body: {detail: badMessage},
  642. match: [
  643. MockApiClient.matchQuery({
  644. field: [`sum(sentry.sessions.session)`],
  645. }),
  646. ],
  647. });
  648. const children = jest.fn(() => <div data-test-id="child" />);
  649. render(
  650. <ReleaseWidgetQueries
  651. api={api}
  652. widget={multipleQueryWidget}
  653. organization={organization}
  654. selection={selection}
  655. >
  656. {children}
  657. </ReleaseWidgetQueries>
  658. );
  659. // Child should be rendered and 2 requests should be sent.
  660. expect(screen.getByTestId('child')).toBeInTheDocument();
  661. expect(failMock).toHaveBeenCalledTimes(2);
  662. await waitFor(() =>
  663. expect(children).toHaveBeenLastCalledWith(
  664. expect.objectContaining({errorMessage: badMessage})
  665. )
  666. );
  667. });
  668. it('adjusts interval based on date window', function () {
  669. jest.useFakeTimers().setSystemTime(new Date('2022-08-02'));
  670. const mock = MockApiClient.addMockResponse({
  671. url: '/organizations/org-slug/metrics/data/',
  672. body: TestStubs.SessionsField({
  673. field: `sum(sentry.sessions.session)`,
  674. }),
  675. });
  676. render(
  677. <ReleaseWidgetQueries
  678. api={api}
  679. widget={{...singleQueryWidget, interval: '1m'}}
  680. organization={organization}
  681. selection={{...selection, datetime: {...selection.datetime, period: '14d'}}}
  682. >
  683. {() => <div data-test-id="child" />}
  684. </ReleaseWidgetQueries>
  685. );
  686. expect(screen.getByTestId('child')).toBeInTheDocument();
  687. expect(mock).toHaveBeenCalledTimes(1);
  688. expect(mock).toHaveBeenCalledWith(
  689. expect.anything(),
  690. expect.objectContaining({
  691. query: expect.objectContaining({
  692. interval: '30m',
  693. statsPeriod: '14d',
  694. environment: ['prod'],
  695. project: [1],
  696. }),
  697. })
  698. );
  699. });
  700. it('does not re-fetch when renaming legend alias / adding falsy fields', () => {
  701. const mock = MockApiClient.addMockResponse({
  702. url: '/organizations/org-slug/metrics/data/',
  703. body: TestStubs.SessionsField({
  704. field: `sum(sentry.sessions.session)`,
  705. }),
  706. });
  707. const children = jest.fn(() => <div />);
  708. const {rerender} = render(
  709. <ReleaseWidgetQueries
  710. api={api}
  711. widget={singleQueryWidget}
  712. organization={organization}
  713. selection={selection}
  714. >
  715. {children}
  716. </ReleaseWidgetQueries>
  717. );
  718. expect(mock).toHaveBeenCalledTimes(1);
  719. rerender(
  720. <ReleaseWidgetQueries
  721. api={api}
  722. widget={{
  723. ...singleQueryWidget,
  724. queries: [
  725. {
  726. ...singleQueryWidget.queries[0],
  727. name: 'New Legend Alias',
  728. fields: [...singleQueryWidget.queries[0].fields, ''],
  729. },
  730. ],
  731. }}
  732. organization={organization}
  733. selection={selection}
  734. >
  735. {children}
  736. </ReleaseWidgetQueries>
  737. );
  738. // no additional request has been sent, the total count of requests is still 1
  739. expect(mock).toHaveBeenCalledTimes(1);
  740. });
  741. it('does not re-fetch when dashboard filter remains the same', () => {
  742. const mock = MockApiClient.addMockResponse({
  743. url: '/organizations/org-slug/metrics/data/',
  744. body: TestStubs.SessionsField({
  745. field: `sum(sentry.sessions.session)`,
  746. }),
  747. });
  748. const children = jest.fn(() => <div />);
  749. const {rerender} = render(
  750. <ReleaseWidgetQueries
  751. api={api}
  752. widget={singleQueryWidget}
  753. organization={organization}
  754. selection={selection}
  755. dashboardFilters={{[DashboardFilterKeys.RELEASE]: ['abc@1.3.0']}}
  756. >
  757. {children}
  758. </ReleaseWidgetQueries>
  759. );
  760. expect(mock).toHaveBeenCalledTimes(1);
  761. rerender(
  762. <ReleaseWidgetQueries
  763. api={api}
  764. widget={singleQueryWidget}
  765. organization={organization}
  766. selection={selection}
  767. dashboardFilters={{[DashboardFilterKeys.RELEASE]: ['abc@1.3.0']}}
  768. >
  769. {children}
  770. </ReleaseWidgetQueries>
  771. );
  772. // no additional request has been sent, the total count of requests is still 1
  773. expect(mock).toHaveBeenCalledTimes(1);
  774. });
  775. });