results.spec.jsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. import {browserHistory} from 'react-router';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import ProjectsStore from 'app/stores/projectsStore';
  5. import Results from 'app/views/eventsV2/results';
  6. const FIELDS = [
  7. {
  8. field: 'title',
  9. },
  10. {
  11. field: 'timestamp',
  12. },
  13. {
  14. field: 'user',
  15. },
  16. {
  17. field: 'count()',
  18. },
  19. ];
  20. const generateFields = () => ({
  21. field: FIELDS.map(i => i.field),
  22. });
  23. describe('EventsV2 > Results', function () {
  24. const eventTitle = 'Oh no something bad';
  25. const features = ['discover-basic'];
  26. let eventResultsMock, mockSaved, eventsStatsMock, mockVisit;
  27. beforeEach(function () {
  28. MockApiClient.addMockResponse({
  29. url: '/organizations/org-slug/projects/',
  30. body: [],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/projects-count/',
  34. body: {myProjects: 10, allProjects: 300},
  35. });
  36. MockApiClient.addMockResponse({
  37. url: '/organizations/org-slug/tags/',
  38. body: [],
  39. });
  40. eventsStatsMock = MockApiClient.addMockResponse({
  41. url: '/organizations/org-slug/events-stats/',
  42. body: {data: [[123, []]]},
  43. });
  44. MockApiClient.addMockResponse({
  45. url: '/organizations/org-slug/recent-searches/',
  46. body: [],
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/recent-searches/',
  50. method: 'POST',
  51. body: [],
  52. });
  53. MockApiClient.addMockResponse({
  54. url: '/organizations/org-slug/releases/stats/',
  55. body: [],
  56. });
  57. eventResultsMock = MockApiClient.addMockResponse({
  58. url: '/organizations/org-slug/eventsv2/',
  59. body: {
  60. meta: {
  61. id: 'string',
  62. title: 'string',
  63. 'project.name': 'string',
  64. timestamp: 'date',
  65. 'user.id': 'string',
  66. },
  67. data: [
  68. {
  69. id: 'deadbeef',
  70. 'user.id': 'alberto leal',
  71. title: eventTitle,
  72. 'project.name': 'project-slug',
  73. timestamp: '2019-05-23T22:12:48+00:00',
  74. },
  75. ],
  76. },
  77. });
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/events-meta/',
  80. body: {
  81. count: 2,
  82. },
  83. });
  84. MockApiClient.addMockResponse({
  85. url: '/organizations/org-slug/events/project-slug:deadbeef/',
  86. method: 'GET',
  87. body: {
  88. id: '1234',
  89. size: 1200,
  90. eventID: 'deadbeef',
  91. title: 'Oh no something bad',
  92. message: 'It was not good',
  93. dateCreated: '2019-05-23T22:12:48+00:00',
  94. entries: [
  95. {
  96. type: 'message',
  97. message: 'bad stuff',
  98. data: {},
  99. },
  100. ],
  101. tags: [{key: 'browser', value: 'Firefox'}],
  102. },
  103. });
  104. MockApiClient.addMockResponse({
  105. url: '/organizations/org-slug/events-facets/',
  106. body: [
  107. {
  108. key: 'release',
  109. topValues: [{count: 2, value: 'abcd123', name: 'abcd123'}],
  110. },
  111. {
  112. key: 'environment',
  113. topValues: [{count: 2, value: 'abcd123', name: 'abcd123'}],
  114. },
  115. ],
  116. });
  117. mockVisit = MockApiClient.addMockResponse({
  118. url: '/organizations/org-slug/discover/saved/1/visit/',
  119. method: 'POST',
  120. body: [],
  121. statusCode: 200,
  122. });
  123. mockSaved = MockApiClient.addMockResponse({
  124. url: '/organizations/org-slug/discover/saved/1/',
  125. method: 'GET',
  126. statusCode: 200,
  127. body: {
  128. id: '1',
  129. name: 'new',
  130. projects: [],
  131. version: 2,
  132. expired: false,
  133. dateCreated: '2021-04-08T17:53:25.195782Z',
  134. dateUpdated: '2021-04-09T12:13:18.567264Z',
  135. createdBy: {
  136. id: '2',
  137. },
  138. environment: [],
  139. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  140. widths: ['-1', '-1', '-1', '-1', '-1'],
  141. range: '24h',
  142. orderby: '-user.display',
  143. },
  144. });
  145. });
  146. afterEach(function () {
  147. MockApiClient.clearMockResponses();
  148. ProjectsStore.reset();
  149. });
  150. it('loads data when moving from an invalid to valid EventView', async function () {
  151. const organization = TestStubs.Organization({
  152. features,
  153. projects: [TestStubs.Project()],
  154. });
  155. // Start off with an invalid view (empty is invalid)
  156. const initialData = initializeOrg({
  157. organization,
  158. router: {
  159. location: {query: {query: 'tag:value'}},
  160. },
  161. });
  162. const wrapper = mountWithTheme(
  163. <Results
  164. organization={organization}
  165. location={initialData.router.location}
  166. router={initialData.router}
  167. />,
  168. initialData.routerContext
  169. );
  170. ProjectsStore.loadInitialData(initialData.organization.projects);
  171. await tick();
  172. wrapper.update();
  173. // No request as eventview was invalid.
  174. expect(eventResultsMock).not.toHaveBeenCalled();
  175. // Should redirect and retain the old query value..
  176. expect(browserHistory.replace).toHaveBeenCalledWith(
  177. expect.objectContaining({
  178. pathname: '/organizations/org-slug/discover/results/',
  179. query: expect.objectContaining({
  180. query: 'tag:value',
  181. }),
  182. })
  183. );
  184. // Update location simulating a redirect.
  185. wrapper.setProps({location: {query: {...generateFields()}}});
  186. wrapper.update();
  187. // Should load events once
  188. expect(eventResultsMock).toHaveBeenCalled();
  189. });
  190. it('pushes to router when user id is wrong', async function () {
  191. const organization = TestStubs.Organization({
  192. features,
  193. projects: [TestStubs.Project()],
  194. });
  195. const initialData = initializeOrg({
  196. organization,
  197. router: {
  198. location: {query: {...generateFields(), cursor: '0%3A50%3A0', user: '2'}},
  199. },
  200. });
  201. ProjectsStore.loadInitialData(initialData.organization.projects);
  202. const wrapper = mountWithTheme(
  203. <Results
  204. organization={organization}
  205. location={initialData.router.location}
  206. router={initialData.router}
  207. />,
  208. initialData.routerContext
  209. );
  210. await tick();
  211. wrapper.update();
  212. expect(initialData.router.location).toEqual({
  213. query: {
  214. ...generateFields(),
  215. cursor: '0%3A50%3A0',
  216. user: '2',
  217. },
  218. });
  219. expect(initialData.router.push).toHaveBeenCalledWith({
  220. pathname: undefined,
  221. query: {
  222. ...generateFields(),
  223. cursor: '0%3A50%3A0',
  224. user: '1',
  225. },
  226. });
  227. wrapper.unmount();
  228. });
  229. it('pagination cursor should be cleared when making a search', async function () {
  230. const organization = TestStubs.Organization({
  231. features,
  232. projects: [TestStubs.Project()],
  233. });
  234. const initialData = initializeOrg({
  235. organization,
  236. router: {
  237. location: {query: {...generateFields(), cursor: '0%3A50%3A0'}},
  238. },
  239. });
  240. ProjectsStore.loadInitialData(initialData.organization.projects);
  241. const wrapper = mountWithTheme(
  242. <Results
  243. organization={organization}
  244. location={initialData.router.location}
  245. router={initialData.router}
  246. />,
  247. initialData.routerContext
  248. );
  249. await tick();
  250. wrapper.update();
  251. // ensure cursor query string is initially present in the location
  252. expect(initialData.router.location).toEqual({
  253. query: {
  254. ...generateFields(),
  255. cursor: '0%3A50%3A0',
  256. },
  257. });
  258. // perform a search
  259. const search = wrapper.find('#smart-search-input').first();
  260. search.simulate('change', {target: {value: 'geo:canada'}}).simulate('submit', {
  261. preventDefault() {},
  262. });
  263. await tick();
  264. // should only be called with saved queries
  265. expect(mockVisit).not.toHaveBeenCalled();
  266. // cursor query string should be omitted from the query string
  267. expect(initialData.router.push).toHaveBeenCalledWith({
  268. pathname: undefined,
  269. query: {
  270. ...generateFields(),
  271. query: 'geo:canada',
  272. statsPeriod: '14d',
  273. },
  274. });
  275. wrapper.unmount();
  276. });
  277. it('renders a y-axis selector', async function () {
  278. const organization = TestStubs.Organization({
  279. features,
  280. projects: [TestStubs.Project()],
  281. });
  282. const initialData = initializeOrg({
  283. organization,
  284. router: {
  285. location: {query: {...generateFields(), yAxis: 'count()'}},
  286. },
  287. });
  288. const wrapper = mountWithTheme(
  289. <Results
  290. organization={organization}
  291. location={initialData.router.location}
  292. router={initialData.router}
  293. />,
  294. initialData.routerContext
  295. );
  296. // y-axis selector is last.
  297. const selector = wrapper.find('OptionSelector').last();
  298. // Open the selector
  299. selector.find('StyledDropdownButton button').simulate('click');
  300. // Click one of the options.
  301. selector.find('DropdownMenu MenuItem span').first().simulate('click');
  302. await tick();
  303. wrapper.update();
  304. const eventsRequest = wrapper.find('EventsChart');
  305. expect(eventsRequest.props().yAxis).toEqual(['count()']);
  306. wrapper.unmount();
  307. });
  308. it('renders a display selector', async function () {
  309. const organization = TestStubs.Organization({
  310. features,
  311. projects: [TestStubs.Project()],
  312. });
  313. const initialData = initializeOrg({
  314. organization,
  315. router: {
  316. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  317. },
  318. });
  319. const wrapper = mountWithTheme(
  320. <Results
  321. organization={organization}
  322. location={initialData.router.location}
  323. router={initialData.router}
  324. />,
  325. initialData.routerContext
  326. );
  327. ProjectsStore.loadInitialData(initialData.organization.projects);
  328. await tick();
  329. wrapper.update();
  330. // display selector is first.
  331. const selector = wrapper.find('OptionSelector').first();
  332. // Open the selector
  333. selector.find('StyledDropdownButton button').simulate('click');
  334. // Click the 'default' option.
  335. selector
  336. .find('DropdownMenu MenuItem [data-test-id="option-default"]')
  337. .first()
  338. .simulate('click');
  339. await tick();
  340. wrapper.update();
  341. const eventsRequest = wrapper.find('EventsChart').props();
  342. expect(eventsRequest.disableReleases).toEqual(false);
  343. expect(eventsRequest.disablePrevious).toEqual(true);
  344. wrapper.unmount();
  345. });
  346. it('excludes top5 options when plan does not include discover-query', async function () {
  347. const organization = TestStubs.Organization({
  348. features: ['discover-basic'],
  349. projects: [TestStubs.Project()],
  350. });
  351. const initialData = initializeOrg({
  352. organization,
  353. router: {
  354. location: {query: {...generateFields(), display: 'previous'}},
  355. },
  356. });
  357. const wrapper = mountWithTheme(
  358. <Results
  359. organization={organization}
  360. location={initialData.router.location}
  361. router={initialData.router}
  362. />,
  363. initialData.routerContext
  364. );
  365. // display selector is first.
  366. const selector = wrapper.find('OptionSelector').first();
  367. // Open the selector
  368. selector.find('StyledDropdownButton button').simulate('click');
  369. await tick();
  370. // Make sure the top5 option isn't present
  371. const options = selector
  372. .find('DropdownMenu MenuItem')
  373. .map(item => item.prop('data-test-id'));
  374. expect(options).not.toContain('option-top5');
  375. expect(options).not.toContain('option-dailytop5');
  376. expect(options).toContain('option-default');
  377. wrapper.unmount();
  378. });
  379. it('needs confirmation on long queries', async function () {
  380. const organization = TestStubs.Organization({
  381. features: ['discover-basic'],
  382. projects: [TestStubs.Project()],
  383. });
  384. const initialData = initializeOrg({
  385. organization,
  386. router: {
  387. location: {query: {...generateFields(), statsPeriod: '60d', project: '-1'}},
  388. },
  389. });
  390. const wrapper = mountWithTheme(
  391. <Results
  392. organization={organization}
  393. location={initialData.router.location}
  394. router={initialData.router}
  395. />,
  396. initialData.routerContext
  397. );
  398. await tick();
  399. const results = wrapper.find('Results');
  400. expect(results.state('needConfirmation')).toEqual(true);
  401. wrapper.unmount();
  402. });
  403. it('needs confirmation on long query with explicit projects', async function () {
  404. const organization = TestStubs.Organization({
  405. features: ['discover-basic'],
  406. projects: [TestStubs.Project()],
  407. });
  408. const initialData = initializeOrg({
  409. organization,
  410. router: {
  411. location: {
  412. query: {
  413. ...generateFields(),
  414. statsPeriod: '60d',
  415. project: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
  416. },
  417. },
  418. },
  419. });
  420. const wrapper = mountWithTheme(
  421. <Results
  422. organization={organization}
  423. location={initialData.router.location}
  424. router={initialData.router}
  425. />,
  426. initialData.routerContext
  427. );
  428. await tick();
  429. const results = wrapper.find('Results');
  430. expect(results.state('needConfirmation')).toEqual(true);
  431. wrapper.unmount();
  432. });
  433. it('does not need confirmation on short queries', async function () {
  434. const organization = TestStubs.Organization({
  435. features: ['discover-basic'],
  436. projects: [TestStubs.Project()],
  437. });
  438. const initialData = initializeOrg({
  439. organization,
  440. router: {
  441. location: {query: {...generateFields(), statsPeriod: '30d', project: '-1'}},
  442. },
  443. });
  444. const wrapper = mountWithTheme(
  445. <Results
  446. organization={organization}
  447. location={initialData.router.location}
  448. router={initialData.router}
  449. />,
  450. initialData.routerContext
  451. );
  452. await tick();
  453. const results = wrapper.find('Results');
  454. expect(results.state('needConfirmation')).toEqual(false);
  455. wrapper.unmount();
  456. });
  457. it('does not need confirmation with to few projects', async function () {
  458. const organization = TestStubs.Organization({
  459. features: ['discover-basic'],
  460. projects: [TestStubs.Project()],
  461. });
  462. const initialData = initializeOrg({
  463. organization,
  464. router: {
  465. location: {
  466. query: {...generateFields(), statsPeriod: '90d', project: [1, 2, 3, 4]},
  467. },
  468. },
  469. });
  470. const wrapper = mountWithTheme(
  471. <Results
  472. organization={organization}
  473. location={initialData.router.location}
  474. router={initialData.router}
  475. />,
  476. initialData.routerContext
  477. );
  478. await tick();
  479. const results = wrapper.find('Results');
  480. expect(results.state('needConfirmation')).toEqual(false);
  481. wrapper.unmount();
  482. });
  483. it('retrieves saved query', async function () {
  484. const organization = TestStubs.Organization({
  485. features,
  486. projects: [TestStubs.Project()],
  487. slug: 'org-slug',
  488. });
  489. const initialData = initializeOrg({
  490. organization,
  491. router: {
  492. location: {query: {id: '1', statsPeriod: '24h'}},
  493. },
  494. });
  495. const wrapper = mountWithTheme(
  496. <Results
  497. organization={organization}
  498. location={initialData.router.location}
  499. router={initialData.router}
  500. />,
  501. initialData.routerContext
  502. );
  503. await tick();
  504. const savedQuery = wrapper.find('SavedQueryAPI').state('savedQuery');
  505. expect(savedQuery.name).toEqual('new');
  506. expect(savedQuery.id).toEqual('1');
  507. expect(savedQuery.fields).toEqual([
  508. 'title',
  509. 'event.type',
  510. 'project',
  511. 'user.display',
  512. 'timestamp',
  513. ]);
  514. expect(savedQuery.projects).toEqual([]);
  515. expect(savedQuery.range).toEqual('24h');
  516. expect(mockSaved).toHaveBeenCalled();
  517. expect(mockVisit).toHaveBeenCalledTimes(1);
  518. wrapper.unmount();
  519. });
  520. it('creates event view from saved query', async function () {
  521. const organization = TestStubs.Organization({
  522. features,
  523. projects: [TestStubs.Project()],
  524. slug: 'org-slug',
  525. });
  526. const initialData = initializeOrg({
  527. organization,
  528. router: {
  529. location: {query: {id: '1', statsPeriod: '24h'}},
  530. },
  531. });
  532. const wrapper = mountWithTheme(
  533. <Results
  534. organization={organization}
  535. location={initialData.router.location}
  536. router={initialData.router}
  537. />,
  538. initialData.routerContext
  539. );
  540. await tick();
  541. const eventView = wrapper.find('Results').state('eventView');
  542. expect(eventView.name).toEqual('new');
  543. expect(eventView.id).toEqual('1');
  544. expect(eventView.fields.length).toEqual(5);
  545. expect(eventView.project).toEqual([]);
  546. expect(eventView.statsPeriod).toEqual('24h');
  547. expect(eventView.sorts).toEqual([{field: 'user.display', kind: 'desc'}]);
  548. wrapper.unmount();
  549. });
  550. it('overrides saved query params with location query params', async function () {
  551. const organization = TestStubs.Organization({
  552. features,
  553. projects: [TestStubs.Project()],
  554. slug: 'org-slug',
  555. });
  556. const initialData = initializeOrg({
  557. organization,
  558. router: {
  559. location: {
  560. query: {
  561. id: '1',
  562. statsPeriod: '7d',
  563. project: [2],
  564. environment: ['production'],
  565. },
  566. },
  567. },
  568. });
  569. const wrapper = mountWithTheme(
  570. <Results
  571. organization={organization}
  572. location={initialData.router.location}
  573. router={initialData.router}
  574. />,
  575. initialData.routerContext
  576. );
  577. await tick();
  578. const eventView = wrapper.find('Results').state('eventView');
  579. expect(eventView.name).toEqual('new');
  580. expect(eventView.id).toEqual('1');
  581. expect(eventView.fields.length).toEqual(5);
  582. expect(eventView.project).toEqual([2]);
  583. expect(eventView.statsPeriod).toEqual('7d');
  584. expect(eventView.environment).toEqual(['production']);
  585. expect(mockVisit).toHaveBeenCalledTimes(1);
  586. wrapper.unmount();
  587. });
  588. it('updates chart whenever yAxis parameter changes', async function () {
  589. const organization = TestStubs.Organization({
  590. features,
  591. projects: [TestStubs.Project()],
  592. });
  593. const initialData = initializeOrg({
  594. organization,
  595. router: {
  596. location: {query: {...generateFields(), yAxis: 'count()'}},
  597. },
  598. });
  599. const wrapper = mountWithTheme(
  600. <Results
  601. organization={organization}
  602. location={initialData.router.location}
  603. router={initialData.router}
  604. />,
  605. initialData.routerContext
  606. );
  607. ProjectsStore.loadInitialData(initialData.organization.projects);
  608. await tick();
  609. wrapper.update();
  610. // Should load events once
  611. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  612. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  613. 1,
  614. '/organizations/org-slug/events-stats/',
  615. expect.objectContaining({
  616. query: expect.objectContaining({
  617. statsPeriod: '14d',
  618. yAxis: ['count()'],
  619. }),
  620. })
  621. );
  622. // Update location simulating a browser back button action
  623. wrapper.setProps({
  624. location: {
  625. query: {...generateFields(), yAxis: 'count_unique(user)'},
  626. },
  627. });
  628. await tick();
  629. wrapper.update();
  630. // Should load events again
  631. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  632. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  633. 2,
  634. '/organizations/org-slug/events-stats/',
  635. expect.objectContaining({
  636. query: expect.objectContaining({
  637. statsPeriod: '14d',
  638. yAxis: ['count_unique(user)'],
  639. }),
  640. })
  641. );
  642. wrapper.unmount();
  643. });
  644. it('updates chart whenever display parameter changes', async function () {
  645. const organization = TestStubs.Organization({
  646. features,
  647. projects: [TestStubs.Project()],
  648. });
  649. const initialData = initializeOrg({
  650. organization,
  651. router: {
  652. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  653. },
  654. });
  655. const wrapper = mountWithTheme(
  656. <Results
  657. organization={organization}
  658. location={initialData.router.location}
  659. router={initialData.router}
  660. />,
  661. initialData.routerContext
  662. );
  663. ProjectsStore.loadInitialData(initialData.organization.projects);
  664. await tick();
  665. wrapper.update();
  666. // Should load events once
  667. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  668. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  669. 1,
  670. '/organizations/org-slug/events-stats/',
  671. expect.objectContaining({
  672. query: expect.objectContaining({
  673. statsPeriod: '14d',
  674. yAxis: ['count()'],
  675. }),
  676. })
  677. );
  678. // Update location simulating a browser back button action
  679. wrapper.setProps({
  680. location: {
  681. query: {...generateFields(), display: 'previous', yAxis: 'count()'},
  682. },
  683. });
  684. await tick();
  685. wrapper.update();
  686. // Should load events again
  687. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  688. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  689. 2,
  690. '/organizations/org-slug/events-stats/',
  691. expect.objectContaining({
  692. query: expect.objectContaining({
  693. statsPeriod: '28d',
  694. yAxis: ['count()'],
  695. }),
  696. })
  697. );
  698. wrapper.unmount();
  699. });
  700. it('updates chart whenever display and yAxis parameters change', async function () {
  701. const organization = TestStubs.Organization({
  702. features,
  703. projects: [TestStubs.Project()],
  704. });
  705. const initialData = initializeOrg({
  706. organization,
  707. router: {
  708. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  709. },
  710. });
  711. const wrapper = mountWithTheme(
  712. <Results
  713. organization={organization}
  714. location={initialData.router.location}
  715. router={initialData.router}
  716. />,
  717. initialData.routerContext
  718. );
  719. ProjectsStore.loadInitialData(initialData.organization.projects);
  720. await tick();
  721. wrapper.update();
  722. // Should load events once
  723. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  724. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  725. 1,
  726. '/organizations/org-slug/events-stats/',
  727. expect.objectContaining({
  728. query: expect.objectContaining({
  729. statsPeriod: '14d',
  730. yAxis: ['count()'],
  731. }),
  732. })
  733. );
  734. // Update location simulating a browser back button action
  735. wrapper.setProps({
  736. location: {
  737. query: {...generateFields(), display: 'previous', yAxis: 'count_unique(user)'},
  738. },
  739. });
  740. await tick();
  741. wrapper.update();
  742. // Should load events again
  743. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  744. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  745. 2,
  746. '/organizations/org-slug/events-stats/',
  747. expect.objectContaining({
  748. query: expect.objectContaining({
  749. statsPeriod: '28d',
  750. yAxis: ['count_unique(user)'],
  751. }),
  752. })
  753. );
  754. wrapper.unmount();
  755. });
  756. });