releaseWidgetQueries.spec.tsx 26 KB

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