results.spec.tsx 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. import {LocationFixture} from 'sentry-fixture/locationFixture';
  2. import {OrganizationFixture} from 'sentry-fixture/organization';
  3. import {ProjectFixture} from 'sentry-fixture/project';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  6. import selectEvent from 'sentry-test/selectEvent';
  7. import * as PageFilterPersistence from 'sentry/components/organizations/pageFilters/persistence';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import {browserHistory} from 'sentry/utils/browserHistory';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import Results from 'sentry/views/discover/results';
  12. import {DEFAULT_EVENT_VIEW, TRANSACTION_VIEWS} from './data';
  13. const FIELDS = [
  14. {
  15. field: 'title',
  16. },
  17. {
  18. field: 'timestamp',
  19. },
  20. {
  21. field: 'user',
  22. },
  23. {
  24. field: 'count()',
  25. },
  26. ];
  27. const generateFields = () => ({
  28. field: FIELDS.map(i => i.field),
  29. });
  30. const eventTitle = 'Oh no something bad';
  31. function renderMockRequests() {
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/projects/',
  34. body: [],
  35. });
  36. MockApiClient.addMockResponse({
  37. url: '/organizations/org-slug/projects-count/',
  38. body: {myProjects: 10, allProjects: 300},
  39. });
  40. MockApiClient.addMockResponse({
  41. url: '/organizations/org-slug/tags/',
  42. body: [],
  43. });
  44. const eventsStatsMock = MockApiClient.addMockResponse({
  45. url: '/organizations/org-slug/events-stats/',
  46. body: {data: [[123, []]]},
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/recent-searches/',
  50. body: [],
  51. });
  52. MockApiClient.addMockResponse({
  53. url: '/organizations/org-slug/recent-searches/',
  54. method: 'POST',
  55. body: [],
  56. });
  57. MockApiClient.addMockResponse({
  58. url: '/organizations/org-slug/releases/stats/',
  59. body: [],
  60. });
  61. const measurementsMetaMock = MockApiClient.addMockResponse({
  62. url: '/organizations/org-slug/measurements-meta/',
  63. method: 'GET',
  64. body: {},
  65. });
  66. const eventsResultsMock = MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/events/',
  68. body: {
  69. meta: {
  70. fields: {
  71. id: 'string',
  72. title: 'string',
  73. 'project.name': 'string',
  74. timestamp: 'date',
  75. 'user.id': 'string',
  76. },
  77. discoverSplitDecision: 'transaction-like',
  78. },
  79. data: [
  80. {
  81. id: 'deadbeef',
  82. 'user.id': 'alberto leal',
  83. title: eventTitle,
  84. 'project.name': 'project-slug',
  85. timestamp: '2019-05-23T22:12:48+00:00',
  86. },
  87. ],
  88. },
  89. });
  90. const eventsMetaMock = MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/events-meta/',
  92. body: {
  93. count: 2,
  94. },
  95. });
  96. MockApiClient.addMockResponse({
  97. url: '/organizations/org-slug/events/project-slug:deadbeef/',
  98. method: 'GET',
  99. body: {
  100. id: '1234',
  101. size: 1200,
  102. eventID: 'deadbeef',
  103. title: 'Oh no something bad',
  104. message: 'It was not good',
  105. dateCreated: '2019-05-23T22:12:48+00:00',
  106. entries: [
  107. {
  108. type: 'message',
  109. message: 'bad stuff',
  110. data: {},
  111. },
  112. ],
  113. tags: [{key: 'browser', value: 'Firefox'}],
  114. },
  115. });
  116. const eventFacetsMock = MockApiClient.addMockResponse({
  117. url: '/organizations/org-slug/events-facets/',
  118. body: [
  119. {
  120. key: 'release',
  121. topValues: [{count: 3, value: 'abcd123', name: 'abcd123'}],
  122. },
  123. {
  124. key: 'environment',
  125. topValues: [
  126. {count: 2, value: 'dev', name: 'dev'},
  127. {count: 1, value: 'prod', name: 'prod'},
  128. ],
  129. },
  130. {
  131. key: 'foo',
  132. topValues: [
  133. {count: 2, value: 'bar', name: 'bar'},
  134. {count: 1, value: 'baz', name: 'baz'},
  135. ],
  136. },
  137. ],
  138. });
  139. const mockVisit = MockApiClient.addMockResponse({
  140. url: '/organizations/org-slug/discover/saved/1/visit/',
  141. method: 'POST',
  142. body: [],
  143. statusCode: 200,
  144. });
  145. const mockSaved = MockApiClient.addMockResponse({
  146. url: '/organizations/org-slug/discover/saved/1/',
  147. method: 'GET',
  148. statusCode: 200,
  149. body: {
  150. id: '1',
  151. name: 'new',
  152. projects: [],
  153. version: 2,
  154. expired: false,
  155. dateCreated: '2021-04-08T17:53:25.195782Z',
  156. dateUpdated: '2021-04-09T12:13:18.567264Z',
  157. createdBy: {
  158. id: '2',
  159. },
  160. environment: [],
  161. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  162. widths: ['-1', '-1', '-1', '-1', '-1'],
  163. range: '24h',
  164. orderby: '-user.display',
  165. queryDataset: 'discover',
  166. },
  167. });
  168. MockApiClient.addMockResponse({
  169. url: '/organizations/org-slug/discover/homepage/',
  170. method: 'GET',
  171. statusCode: 200,
  172. body: {
  173. id: '2',
  174. name: '',
  175. projects: [],
  176. version: 2,
  177. expired: false,
  178. dateCreated: '2021-04-08T17:53:25.195782Z',
  179. dateUpdated: '2021-04-09T12:13:18.567264Z',
  180. createdBy: {
  181. id: '2',
  182. },
  183. environment: [],
  184. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  185. widths: ['-1', '-1', '-1', '-1', '-1'],
  186. range: '24h',
  187. orderby: '-user.display',
  188. queryDataset: 'discover',
  189. },
  190. });
  191. MockApiClient.addMockResponse({
  192. url: '/organizations/org-slug/dynamic-sampling/custom-rules/',
  193. method: 'GET',
  194. statusCode: 204,
  195. body: '',
  196. });
  197. return {
  198. eventsStatsMock,
  199. eventsMetaMock,
  200. eventsResultsMock,
  201. mockVisit,
  202. mockSaved,
  203. eventFacetsMock,
  204. measurementsMetaMock,
  205. };
  206. }
  207. describe('Results', function () {
  208. afterEach(function () {
  209. MockApiClient.clearMockResponses();
  210. ProjectsStore.reset();
  211. });
  212. describe('Events', function () {
  213. const features = ['discover-basic'];
  214. it('loads data when moving from an invalid to valid EventView', function () {
  215. const organization = OrganizationFixture({
  216. features,
  217. });
  218. // Start off with an invalid view (empty is invalid)
  219. const {router} = initializeOrg({
  220. organization,
  221. router: {
  222. location: {query: {query: 'tag:value'}},
  223. },
  224. });
  225. const mockRequests = renderMockRequests();
  226. ProjectsStore.loadInitialData([ProjectFixture()]);
  227. render(
  228. <Results
  229. location={router.location}
  230. router={router}
  231. loading={false}
  232. setSavedQuery={jest.fn()}
  233. />,
  234. {
  235. router: router,
  236. organization,
  237. }
  238. );
  239. // No request as eventview was invalid.
  240. expect(mockRequests.eventsStatsMock).not.toHaveBeenCalled();
  241. // Should redirect and retain the old query value
  242. expect(browserHistory.replace).toHaveBeenCalledWith(
  243. expect.objectContaining({
  244. pathname: '/organizations/org-slug/discover/results/',
  245. query: expect.objectContaining({
  246. query: 'tag:value',
  247. }),
  248. })
  249. );
  250. });
  251. it('pagination cursor should be cleared when making a search', async function () {
  252. const organization = OrganizationFixture({
  253. features,
  254. });
  255. const {router} = initializeOrg({
  256. organization,
  257. router: {
  258. location: {
  259. query: {
  260. ...generateFields(),
  261. cursor: '0%3A50%3A0',
  262. },
  263. },
  264. },
  265. });
  266. const mockRequests = renderMockRequests();
  267. ProjectsStore.loadInitialData([ProjectFixture()]);
  268. render(
  269. <Results
  270. organization={organization}
  271. location={router.location}
  272. router={router}
  273. loading={false}
  274. setSavedQuery={jest.fn()}
  275. />,
  276. {
  277. router: router,
  278. organization,
  279. }
  280. );
  281. // ensure cursor query string is initially present in the location
  282. expect(router.location).toEqual({
  283. query: {
  284. ...generateFields(),
  285. cursor: '0%3A50%3A0',
  286. },
  287. });
  288. await waitFor(() =>
  289. expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument()
  290. );
  291. // perform a search
  292. await userEvent.click(
  293. screen.getByPlaceholderText('Search for events, users, tags, and more')
  294. );
  295. await userEvent.paste('geo:canada');
  296. await userEvent.keyboard('{enter}');
  297. // should only be called with saved queries
  298. expect(mockRequests.mockVisit).not.toHaveBeenCalled();
  299. // cursor query string should be omitted from the query string
  300. expect(router.push).toHaveBeenCalledWith({
  301. pathname: undefined,
  302. query: {
  303. ...generateFields(),
  304. query: 'geo:canada',
  305. statsPeriod: '14d',
  306. },
  307. });
  308. });
  309. it('renders a y-axis selector', async function () {
  310. const organization = OrganizationFixture({
  311. features,
  312. });
  313. const {router} = initializeOrg({
  314. organization,
  315. router: {
  316. location: {query: {...generateFields(), yAxis: 'count()'}},
  317. },
  318. });
  319. renderMockRequests();
  320. ProjectsStore.loadInitialData([ProjectFixture()]);
  321. render(
  322. <Results
  323. organization={organization}
  324. location={router.location}
  325. router={router}
  326. loading={false}
  327. setSavedQuery={jest.fn()}
  328. />,
  329. {
  330. router: router,
  331. organization,
  332. }
  333. );
  334. // Click the 'default' option.
  335. await selectEvent.select(
  336. await screen.findByRole('button', {name: 'Y-Axis count()'}),
  337. 'count_unique(user)'
  338. );
  339. });
  340. it('renders a display selector', async function () {
  341. const organization = OrganizationFixture({
  342. features,
  343. });
  344. const {router} = initializeOrg({
  345. organization,
  346. router: {
  347. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  348. },
  349. });
  350. renderMockRequests();
  351. ProjectsStore.loadInitialData([ProjectFixture()]);
  352. render(
  353. <Results
  354. organization={organization}
  355. location={router.location}
  356. router={router}
  357. loading={false}
  358. setSavedQuery={jest.fn()}
  359. />,
  360. {
  361. router: router,
  362. organization,
  363. }
  364. );
  365. // Click the 'default' option.
  366. await selectEvent.select(
  367. await screen.findByRole('button', {name: /Display/}),
  368. 'Total Period'
  369. );
  370. });
  371. it('excludes top5 options when plan does not include discover-query', async function () {
  372. const organization = OrganizationFixture({
  373. features: ['discover-basic'],
  374. });
  375. const {router} = initializeOrg({
  376. organization,
  377. router: {
  378. location: {query: {...generateFields(), display: 'previous'}},
  379. },
  380. });
  381. ProjectsStore.loadInitialData([ProjectFixture()]);
  382. renderMockRequests();
  383. render(
  384. <Results
  385. organization={organization}
  386. location={router.location}
  387. router={router}
  388. loading={false}
  389. setSavedQuery={jest.fn()}
  390. />,
  391. {
  392. router: router,
  393. organization,
  394. }
  395. );
  396. await userEvent.click(await screen.findByRole('button', {name: /Display/}));
  397. expect(screen.queryByText('Top 5 Daily')).not.toBeInTheDocument();
  398. expect(screen.queryByText('Top 5 Period')).not.toBeInTheDocument();
  399. });
  400. it('needs confirmation on long queries', async function () {
  401. const organization = OrganizationFixture({
  402. features: ['discover-basic'],
  403. });
  404. const {router} = initializeOrg({
  405. organization,
  406. router: {
  407. location: {query: {...generateFields(), statsPeriod: '60d', project: '-1'}},
  408. },
  409. });
  410. ProjectsStore.loadInitialData([ProjectFixture()]);
  411. const mockRequests = renderMockRequests();
  412. render(
  413. <Results
  414. organization={organization}
  415. location={router.location}
  416. router={router}
  417. loading={false}
  418. setSavedQuery={jest.fn()}
  419. />,
  420. {
  421. router: router,
  422. organization,
  423. }
  424. );
  425. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(0);
  426. await waitFor(() => {
  427. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  428. });
  429. });
  430. it('needs confirmation on long query with explicit projects', async function () {
  431. const organization = OrganizationFixture({
  432. features: ['discover-basic'],
  433. });
  434. const {router} = initializeOrg({
  435. organization,
  436. router: {
  437. location: {
  438. query: {
  439. ...generateFields(),
  440. statsPeriod: '60d',
  441. project: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(String),
  442. },
  443. },
  444. },
  445. });
  446. ProjectsStore.loadInitialData([ProjectFixture()]);
  447. const mockRequests = renderMockRequests();
  448. render(
  449. <Results
  450. organization={organization}
  451. location={router.location}
  452. router={router}
  453. loading={false}
  454. setSavedQuery={jest.fn()}
  455. />,
  456. {
  457. router: router,
  458. organization,
  459. }
  460. );
  461. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(0);
  462. await waitFor(() => {
  463. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  464. });
  465. });
  466. it('does not need confirmation on short queries', async function () {
  467. const organization = OrganizationFixture({
  468. features: ['discover-basic'],
  469. });
  470. const {router} = initializeOrg({
  471. organization,
  472. router: {
  473. location: {query: {...generateFields(), statsPeriod: '30d', project: '-1'}},
  474. },
  475. });
  476. ProjectsStore.loadInitialData([ProjectFixture()]);
  477. const mockRequests = renderMockRequests();
  478. render(
  479. <Results
  480. organization={organization}
  481. location={router.location}
  482. router={router}
  483. loading={false}
  484. setSavedQuery={jest.fn()}
  485. />,
  486. {
  487. router: router,
  488. organization,
  489. }
  490. );
  491. await waitFor(() => {
  492. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  493. });
  494. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  495. });
  496. it('does not need confirmation with to few projects', async function () {
  497. const organization = OrganizationFixture({
  498. features: ['discover-basic'],
  499. });
  500. const {router} = initializeOrg({
  501. organization,
  502. router: {
  503. location: {
  504. query: {
  505. ...generateFields(),
  506. statsPeriod: '90d',
  507. project: [1, 2, 3, 4].map(String),
  508. },
  509. },
  510. },
  511. });
  512. ProjectsStore.loadInitialData([ProjectFixture()]);
  513. const mockRequests = renderMockRequests();
  514. render(
  515. <Results
  516. organization={organization}
  517. location={router.location}
  518. router={router}
  519. loading={false}
  520. setSavedQuery={jest.fn()}
  521. />,
  522. {
  523. router: router,
  524. organization,
  525. }
  526. );
  527. await waitFor(() => {
  528. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  529. });
  530. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  531. });
  532. it('creates event view from saved query', async function () {
  533. const organization = OrganizationFixture({
  534. features,
  535. slug: 'org-slug',
  536. });
  537. const {router} = initializeOrg({
  538. organization,
  539. router: {
  540. location: {query: {id: '1', statsPeriod: '24h'}},
  541. },
  542. });
  543. ProjectsStore.loadInitialData([ProjectFixture()]);
  544. const mockRequests = renderMockRequests();
  545. render(
  546. <Results
  547. organization={organization}
  548. location={router.location}
  549. router={router}
  550. loading={false}
  551. setSavedQuery={jest.fn()}
  552. />,
  553. {
  554. router: router,
  555. organization,
  556. }
  557. );
  558. await waitFor(() => expect(mockRequests.mockVisit).toHaveBeenCalled());
  559. expect(screen.getByRole('link', {name: 'timestamp'})).toHaveAttribute(
  560. 'href',
  561. 'undefined?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&queryDataset=discover&sort=-timestamp&statsPeriod=24h&topEvents=5'
  562. );
  563. expect(screen.getByRole('link', {name: 'project'})).toHaveAttribute(
  564. 'href',
  565. 'undefined?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&queryDataset=discover&sort=-project&statsPeriod=24h&topEvents=5'
  566. );
  567. // NOTE: This uses a legacy redirect for project event to the issue group event link
  568. expect(screen.getByRole('link', {name: 'deadbeef'})).toHaveAttribute(
  569. 'href',
  570. '/org-slug/project-slug/events/deadbeef/?id=1&referrer=discover-events-table&statsPeriod=24h'
  571. );
  572. expect(screen.getByRole('link', {name: 'user.display'})).toHaveAttribute(
  573. 'href',
  574. 'undefined?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&queryDataset=discover&sort=user.display&statsPeriod=24h&topEvents=5'
  575. );
  576. expect(screen.getByRole('link', {name: 'title'})).toHaveAttribute(
  577. 'href',
  578. 'undefined?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&queryDataset=discover&sort=-title&statsPeriod=24h&topEvents=5'
  579. );
  580. });
  581. it('overrides saved query params with location query params', async function () {
  582. const organization = OrganizationFixture({
  583. features,
  584. slug: 'org-slug',
  585. });
  586. const {router} = initializeOrg({
  587. organization,
  588. router: {
  589. location: {
  590. query: {
  591. id: '1',
  592. statsPeriod: '7d',
  593. project: ['2'],
  594. environment: ['production'],
  595. },
  596. },
  597. },
  598. });
  599. ProjectsStore.loadInitialData([ProjectFixture()]);
  600. const mockRequests = renderMockRequests();
  601. render(
  602. <Results
  603. organization={organization}
  604. location={router.location}
  605. router={router}
  606. loading={false}
  607. setSavedQuery={jest.fn()}
  608. />,
  609. {
  610. router: router,
  611. organization,
  612. }
  613. );
  614. await waitFor(() => expect(mockRequests.mockVisit).toHaveBeenCalled());
  615. expect(screen.getByRole('link', {name: 'timestamp'})).toHaveAttribute(
  616. 'href',
  617. 'undefined?environment=production&field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&project=2&query=&queryDataset=discover&sort=-timestamp&statsPeriod=7d&topEvents=5'
  618. );
  619. });
  620. it('updates chart whenever yAxis parameter changes', async function () {
  621. const organization = OrganizationFixture({
  622. features,
  623. });
  624. const {router} = initializeOrg({
  625. organization,
  626. router: {
  627. location: {query: {...generateFields(), yAxis: 'count()'}},
  628. },
  629. });
  630. ProjectsStore.loadInitialData([ProjectFixture()]);
  631. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  632. const {rerender} = render(
  633. <Results
  634. organization={organization}
  635. location={router.location}
  636. router={router}
  637. loading={false}
  638. setSavedQuery={jest.fn()}
  639. />,
  640. {
  641. router: router,
  642. organization,
  643. }
  644. );
  645. // Should load events once
  646. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  647. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  648. 1,
  649. '/organizations/org-slug/events-stats/',
  650. expect.objectContaining({
  651. query: expect.objectContaining({
  652. statsPeriod: '14d',
  653. yAxis: ['count()'],
  654. }),
  655. })
  656. );
  657. await waitFor(() => {
  658. expect(measurementsMetaMock).toHaveBeenCalled();
  659. });
  660. // Update location simulating a browser back button action
  661. rerender(
  662. <Results
  663. organization={organization}
  664. location={{
  665. ...router.location,
  666. query: {...generateFields(), yAxis: 'count_unique(user)'},
  667. }}
  668. router={router}
  669. loading={false}
  670. setSavedQuery={jest.fn()}
  671. />
  672. );
  673. // Should load events again
  674. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  675. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  676. 2,
  677. '/organizations/org-slug/events-stats/',
  678. expect.objectContaining({
  679. query: expect.objectContaining({
  680. statsPeriod: '14d',
  681. yAxis: ['count_unique(user)'],
  682. }),
  683. })
  684. );
  685. await waitFor(() => {
  686. expect(measurementsMetaMock).toHaveBeenCalled();
  687. });
  688. });
  689. it('updates chart whenever display parameter changes', async function () {
  690. const organization = OrganizationFixture({
  691. features,
  692. });
  693. const {router} = initializeOrg({
  694. organization,
  695. router: {
  696. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  697. },
  698. });
  699. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  700. ProjectsStore.loadInitialData([ProjectFixture()]);
  701. const {rerender} = render(
  702. <Results
  703. organization={organization}
  704. location={router.location}
  705. router={router}
  706. loading={false}
  707. setSavedQuery={jest.fn()}
  708. />,
  709. {
  710. router: router,
  711. organization,
  712. }
  713. );
  714. // Should load events once
  715. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  716. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  717. 1,
  718. '/organizations/org-slug/events-stats/',
  719. expect.objectContaining({
  720. query: expect.objectContaining({
  721. statsPeriod: '14d',
  722. yAxis: ['count()'],
  723. }),
  724. })
  725. );
  726. await waitFor(() => {
  727. expect(measurementsMetaMock).toHaveBeenCalled();
  728. });
  729. // Update location simulating a browser back button action
  730. rerender(
  731. <Results
  732. organization={organization}
  733. location={{
  734. ...router.location,
  735. query: {...generateFields(), display: 'previous', yAxis: 'count()'},
  736. }}
  737. router={router}
  738. loading={false}
  739. setSavedQuery={jest.fn()}
  740. />
  741. );
  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()'],
  751. }),
  752. })
  753. );
  754. await waitFor(() => {
  755. expect(measurementsMetaMock).toHaveBeenCalled();
  756. });
  757. });
  758. it('updates chart whenever display and yAxis parameters change', async function () {
  759. const organization = OrganizationFixture({
  760. features,
  761. });
  762. const {router} = initializeOrg({
  763. organization,
  764. router: {
  765. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  766. },
  767. });
  768. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  769. ProjectsStore.loadInitialData([ProjectFixture()]);
  770. const {rerender} = render(
  771. <Results
  772. organization={organization}
  773. location={router.location}
  774. router={router}
  775. loading={false}
  776. setSavedQuery={jest.fn()}
  777. />,
  778. {
  779. router: router,
  780. organization,
  781. }
  782. );
  783. // Should load events once
  784. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  785. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  786. 1,
  787. '/organizations/org-slug/events-stats/',
  788. expect.objectContaining({
  789. query: expect.objectContaining({
  790. statsPeriod: '14d',
  791. yAxis: ['count()'],
  792. }),
  793. })
  794. );
  795. await waitFor(() => {
  796. expect(measurementsMetaMock).toHaveBeenCalled();
  797. });
  798. // Update location simulating a browser back button action
  799. rerender(
  800. <Results
  801. organization={organization}
  802. location={{
  803. ...router.location,
  804. query: {
  805. ...generateFields(),
  806. display: 'previous',
  807. yAxis: 'count_unique(user)',
  808. },
  809. }}
  810. router={router}
  811. loading={false}
  812. setSavedQuery={jest.fn()}
  813. />
  814. );
  815. // Should load events again
  816. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  817. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  818. 2,
  819. '/organizations/org-slug/events-stats/',
  820. expect.objectContaining({
  821. query: expect.objectContaining({
  822. statsPeriod: '28d',
  823. yAxis: ['count_unique(user)'],
  824. }),
  825. })
  826. );
  827. await waitFor(() => {
  828. expect(measurementsMetaMock).toHaveBeenCalled();
  829. });
  830. });
  831. it('appends tag value to existing query when clicked', async function () {
  832. const organization = OrganizationFixture({
  833. features,
  834. });
  835. const {router} = initializeOrg({
  836. organization,
  837. router: {
  838. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  839. },
  840. });
  841. const mockRequests = renderMockRequests();
  842. ProjectsStore.loadInitialData([ProjectFixture()]);
  843. render(
  844. <Results
  845. organization={organization}
  846. location={router.location}
  847. router={router}
  848. loading={false}
  849. setSavedQuery={jest.fn()}
  850. />,
  851. {
  852. router: router,
  853. organization,
  854. }
  855. );
  856. await userEvent.click(await screen.findByRole('button', {name: 'Show Tags'}));
  857. await waitFor(() => expect(mockRequests.eventFacetsMock).toHaveBeenCalled());
  858. // TODO(edward): update this to be less generic
  859. await userEvent.click(screen.getByText('environment'));
  860. await userEvent.click(screen.getByText('foo'));
  861. // since environment collides with the environment field, it is wrapped with `tags[...]`
  862. expect(
  863. await screen.findByRole('link', {
  864. name: 'environment, dev, 100% of all events. View events with this tag value.',
  865. })
  866. ).toBeInTheDocument();
  867. expect(
  868. screen.getByRole('link', {
  869. name: 'foo, bar, 100% of all events. View events with this tag value.',
  870. })
  871. ).toBeInTheDocument();
  872. });
  873. it('respects pinned filters for prebuilt queries', async function () {
  874. const organization = OrganizationFixture({
  875. features: [...features, 'global-views'],
  876. });
  877. const {router} = initializeOrg({
  878. organization,
  879. router: {
  880. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  881. },
  882. });
  883. renderMockRequests();
  884. jest.spyOn(PageFilterPersistence, 'getPageFilterStorage').mockReturnValue({
  885. state: {
  886. project: [1],
  887. environment: [],
  888. start: null,
  889. end: null,
  890. period: '14d',
  891. utc: null,
  892. },
  893. pinnedFilters: new Set(['projects']),
  894. });
  895. ProjectsStore.loadInitialData([ProjectFixture({id: '1', slug: 'Pinned Project'})]);
  896. render(
  897. <Results
  898. organization={organization}
  899. location={router.location}
  900. router={router}
  901. loading={false}
  902. setSavedQuery={jest.fn()}
  903. />,
  904. {router: router, organization}
  905. );
  906. const projectPageFilter = await screen.findByTestId('page-filter-project-selector');
  907. expect(projectPageFilter).toHaveTextContent('All Projects');
  908. });
  909. it('displays tip when events response contains a tip', async function () {
  910. renderMockRequests();
  911. MockApiClient.addMockResponse({
  912. url: '/organizations/org-slug/events/',
  913. body: {
  914. meta: {
  915. fields: {},
  916. tips: {query: 'this is a tip'},
  917. },
  918. data: [],
  919. },
  920. });
  921. const organization = OrganizationFixture({
  922. features,
  923. });
  924. const {router} = initializeOrg({
  925. organization,
  926. router: {
  927. location: {query: {...generateFields(), yAxis: 'count()'}},
  928. },
  929. });
  930. ProjectsStore.loadInitialData([ProjectFixture()]);
  931. render(
  932. <Results
  933. organization={organization}
  934. location={router.location}
  935. router={router}
  936. loading={false}
  937. setSavedQuery={jest.fn()}
  938. />,
  939. {router: router, organization}
  940. );
  941. expect(await screen.findByText('this is a tip')).toBeInTheDocument();
  942. });
  943. it('renders metric fallback alert', async function () {
  944. const organization = OrganizationFixture({
  945. features: ['discover-basic'],
  946. });
  947. const {router} = initializeOrg({
  948. organization,
  949. router: {
  950. location: {query: {fromMetric: 'true', id: '1'}},
  951. },
  952. });
  953. ProjectsStore.loadInitialData([ProjectFixture()]);
  954. renderMockRequests();
  955. render(
  956. <Results
  957. organization={organization}
  958. location={router.location}
  959. router={router}
  960. loading={false}
  961. setSavedQuery={jest.fn()}
  962. />,
  963. {
  964. router: router,
  965. organization,
  966. }
  967. );
  968. expect(
  969. await screen.findByText(
  970. /You've navigated to this page from a performance metric widget generated from processed events/
  971. )
  972. ).toBeInTheDocument();
  973. });
  974. it('renders unparameterized data banner', async function () {
  975. const organization = OrganizationFixture({
  976. features: ['discover-basic'],
  977. });
  978. const {router} = initializeOrg({
  979. organization,
  980. router: {
  981. location: {query: {showUnparameterizedBanner: 'true', id: '1'}},
  982. },
  983. });
  984. ProjectsStore.loadInitialData([ProjectFixture()]);
  985. renderMockRequests();
  986. render(
  987. <Results
  988. organization={organization}
  989. location={router.location}
  990. router={router}
  991. loading={false}
  992. setSavedQuery={jest.fn()}
  993. />,
  994. {
  995. router: router,
  996. organization,
  997. }
  998. );
  999. expect(
  1000. await screen.findByText(/These are unparameterized transactions/)
  1001. ).toBeInTheDocument();
  1002. });
  1003. it('updates the homepage query with up to date eventView when Set as Default is clicked', async () => {
  1004. const mockHomepageUpdate = MockApiClient.addMockResponse({
  1005. url: '/organizations/org-slug/discover/homepage/',
  1006. method: 'PUT',
  1007. statusCode: 200,
  1008. });
  1009. const organization = OrganizationFixture({
  1010. features: ['discover-basic', 'discover-query'],
  1011. });
  1012. const {router} = initializeOrg({
  1013. organization,
  1014. router: {
  1015. // These fields take priority and should be sent in the request
  1016. location: {query: {field: ['title', 'user'], id: '1'}},
  1017. },
  1018. });
  1019. ProjectsStore.loadInitialData([ProjectFixture()]);
  1020. renderMockRequests();
  1021. render(
  1022. <Results
  1023. organization={organization}
  1024. location={router.location}
  1025. router={router}
  1026. loading={false}
  1027. setSavedQuery={jest.fn()}
  1028. />,
  1029. {router: router, organization}
  1030. );
  1031. await waitFor(() =>
  1032. expect(screen.getByRole('button', {name: /set as default/i})).toBeEnabled()
  1033. );
  1034. await userEvent.click(screen.getByText('Set as Default'));
  1035. expect(mockHomepageUpdate).toHaveBeenCalledWith(
  1036. '/organizations/org-slug/discover/homepage/',
  1037. expect.objectContaining({
  1038. data: expect.objectContaining({
  1039. fields: ['title', 'user'],
  1040. }),
  1041. })
  1042. );
  1043. });
  1044. it('Changes the Use as Discover button to a reset button for saved query', async () => {
  1045. renderMockRequests();
  1046. MockApiClient.addMockResponse({
  1047. url: '/organizations/org-slug/discover/homepage/',
  1048. method: 'PUT',
  1049. statusCode: 200,
  1050. body: {
  1051. id: '2',
  1052. name: '',
  1053. projects: [],
  1054. version: 2,
  1055. expired: false,
  1056. dateCreated: '2021-04-08T17:53:25.195782Z',
  1057. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1058. createdBy: {
  1059. id: '2',
  1060. },
  1061. environment: [],
  1062. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1063. widths: ['-1', '-1', '-1', '-1', '-1'],
  1064. range: '24h',
  1065. orderby: '-user.display',
  1066. queryDataset: 'discover',
  1067. },
  1068. });
  1069. const organization = OrganizationFixture({
  1070. features: ['discover-basic', 'discover-query'],
  1071. });
  1072. const {router} = initializeOrg({
  1073. organization,
  1074. router: {
  1075. location: {query: {id: '1'}},
  1076. },
  1077. });
  1078. ProjectsStore.loadInitialData([ProjectFixture()]);
  1079. renderMockRequests();
  1080. const {rerender} = render(
  1081. <Results
  1082. loading={false}
  1083. setSavedQuery={jest.fn()}
  1084. organization={organization}
  1085. location={router.location}
  1086. router={router}
  1087. />,
  1088. {router: router, organization}
  1089. );
  1090. await waitFor(() =>
  1091. expect(screen.getByRole('button', {name: /set as default/i})).toBeEnabled()
  1092. );
  1093. await userEvent.click(screen.getByText('Set as Default'));
  1094. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  1095. await userEvent.click(screen.getByText('Total Period'));
  1096. await userEvent.click(screen.getByText('Previous Period'));
  1097. const rerenderData = initializeOrg({
  1098. organization,
  1099. router: {
  1100. location: {query: {...router.location.query, display: 'previous'}},
  1101. },
  1102. });
  1103. rerender(
  1104. <Results
  1105. loading={false}
  1106. setSavedQuery={jest.fn()}
  1107. organization={organization}
  1108. location={rerenderData.router.location}
  1109. router={rerenderData.router}
  1110. />
  1111. );
  1112. screen.getByText('Previous Period');
  1113. expect(await screen.findByText('Set as Default')).toBeInTheDocument();
  1114. });
  1115. it('Changes the Use as Discover button to a reset button for prebuilt query', async () => {
  1116. MockApiClient.addMockResponse({
  1117. url: '/organizations/org-slug/discover/homepage/',
  1118. method: 'PUT',
  1119. statusCode: 200,
  1120. body: {...TRANSACTION_VIEWS[0], name: ''},
  1121. });
  1122. const organization = OrganizationFixture({
  1123. features: ['discover-basic', 'discover-query'],
  1124. });
  1125. const {router} = initializeOrg({
  1126. organization,
  1127. router: {
  1128. location: {
  1129. ...LocationFixture(),
  1130. query: {
  1131. ...EventView.fromNewQueryWithLocation(
  1132. TRANSACTION_VIEWS[0],
  1133. LocationFixture()
  1134. ).generateQueryStringObject(),
  1135. },
  1136. },
  1137. },
  1138. });
  1139. ProjectsStore.loadInitialData([ProjectFixture()]);
  1140. renderMockRequests();
  1141. const {rerender} = render(
  1142. <Results
  1143. organization={organization}
  1144. location={router.location}
  1145. router={router}
  1146. loading={false}
  1147. setSavedQuery={jest.fn()}
  1148. />,
  1149. {router: router, organization}
  1150. );
  1151. await screen.findAllByText(TRANSACTION_VIEWS[0].name);
  1152. await userEvent.click(screen.getByText('Set as Default'));
  1153. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  1154. await userEvent.click(screen.getByText('Total Period'));
  1155. await userEvent.click(screen.getByText('Previous Period'));
  1156. const rerenderData = initializeOrg({
  1157. organization,
  1158. router: {
  1159. location: {query: {...router.location.query, display: 'previous'}},
  1160. },
  1161. });
  1162. rerender(
  1163. <Results
  1164. organization={organization}
  1165. location={rerenderData.router.location}
  1166. router={rerenderData.router}
  1167. loading={false}
  1168. setSavedQuery={jest.fn()}
  1169. />
  1170. );
  1171. screen.getByText('Previous Period');
  1172. expect(await screen.findByText('Set as Default')).toBeInTheDocument();
  1173. });
  1174. it('links back to the homepage through the Discover breadcrumb', async () => {
  1175. const organization = OrganizationFixture({
  1176. features: ['discover-basic', 'discover-query'],
  1177. });
  1178. const {router} = initializeOrg({
  1179. organization,
  1180. router: {
  1181. location: {query: {id: '1'}},
  1182. },
  1183. });
  1184. ProjectsStore.loadInitialData([ProjectFixture()]);
  1185. const {measurementsMetaMock} = renderMockRequests();
  1186. render(
  1187. <Results
  1188. organization={organization}
  1189. location={router.location}
  1190. router={router}
  1191. loading={false}
  1192. setSavedQuery={jest.fn()}
  1193. />,
  1194. {router: router, organization}
  1195. );
  1196. await waitFor(() => {
  1197. expect(measurementsMetaMock).toHaveBeenCalled();
  1198. });
  1199. expect(screen.getByText('Discover')).toHaveAttribute(
  1200. 'href',
  1201. expect.stringMatching(new RegExp('^/organizations/org-slug/discover/homepage/'))
  1202. );
  1203. });
  1204. it('links back to the Saved Queries through the Saved Queries breadcrumb', async () => {
  1205. const organization = OrganizationFixture({
  1206. features: ['discover-basic', 'discover-query'],
  1207. });
  1208. const {router} = initializeOrg({
  1209. organization,
  1210. router: {
  1211. location: {query: {id: '1'}},
  1212. },
  1213. });
  1214. const {measurementsMetaMock} = renderMockRequests();
  1215. render(
  1216. <Results
  1217. organization={organization}
  1218. location={router.location}
  1219. router={router}
  1220. loading={false}
  1221. setSavedQuery={jest.fn()}
  1222. />,
  1223. {router: router, organization}
  1224. );
  1225. await waitFor(() => {
  1226. expect(measurementsMetaMock).toHaveBeenCalled();
  1227. });
  1228. expect(screen.getByRole('link', {name: 'Saved Queries'})).toHaveAttribute(
  1229. 'href',
  1230. expect.stringMatching(new RegExp('^/organizations/org-slug/discover/queries/'))
  1231. );
  1232. });
  1233. it('allows users to Set As Default on the All Events query', async () => {
  1234. const organization = OrganizationFixture({
  1235. features: ['discover-basic', 'discover-query'],
  1236. });
  1237. const {router} = initializeOrg({
  1238. organization,
  1239. router: {
  1240. location: {
  1241. ...LocationFixture(),
  1242. query: {
  1243. ...EventView.fromNewQueryWithLocation(
  1244. DEFAULT_EVENT_VIEW,
  1245. LocationFixture()
  1246. ).generateQueryStringObject(),
  1247. },
  1248. },
  1249. },
  1250. });
  1251. ProjectsStore.loadInitialData([ProjectFixture()]);
  1252. const {measurementsMetaMock} = renderMockRequests();
  1253. render(
  1254. <Results
  1255. organization={organization}
  1256. location={router.location}
  1257. router={router}
  1258. loading={false}
  1259. setSavedQuery={jest.fn()}
  1260. />,
  1261. {router: router, organization}
  1262. );
  1263. await waitFor(() => {
  1264. expect(measurementsMetaMock).toHaveBeenCalled();
  1265. });
  1266. expect(screen.getByTestId('set-as-default')).toBeEnabled();
  1267. });
  1268. it("doesn't render sample data alert", async function () {
  1269. const organization = OrganizationFixture({
  1270. features: ['discover-basic', 'discover-query'],
  1271. });
  1272. const {router} = initializeOrg({
  1273. organization,
  1274. router: {
  1275. location: {
  1276. ...LocationFixture(),
  1277. query: {
  1278. ...EventView.fromNewQueryWithLocation(
  1279. {...DEFAULT_EVENT_VIEW, query: 'event.type:error'},
  1280. LocationFixture()
  1281. ).generateQueryStringObject(),
  1282. },
  1283. },
  1284. },
  1285. });
  1286. const {measurementsMetaMock} = renderMockRequests();
  1287. render(
  1288. <Results
  1289. organization={organization}
  1290. location={router.location}
  1291. router={router}
  1292. loading={false}
  1293. setSavedQuery={jest.fn()}
  1294. />,
  1295. {router: router, organization}
  1296. );
  1297. await waitFor(() => {
  1298. expect(measurementsMetaMock).toHaveBeenCalled();
  1299. });
  1300. expect(screen.queryByText(/Based on your search criteria/)).not.toBeInTheDocument();
  1301. });
  1302. it('uses split decision to populate dataset selector', async function () {
  1303. const organization = OrganizationFixture({
  1304. features: [
  1305. 'discover-basic',
  1306. 'discover-query',
  1307. 'performance-discover-dataset-selector',
  1308. ],
  1309. });
  1310. const {router} = initializeOrg({
  1311. organization,
  1312. router: {
  1313. location: {query: {id: '1'}},
  1314. },
  1315. });
  1316. ProjectsStore.loadInitialData([ProjectFixture()]);
  1317. const mockRequests = renderMockRequests();
  1318. render(
  1319. <Results
  1320. organization={organization}
  1321. location={router.location}
  1322. router={router}
  1323. loading={false}
  1324. setSavedQuery={jest.fn()}
  1325. />,
  1326. {
  1327. router: router,
  1328. organization,
  1329. }
  1330. );
  1331. await waitFor(() => {
  1332. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  1333. });
  1334. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  1335. expect(
  1336. screen.getByRole('button', {name: 'Dataset Transactions'})
  1337. ).toBeInTheDocument();
  1338. });
  1339. });
  1340. });