results.spec.tsx 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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 {SavedSearchType} from 'sentry/types/group';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import Results from 'sentry/views/discover/results';
  12. import {DEFAULT_EVENT_VIEW, getTransactionViews} 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,
  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(router.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,
  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,
  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,
  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,
  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,
  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,
  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,
  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,
  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: {pathname: '/', 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,
  555. organization,
  556. }
  557. );
  558. await waitFor(() => expect(mockRequests.mockVisit).toHaveBeenCalled());
  559. expect(screen.getByRole('link', {name: 'timestamp'})).toHaveAttribute(
  560. 'href',
  561. '/?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&sort=-timestamp&statsPeriod=24h&topEvents=5'
  562. );
  563. expect(screen.getByRole('link', {name: 'project'})).toHaveAttribute(
  564. 'href',
  565. '/?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&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. '/?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&sort=user.display&statsPeriod=24h&topEvents=5'
  575. );
  576. expect(screen.getByRole('link', {name: 'title'})).toHaveAttribute(
  577. 'href',
  578. '/?field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&query=&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. pathname: '/',
  591. query: {
  592. id: '1',
  593. statsPeriod: '7d',
  594. project: ['2'],
  595. environment: ['production'],
  596. },
  597. },
  598. },
  599. });
  600. ProjectsStore.loadInitialData([ProjectFixture()]);
  601. const mockRequests = renderMockRequests();
  602. render(
  603. <Results
  604. organization={organization}
  605. location={router.location}
  606. router={router}
  607. loading={false}
  608. setSavedQuery={jest.fn()}
  609. />,
  610. {
  611. router,
  612. organization,
  613. }
  614. );
  615. await waitFor(() => expect(mockRequests.mockVisit).toHaveBeenCalled());
  616. expect(screen.getByRole('link', {name: 'timestamp'})).toHaveAttribute(
  617. 'href',
  618. '/?environment=production&field=title&field=event.type&field=project&field=user.display&field=timestamp&id=1&name=new&project=2&query=&sort=-timestamp&statsPeriod=7d&topEvents=5'
  619. );
  620. });
  621. it('updates chart whenever yAxis parameter changes', async function () {
  622. const organization = OrganizationFixture({
  623. features,
  624. });
  625. const {router} = initializeOrg({
  626. organization,
  627. router: {
  628. location: {query: {...generateFields(), yAxis: 'count()'}},
  629. },
  630. });
  631. ProjectsStore.loadInitialData([ProjectFixture()]);
  632. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  633. const {rerender} = render(
  634. <Results
  635. organization={organization}
  636. location={router.location}
  637. router={router}
  638. loading={false}
  639. setSavedQuery={jest.fn()}
  640. />,
  641. {
  642. router,
  643. organization,
  644. }
  645. );
  646. // Should load events once
  647. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  648. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  649. 1,
  650. '/organizations/org-slug/events-stats/',
  651. expect.objectContaining({
  652. query: expect.objectContaining({
  653. statsPeriod: '14d',
  654. yAxis: ['count()'],
  655. }),
  656. })
  657. );
  658. await waitFor(() => {
  659. expect(measurementsMetaMock).toHaveBeenCalled();
  660. });
  661. // Update location simulating a browser back button action
  662. rerender(
  663. <Results
  664. organization={organization}
  665. location={{
  666. ...router.location,
  667. query: {...generateFields(), yAxis: 'count_unique(user)'},
  668. }}
  669. router={router}
  670. loading={false}
  671. setSavedQuery={jest.fn()}
  672. />
  673. );
  674. // Should load events again
  675. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  676. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  677. 2,
  678. '/organizations/org-slug/events-stats/',
  679. expect.objectContaining({
  680. query: expect.objectContaining({
  681. statsPeriod: '14d',
  682. yAxis: ['count_unique(user)'],
  683. }),
  684. })
  685. );
  686. await waitFor(() => {
  687. expect(measurementsMetaMock).toHaveBeenCalled();
  688. });
  689. });
  690. it('updates chart whenever display parameter changes', async function () {
  691. const organization = OrganizationFixture({
  692. features,
  693. });
  694. const {router} = initializeOrg({
  695. organization,
  696. router: {
  697. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  698. },
  699. });
  700. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  701. ProjectsStore.loadInitialData([ProjectFixture()]);
  702. const {rerender} = render(
  703. <Results
  704. organization={organization}
  705. location={router.location}
  706. router={router}
  707. loading={false}
  708. setSavedQuery={jest.fn()}
  709. />,
  710. {
  711. router,
  712. organization,
  713. }
  714. );
  715. // Should load events once
  716. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  717. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  718. 1,
  719. '/organizations/org-slug/events-stats/',
  720. expect.objectContaining({
  721. query: expect.objectContaining({
  722. statsPeriod: '14d',
  723. yAxis: ['count()'],
  724. }),
  725. })
  726. );
  727. await waitFor(() => {
  728. expect(measurementsMetaMock).toHaveBeenCalled();
  729. });
  730. // Update location simulating a browser back button action
  731. rerender(
  732. <Results
  733. organization={organization}
  734. location={{
  735. ...router.location,
  736. query: {...generateFields(), display: 'previous', yAxis: 'count()'},
  737. }}
  738. router={router}
  739. loading={false}
  740. setSavedQuery={jest.fn()}
  741. />
  742. );
  743. // Should load events again
  744. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  745. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  746. 2,
  747. '/organizations/org-slug/events-stats/',
  748. expect.objectContaining({
  749. query: expect.objectContaining({
  750. statsPeriod: '28d',
  751. yAxis: ['count()'],
  752. }),
  753. })
  754. );
  755. await waitFor(() => {
  756. expect(measurementsMetaMock).toHaveBeenCalled();
  757. });
  758. });
  759. it('updates chart whenever display and yAxis parameters change', async function () {
  760. const organization = OrganizationFixture({
  761. features,
  762. });
  763. const {router} = initializeOrg({
  764. organization,
  765. router: {
  766. location: {query: {...generateFields(), display: 'default', yAxis: 'count()'}},
  767. },
  768. });
  769. const {eventsStatsMock, measurementsMetaMock} = renderMockRequests();
  770. ProjectsStore.loadInitialData([ProjectFixture()]);
  771. const {rerender} = render(
  772. <Results
  773. organization={organization}
  774. location={router.location}
  775. router={router}
  776. loading={false}
  777. setSavedQuery={jest.fn()}
  778. />,
  779. {
  780. router,
  781. organization,
  782. }
  783. );
  784. // Should load events once
  785. expect(eventsStatsMock).toHaveBeenCalledTimes(1);
  786. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  787. 1,
  788. '/organizations/org-slug/events-stats/',
  789. expect.objectContaining({
  790. query: expect.objectContaining({
  791. statsPeriod: '14d',
  792. yAxis: ['count()'],
  793. }),
  794. })
  795. );
  796. await waitFor(() => {
  797. expect(measurementsMetaMock).toHaveBeenCalled();
  798. });
  799. // Update location simulating a browser back button action
  800. rerender(
  801. <Results
  802. organization={organization}
  803. location={{
  804. ...router.location,
  805. query: {
  806. ...generateFields(),
  807. display: 'previous',
  808. yAxis: 'count_unique(user)',
  809. },
  810. }}
  811. router={router}
  812. loading={false}
  813. setSavedQuery={jest.fn()}
  814. />
  815. );
  816. // Should load events again
  817. expect(eventsStatsMock).toHaveBeenCalledTimes(2);
  818. expect(eventsStatsMock).toHaveBeenNthCalledWith(
  819. 2,
  820. '/organizations/org-slug/events-stats/',
  821. expect.objectContaining({
  822. query: expect.objectContaining({
  823. statsPeriod: '28d',
  824. yAxis: ['count_unique(user)'],
  825. }),
  826. })
  827. );
  828. await waitFor(() => {
  829. expect(measurementsMetaMock).toHaveBeenCalled();
  830. });
  831. });
  832. it('appends tag value to existing query when clicked', async function () {
  833. const organization = OrganizationFixture({
  834. features,
  835. });
  836. const {router} = initializeOrg({
  837. organization,
  838. router: {
  839. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  840. },
  841. });
  842. const mockRequests = renderMockRequests();
  843. ProjectsStore.loadInitialData([ProjectFixture()]);
  844. render(
  845. <Results
  846. organization={organization}
  847. location={router.location}
  848. router={router}
  849. loading={false}
  850. setSavedQuery={jest.fn()}
  851. />,
  852. {
  853. router,
  854. organization,
  855. }
  856. );
  857. await userEvent.click(await screen.findByRole('button', {name: 'Show Tags'}));
  858. await waitFor(() => expect(mockRequests.eventFacetsMock).toHaveBeenCalled());
  859. // TODO(edward): update this to be less generic
  860. await userEvent.click(screen.getByText('environment'));
  861. await userEvent.click(screen.getByText('foo'));
  862. // since environment collides with the environment field, it is wrapped with `tags[...]`
  863. expect(
  864. await screen.findByRole('link', {
  865. name: 'environment, dev, 100% of all events. View events with this tag value.',
  866. })
  867. ).toBeInTheDocument();
  868. expect(
  869. screen.getByRole('link', {
  870. name: 'foo, bar, 100% of all events. View events with this tag value.',
  871. })
  872. ).toBeInTheDocument();
  873. });
  874. it('respects pinned filters for prebuilt queries', async function () {
  875. const organization = OrganizationFixture({
  876. features: [...features, 'global-views'],
  877. });
  878. const {router} = initializeOrg({
  879. organization,
  880. router: {
  881. location: {query: {...generateFields(), display: 'default', yAxis: 'count'}},
  882. },
  883. });
  884. renderMockRequests();
  885. jest.spyOn(PageFilterPersistence, 'getPageFilterStorage').mockReturnValue({
  886. state: {
  887. project: [1],
  888. environment: [],
  889. start: null,
  890. end: null,
  891. period: '14d',
  892. utc: null,
  893. },
  894. pinnedFilters: new Set(['projects']),
  895. });
  896. ProjectsStore.loadInitialData([ProjectFixture({id: '1', slug: 'Pinned Project'})]);
  897. render(
  898. <Results
  899. organization={organization}
  900. location={router.location}
  901. router={router}
  902. loading={false}
  903. setSavedQuery={jest.fn()}
  904. />,
  905. {router, organization}
  906. );
  907. const projectPageFilter = await screen.findByTestId('page-filter-project-selector');
  908. expect(projectPageFilter).toHaveTextContent('All Projects');
  909. });
  910. it('displays tip when events response contains a tip', async function () {
  911. renderMockRequests();
  912. MockApiClient.addMockResponse({
  913. url: '/organizations/org-slug/events/',
  914. body: {
  915. meta: {
  916. fields: {},
  917. tips: {query: 'this is a tip'},
  918. },
  919. data: [],
  920. },
  921. });
  922. const organization = OrganizationFixture({
  923. features,
  924. });
  925. const {router} = initializeOrg({
  926. organization,
  927. router: {
  928. location: {query: {...generateFields(), yAxis: 'count()'}},
  929. },
  930. });
  931. ProjectsStore.loadInitialData([ProjectFixture()]);
  932. render(
  933. <Results
  934. organization={organization}
  935. location={router.location}
  936. router={router}
  937. loading={false}
  938. setSavedQuery={jest.fn()}
  939. />,
  940. {router, organization}
  941. );
  942. expect(await screen.findByText('this is a tip')).toBeInTheDocument();
  943. });
  944. it('renders metric fallback alert', async function () {
  945. const organization = OrganizationFixture({
  946. features: ['discover-basic'],
  947. });
  948. const {router} = initializeOrg({
  949. organization,
  950. router: {
  951. location: {query: {fromMetric: 'true', id: '1'}},
  952. },
  953. });
  954. ProjectsStore.loadInitialData([ProjectFixture()]);
  955. renderMockRequests();
  956. render(
  957. <Results
  958. organization={organization}
  959. location={router.location}
  960. router={router}
  961. loading={false}
  962. setSavedQuery={jest.fn()}
  963. />,
  964. {
  965. router,
  966. organization,
  967. }
  968. );
  969. expect(
  970. await screen.findByText(
  971. /You've navigated to this page from a performance metric widget generated from processed events/
  972. )
  973. ).toBeInTheDocument();
  974. });
  975. it('renders unparameterized data banner', async function () {
  976. const organization = OrganizationFixture({
  977. features: ['discover-basic'],
  978. });
  979. const {router} = initializeOrg({
  980. organization,
  981. router: {
  982. location: {query: {showUnparameterizedBanner: 'true', id: '1'}},
  983. },
  984. });
  985. ProjectsStore.loadInitialData([ProjectFixture()]);
  986. renderMockRequests();
  987. render(
  988. <Results
  989. organization={organization}
  990. location={router.location}
  991. router={router}
  992. loading={false}
  993. setSavedQuery={jest.fn()}
  994. />,
  995. {
  996. router,
  997. organization,
  998. }
  999. );
  1000. expect(
  1001. await screen.findByText(/These are unparameterized transactions/)
  1002. ).toBeInTheDocument();
  1003. });
  1004. it('updates the homepage query with up to date eventView when Set as Default is clicked', async () => {
  1005. const mockHomepageUpdate = MockApiClient.addMockResponse({
  1006. url: '/organizations/org-slug/discover/homepage/',
  1007. method: 'PUT',
  1008. statusCode: 200,
  1009. });
  1010. const organization = OrganizationFixture({
  1011. features: ['discover-basic', 'discover-query'],
  1012. });
  1013. const {router} = initializeOrg({
  1014. organization,
  1015. router: {
  1016. // These fields take priority and should be sent in the request
  1017. location: {query: {field: ['title', 'user'], id: '1'}},
  1018. },
  1019. });
  1020. ProjectsStore.loadInitialData([ProjectFixture()]);
  1021. renderMockRequests();
  1022. render(
  1023. <Results
  1024. organization={organization}
  1025. location={router.location}
  1026. router={router}
  1027. loading={false}
  1028. setSavedQuery={jest.fn()}
  1029. />,
  1030. {router, organization}
  1031. );
  1032. await waitFor(() =>
  1033. expect(screen.getByRole('button', {name: /set as default/i})).toBeEnabled()
  1034. );
  1035. await userEvent.click(screen.getByText('Set as Default'));
  1036. expect(mockHomepageUpdate).toHaveBeenCalledWith(
  1037. '/organizations/org-slug/discover/homepage/',
  1038. expect.objectContaining({
  1039. data: expect.objectContaining({
  1040. fields: ['title', 'user'],
  1041. }),
  1042. })
  1043. );
  1044. });
  1045. it('Changes the Use as Discover button to a reset button for saved query', async () => {
  1046. renderMockRequests();
  1047. MockApiClient.addMockResponse({
  1048. url: '/organizations/org-slug/discover/homepage/',
  1049. method: 'PUT',
  1050. statusCode: 200,
  1051. body: {
  1052. id: '2',
  1053. name: '',
  1054. projects: [],
  1055. version: 2,
  1056. expired: false,
  1057. dateCreated: '2021-04-08T17:53:25.195782Z',
  1058. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1059. createdBy: {
  1060. id: '2',
  1061. },
  1062. environment: [],
  1063. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1064. widths: ['-1', '-1', '-1', '-1', '-1'],
  1065. range: '24h',
  1066. orderby: '-user.display',
  1067. queryDataset: 'discover',
  1068. },
  1069. });
  1070. const organization = OrganizationFixture({
  1071. features: ['discover-basic', 'discover-query'],
  1072. });
  1073. const {router} = initializeOrg({
  1074. organization,
  1075. router: {
  1076. location: {query: {id: '1'}},
  1077. },
  1078. });
  1079. ProjectsStore.loadInitialData([ProjectFixture()]);
  1080. renderMockRequests();
  1081. const {rerender} = render(
  1082. <Results
  1083. loading={false}
  1084. setSavedQuery={jest.fn()}
  1085. organization={organization}
  1086. location={router.location}
  1087. router={router}
  1088. />,
  1089. {router, organization}
  1090. );
  1091. await waitFor(() =>
  1092. expect(screen.getByRole('button', {name: /set as default/i})).toBeEnabled()
  1093. );
  1094. await userEvent.click(screen.getByText('Set as Default'));
  1095. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  1096. await userEvent.click(screen.getByText('Total Period'));
  1097. await userEvent.click(screen.getByText('Previous Period'));
  1098. const rerenderData = initializeOrg({
  1099. organization,
  1100. router: {
  1101. location: {query: {...router.location.query, display: 'previous'}},
  1102. },
  1103. });
  1104. rerender(
  1105. <Results
  1106. loading={false}
  1107. setSavedQuery={jest.fn()}
  1108. organization={organization}
  1109. location={rerenderData.router.location}
  1110. router={rerenderData.router}
  1111. />
  1112. );
  1113. screen.getByText('Previous Period');
  1114. expect(await screen.findByText('Set as Default')).toBeInTheDocument();
  1115. });
  1116. it('Changes the Use as Discover button to a reset button for prebuilt query', async () => {
  1117. const organization = OrganizationFixture({
  1118. features: ['discover-basic', 'discover-query'],
  1119. });
  1120. MockApiClient.addMockResponse({
  1121. url: '/organizations/org-slug/discover/homepage/',
  1122. method: 'PUT',
  1123. statusCode: 200,
  1124. body: {...getTransactionViews(organization)[0], name: ''},
  1125. });
  1126. const {router} = initializeOrg({
  1127. organization,
  1128. router: {
  1129. location: {
  1130. ...LocationFixture(),
  1131. query: {
  1132. ...EventView.fromNewQueryWithLocation(
  1133. getTransactionViews(organization)[0]!,
  1134. LocationFixture()
  1135. ).generateQueryStringObject(),
  1136. },
  1137. },
  1138. },
  1139. });
  1140. ProjectsStore.loadInitialData([ProjectFixture()]);
  1141. renderMockRequests();
  1142. const {rerender} = render(
  1143. <Results
  1144. organization={organization}
  1145. location={router.location}
  1146. router={router}
  1147. loading={false}
  1148. setSavedQuery={jest.fn()}
  1149. />,
  1150. {router, organization}
  1151. );
  1152. await screen.findAllByText(getTransactionViews(organization)[0]!.name);
  1153. await userEvent.click(screen.getByText('Set as Default'));
  1154. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  1155. await userEvent.click(screen.getByText('Total Period'));
  1156. await userEvent.click(screen.getByText('Previous Period'));
  1157. const rerenderData = initializeOrg({
  1158. organization,
  1159. router: {
  1160. location: {query: {...router.location.query, display: 'previous'}},
  1161. },
  1162. });
  1163. rerender(
  1164. <Results
  1165. organization={organization}
  1166. location={rerenderData.router.location}
  1167. router={rerenderData.router}
  1168. loading={false}
  1169. setSavedQuery={jest.fn()}
  1170. />
  1171. );
  1172. screen.getByText('Previous Period');
  1173. expect(await screen.findByText('Set as Default')).toBeInTheDocument();
  1174. });
  1175. it('links back to the homepage through the Discover breadcrumb', async () => {
  1176. const organization = OrganizationFixture({
  1177. features: ['discover-basic', 'discover-query'],
  1178. });
  1179. const {router} = initializeOrg({
  1180. organization,
  1181. router: {
  1182. location: {query: {id: '1'}},
  1183. },
  1184. });
  1185. ProjectsStore.loadInitialData([ProjectFixture()]);
  1186. const {measurementsMetaMock} = renderMockRequests();
  1187. render(
  1188. <Results
  1189. organization={organization}
  1190. location={router.location}
  1191. router={router}
  1192. loading={false}
  1193. setSavedQuery={jest.fn()}
  1194. />,
  1195. {router, organization}
  1196. );
  1197. await waitFor(() => {
  1198. expect(measurementsMetaMock).toHaveBeenCalled();
  1199. });
  1200. expect(screen.getByText('Discover')).toHaveAttribute(
  1201. 'href',
  1202. expect.stringMatching(new RegExp('^/organizations/org-slug/discover/homepage/'))
  1203. );
  1204. });
  1205. it('links back to the Saved Queries through the Saved Queries breadcrumb', async () => {
  1206. const organization = OrganizationFixture({
  1207. features: ['discover-basic', 'discover-query'],
  1208. });
  1209. const {router} = initializeOrg({
  1210. organization,
  1211. router: {
  1212. location: {query: {id: '1'}},
  1213. },
  1214. });
  1215. const {measurementsMetaMock} = renderMockRequests();
  1216. render(
  1217. <Results
  1218. organization={organization}
  1219. location={router.location}
  1220. router={router}
  1221. loading={false}
  1222. setSavedQuery={jest.fn()}
  1223. />,
  1224. {router, organization}
  1225. );
  1226. await waitFor(() => {
  1227. expect(measurementsMetaMock).toHaveBeenCalled();
  1228. });
  1229. expect(screen.getByRole('link', {name: 'Saved Queries'})).toHaveAttribute(
  1230. 'href',
  1231. expect.stringMatching(new RegExp('^/organizations/org-slug/discover/queries/'))
  1232. );
  1233. });
  1234. it('allows users to Set As Default on the All Events query', async () => {
  1235. const organization = OrganizationFixture({
  1236. features: ['discover-basic', 'discover-query'],
  1237. });
  1238. const {router} = initializeOrg({
  1239. organization,
  1240. router: {
  1241. location: {
  1242. ...LocationFixture(),
  1243. query: {
  1244. ...EventView.fromNewQueryWithLocation(
  1245. DEFAULT_EVENT_VIEW,
  1246. LocationFixture()
  1247. ).generateQueryStringObject(),
  1248. },
  1249. },
  1250. },
  1251. });
  1252. ProjectsStore.loadInitialData([ProjectFixture()]);
  1253. const {measurementsMetaMock} = renderMockRequests();
  1254. render(
  1255. <Results
  1256. organization={organization}
  1257. location={router.location}
  1258. router={router}
  1259. loading={false}
  1260. setSavedQuery={jest.fn()}
  1261. />,
  1262. {router, organization}
  1263. );
  1264. await waitFor(() => {
  1265. expect(measurementsMetaMock).toHaveBeenCalled();
  1266. });
  1267. expect(screen.getByTestId('set-as-default')).toBeEnabled();
  1268. });
  1269. it("doesn't render sample data alert", async function () {
  1270. const organization = OrganizationFixture({
  1271. features: ['discover-basic', 'discover-query'],
  1272. });
  1273. const {router} = initializeOrg({
  1274. organization,
  1275. router: {
  1276. location: {
  1277. ...LocationFixture(),
  1278. query: {
  1279. ...EventView.fromNewQueryWithLocation(
  1280. {...DEFAULT_EVENT_VIEW, query: 'event.type:error'},
  1281. LocationFixture()
  1282. ).generateQueryStringObject(),
  1283. },
  1284. },
  1285. },
  1286. });
  1287. const {measurementsMetaMock} = renderMockRequests();
  1288. render(
  1289. <Results
  1290. organization={organization}
  1291. location={router.location}
  1292. router={router}
  1293. loading={false}
  1294. setSavedQuery={jest.fn()}
  1295. />,
  1296. {router, organization}
  1297. );
  1298. await waitFor(() => {
  1299. expect(measurementsMetaMock).toHaveBeenCalled();
  1300. });
  1301. expect(screen.queryByText(/Based on your search criteria/)).not.toBeInTheDocument();
  1302. });
  1303. it('uses split decision to populate dataset selector', async function () {
  1304. const organization = OrganizationFixture({
  1305. features: [
  1306. 'discover-basic',
  1307. 'discover-query',
  1308. 'performance-discover-dataset-selector',
  1309. ],
  1310. });
  1311. const {router} = initializeOrg({
  1312. organization,
  1313. router: {
  1314. location: {query: {id: '1'}},
  1315. },
  1316. });
  1317. ProjectsStore.loadInitialData([ProjectFixture()]);
  1318. const mockRequests = renderMockRequests();
  1319. render(
  1320. <Results
  1321. organization={organization}
  1322. location={router.location}
  1323. router={router}
  1324. loading={false}
  1325. setSavedQuery={jest.fn()}
  1326. />,
  1327. {
  1328. router,
  1329. organization,
  1330. }
  1331. );
  1332. await waitFor(() => {
  1333. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  1334. });
  1335. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  1336. await waitFor(() => {
  1337. expect(screen.getByRole('tab', {name: 'Transactions'})).toHaveAttribute(
  1338. 'aria-selected',
  1339. 'true'
  1340. );
  1341. });
  1342. expect(
  1343. screen.getByText(
  1344. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this query to Transactions. Edit as you see fit."
  1345. )
  1346. ).toBeInTheDocument();
  1347. expect(screen.queryByText('Save Changes')).not.toBeInTheDocument();
  1348. });
  1349. it('calls events endpoint with the right dataset', async function () {
  1350. const organization = OrganizationFixture({
  1351. features: [
  1352. 'discover-basic',
  1353. 'discover-query',
  1354. 'performance-discover-dataset-selector',
  1355. ],
  1356. });
  1357. const {router} = initializeOrg({
  1358. organization,
  1359. router: {
  1360. location: {query: {id: '1'}},
  1361. },
  1362. });
  1363. ProjectsStore.loadInitialData([ProjectFixture()]);
  1364. const mockRequests = renderMockRequests();
  1365. MockApiClient.addMockResponse({
  1366. url: '/organizations/org-slug/discover/saved/1/',
  1367. method: 'GET',
  1368. statusCode: 200,
  1369. body: {
  1370. id: '1',
  1371. name: 'new',
  1372. projects: [],
  1373. version: 2,
  1374. expired: false,
  1375. dateCreated: '2021-04-08T17:53:25.195782Z',
  1376. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1377. createdBy: {
  1378. id: '2',
  1379. },
  1380. environment: [],
  1381. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1382. widths: ['-1', '-1', '-1', '-1', '-1'],
  1383. range: '24h',
  1384. orderby: '-user.display',
  1385. queryDataset: 'error-events',
  1386. },
  1387. });
  1388. render(
  1389. <Results
  1390. organization={organization}
  1391. location={router.location}
  1392. router={router}
  1393. loading={false}
  1394. setSavedQuery={jest.fn()}
  1395. />,
  1396. {
  1397. router,
  1398. organization,
  1399. }
  1400. );
  1401. await waitFor(() => {
  1402. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  1403. });
  1404. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  1405. expect(screen.getByRole('tab', {name: 'Errors'})).toHaveAttribute(
  1406. 'aria-selected',
  1407. 'true'
  1408. );
  1409. expect(mockRequests.eventsStatsMock).toHaveBeenCalledWith(
  1410. '/organizations/org-slug/events-stats/',
  1411. expect.objectContaining({
  1412. query: expect.objectContaining({
  1413. dataset: 'errors',
  1414. }),
  1415. })
  1416. );
  1417. expect(mockRequests.eventsResultsMock).toHaveBeenCalledWith(
  1418. '/organizations/org-slug/events/',
  1419. expect.objectContaining({
  1420. query: expect.objectContaining({
  1421. dataset: 'errors',
  1422. }),
  1423. })
  1424. );
  1425. expect(mockRequests.eventsMetaMock).toHaveBeenCalledWith(
  1426. '/organizations/org-slug/events-meta/',
  1427. expect.objectContaining({
  1428. query: expect.objectContaining({
  1429. dataset: 'errors',
  1430. }),
  1431. })
  1432. );
  1433. });
  1434. it('does not automatically append dataset with selector feature disabled', async function () {
  1435. const organization = OrganizationFixture({
  1436. features: ['discover-basic', 'discover-query'],
  1437. });
  1438. const {router} = initializeOrg({
  1439. organization,
  1440. router: {
  1441. location: {query: {id: '1'}},
  1442. },
  1443. });
  1444. ProjectsStore.loadInitialData([ProjectFixture()]);
  1445. const mockRequests = renderMockRequests();
  1446. MockApiClient.addMockResponse({
  1447. url: '/organizations/org-slug/discover/saved/1/',
  1448. method: 'GET',
  1449. statusCode: 200,
  1450. body: {
  1451. id: '1',
  1452. name: 'new',
  1453. projects: [],
  1454. version: 2,
  1455. expired: false,
  1456. dateCreated: '2021-04-08T17:53:25.195782Z',
  1457. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1458. createdBy: {
  1459. id: '2',
  1460. },
  1461. environment: [],
  1462. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1463. widths: ['-1', '-1', '-1', '-1', '-1'],
  1464. range: '24h',
  1465. orderby: '-user.display',
  1466. queryDataset: 'error-events',
  1467. },
  1468. });
  1469. render(
  1470. <Results
  1471. organization={organization}
  1472. location={router.location}
  1473. router={router}
  1474. loading={false}
  1475. setSavedQuery={jest.fn()}
  1476. />,
  1477. {
  1478. router,
  1479. organization,
  1480. }
  1481. );
  1482. await waitFor(() => {
  1483. expect(mockRequests.measurementsMetaMock).toHaveBeenCalled();
  1484. });
  1485. expect(mockRequests.eventsResultsMock).toHaveBeenCalledTimes(1);
  1486. expect(
  1487. screen.queryByRole('button', {name: 'Dataset Errors'})
  1488. ).not.toBeInTheDocument();
  1489. expect(mockRequests.eventsStatsMock).toHaveBeenCalledWith(
  1490. '/organizations/org-slug/events-stats/',
  1491. expect.objectContaining({
  1492. query: expect.not.objectContaining({
  1493. dataset: 'errors',
  1494. }),
  1495. })
  1496. );
  1497. expect(mockRequests.eventsResultsMock).toHaveBeenCalledWith(
  1498. '/organizations/org-slug/events/',
  1499. expect.objectContaining({
  1500. query: expect.not.objectContaining({
  1501. dataset: 'errors',
  1502. }),
  1503. })
  1504. );
  1505. expect(mockRequests.eventsMetaMock).toHaveBeenCalledWith(
  1506. '/organizations/org-slug/events-meta/',
  1507. expect.objectContaining({
  1508. query: expect.not.objectContaining({
  1509. dataset: 'errors',
  1510. }),
  1511. })
  1512. );
  1513. });
  1514. it('shows the search history for the error dataset', async function () {
  1515. const organization = OrganizationFixture({
  1516. features: [
  1517. 'discover-basic',
  1518. 'discover-query',
  1519. 'performance-discover-dataset-selector',
  1520. ],
  1521. });
  1522. const {router} = initializeOrg({
  1523. organization,
  1524. router: {
  1525. location: {query: {id: '1'}},
  1526. },
  1527. });
  1528. ProjectsStore.loadInitialData([ProjectFixture()]);
  1529. renderMockRequests();
  1530. MockApiClient.addMockResponse({
  1531. url: '/organizations/org-slug/recent-searches/',
  1532. body: [
  1533. {
  1534. query: 'event.type:error',
  1535. },
  1536. ],
  1537. match: [
  1538. (_url, options) => {
  1539. return options.query?.type === SavedSearchType.ERROR;
  1540. },
  1541. ],
  1542. });
  1543. MockApiClient.addMockResponse({
  1544. url: '/organizations/org-slug/recent-searches/',
  1545. body: [
  1546. {
  1547. query: 'transaction.status:ok',
  1548. },
  1549. ],
  1550. match: [
  1551. (_url, options) => {
  1552. return options.query?.type === SavedSearchType.TRANSACTION;
  1553. },
  1554. ],
  1555. });
  1556. MockApiClient.addMockResponse({
  1557. url: '/organizations/org-slug/discover/saved/1/',
  1558. method: 'GET',
  1559. statusCode: 200,
  1560. body: {
  1561. id: '1',
  1562. name: 'new',
  1563. projects: [],
  1564. version: 2,
  1565. expired: false,
  1566. dateCreated: '2021-04-08T17:53:25.195782Z',
  1567. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1568. createdBy: {
  1569. id: '2',
  1570. },
  1571. environment: [],
  1572. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1573. widths: ['-1', '-1', '-1', '-1', '-1'],
  1574. range: '24h',
  1575. orderby: '-user.display',
  1576. queryDataset: 'error-events',
  1577. },
  1578. });
  1579. render(
  1580. <Results
  1581. organization={organization}
  1582. location={router.location}
  1583. router={router}
  1584. loading={false}
  1585. setSavedQuery={jest.fn()}
  1586. />,
  1587. {
  1588. router,
  1589. organization,
  1590. }
  1591. );
  1592. await userEvent.click(
  1593. screen.getByPlaceholderText('Search for events, users, tags, and more')
  1594. );
  1595. expect(
  1596. await screen.findByRole('option', {name: 'event.type:error'})
  1597. ).toBeInTheDocument();
  1598. });
  1599. it('shows the search history for the transaction dataset', async function () {
  1600. const organization = OrganizationFixture({
  1601. features: [
  1602. 'discover-basic',
  1603. 'discover-query',
  1604. 'performance-discover-dataset-selector',
  1605. ],
  1606. });
  1607. const {router} = initializeOrg({
  1608. organization,
  1609. router: {
  1610. location: {query: {id: '1'}},
  1611. },
  1612. });
  1613. ProjectsStore.loadInitialData([ProjectFixture()]);
  1614. renderMockRequests();
  1615. MockApiClient.addMockResponse({
  1616. url: '/organizations/org-slug/recent-searches/',
  1617. body: [
  1618. {
  1619. query: 'event.type:error',
  1620. },
  1621. ],
  1622. match: [
  1623. (_url, options) => {
  1624. return options.query?.type === SavedSearchType.ERROR;
  1625. },
  1626. ],
  1627. });
  1628. MockApiClient.addMockResponse({
  1629. url: '/organizations/org-slug/recent-searches/',
  1630. body: [
  1631. {
  1632. query: 'transaction.status:ok',
  1633. },
  1634. ],
  1635. match: [
  1636. (_url, options) => {
  1637. return options.query?.type === SavedSearchType.TRANSACTION;
  1638. },
  1639. ],
  1640. });
  1641. MockApiClient.addMockResponse({
  1642. url: '/organizations/org-slug/events/',
  1643. body: {
  1644. meta: {
  1645. fields: {
  1646. id: 'string',
  1647. title: 'string',
  1648. 'project.name': 'string',
  1649. timestamp: 'date',
  1650. 'user.id': 'string',
  1651. },
  1652. discoverSplitDecision: 'transaction-like',
  1653. },
  1654. data: [
  1655. {
  1656. trace: 'test',
  1657. id: 'deadbeef',
  1658. 'user.id': 'alberto leal',
  1659. title: eventTitle,
  1660. 'project.name': 'project-slug',
  1661. timestamp: '2019-05-23T22:12:48+00:00',
  1662. },
  1663. ],
  1664. },
  1665. });
  1666. MockApiClient.addMockResponse({
  1667. url: '/organizations/org-slug/discover/saved/1/',
  1668. method: 'GET',
  1669. statusCode: 200,
  1670. body: {
  1671. id: '1',
  1672. name: 'new',
  1673. projects: [],
  1674. version: 2,
  1675. expired: false,
  1676. dateCreated: '2021-04-08T17:53:25.195782Z',
  1677. dateUpdated: '2021-04-09T12:13:18.567264Z',
  1678. createdBy: {
  1679. id: '2',
  1680. },
  1681. environment: [],
  1682. fields: ['title', 'event.type', 'project', 'user.display', 'timestamp'],
  1683. widths: ['-1', '-1', '-1', '-1', '-1'],
  1684. range: '24h',
  1685. orderby: '-user.display',
  1686. queryDataset: 'transaction-like',
  1687. },
  1688. });
  1689. render(
  1690. <Results
  1691. organization={organization}
  1692. location={router.location}
  1693. router={router}
  1694. loading={false}
  1695. setSavedQuery={jest.fn()}
  1696. />,
  1697. {
  1698. router,
  1699. organization,
  1700. }
  1701. );
  1702. await userEvent.click(
  1703. screen.getByPlaceholderText('Search for events, users, tags, and more')
  1704. );
  1705. expect(
  1706. await screen.findByRole('option', {name: 'transaction.status:ok'})
  1707. ).toBeInTheDocument();
  1708. });
  1709. });
  1710. });