releaseWidgetQueries.spec.tsx 26 KB

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