homepage.spec.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import * as pageFilterUtils from 'sentry/components/organizations/pageFilters/persistence';
  13. import ProjectsStore from 'sentry/stores/projectsStore';
  14. import {browserHistory} from 'sentry/utils/browserHistory';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {DEFAULT_EVENT_VIEW} from './data';
  17. import Homepage from './homepage';
  18. describe('Discover > Homepage', () => {
  19. const features = ['global-views', 'discover-query'];
  20. let initialData, organization, mockHomepage, measurementsMetaMock;
  21. beforeEach(() => {
  22. organization = OrganizationFixture({
  23. features,
  24. });
  25. initialData = initializeOrg({
  26. organization,
  27. router: {
  28. location: LocationFixture(),
  29. },
  30. });
  31. ProjectsStore.loadInitialData(initialData.projects);
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/events/',
  34. body: [],
  35. });
  36. MockApiClient.addMockResponse({
  37. url: '/organizations/org-slug/events-meta/',
  38. body: {
  39. count: 2,
  40. },
  41. });
  42. MockApiClient.addMockResponse({
  43. url: '/organizations/org-slug/events-stats/',
  44. body: {data: [[123, []]]},
  45. });
  46. MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/tags/',
  48. body: [],
  49. });
  50. MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/releases/stats/',
  52. body: [],
  53. });
  54. MockApiClient.addMockResponse({
  55. url: '/organizations/org-slug/dynamic-sampling/custom-rules/',
  56. body: '',
  57. });
  58. mockHomepage = MockApiClient.addMockResponse({
  59. url: '/organizations/org-slug/discover/homepage/',
  60. method: 'GET',
  61. statusCode: 200,
  62. body: {
  63. id: '2',
  64. name: 'homepage query',
  65. projects: [],
  66. version: 2,
  67. expired: false,
  68. dateCreated: '2021-04-08T17:53:25.195782Z',
  69. dateUpdated: '2021-04-09T12:13:18.567264Z',
  70. createdBy: {
  71. id: '2',
  72. },
  73. environment: ['alpha'],
  74. fields: ['environment'],
  75. widths: ['-1'],
  76. range: '24h',
  77. orderby: '-environment',
  78. display: 'previous',
  79. query: 'event.type:error',
  80. queryDataset: 'discover',
  81. },
  82. });
  83. measurementsMetaMock = MockApiClient.addMockResponse({
  84. url: '/organizations/org-slug/measurements-meta/',
  85. method: 'GET',
  86. body: {},
  87. });
  88. });
  89. it('renders the Discover banner', async () => {
  90. render(
  91. <Homepage
  92. organization={organization}
  93. location={initialData.router.location}
  94. router={initialData.router}
  95. setSavedQuery={jest.fn()}
  96. loading={false}
  97. />,
  98. {router: initialData.router, organization: initialData.organization}
  99. );
  100. await screen.findByText('Discover Trends');
  101. screen.getByText('Get a Tour');
  102. expect(screen.queryByText('Build a new query')).not.toBeInTheDocument();
  103. });
  104. it('fetches from the homepage URL and renders fields, async page filters, async and chart information', async () => {
  105. render(
  106. <Homepage
  107. organization={organization}
  108. location={initialData.router.location}
  109. router={initialData.router}
  110. setSavedQuery={jest.fn()}
  111. loading={false}
  112. />,
  113. {router: initialData.router, organization: initialData.organization}
  114. );
  115. expect(mockHomepage).toHaveBeenCalled();
  116. await screen.findByText('environment');
  117. // Only the environment field
  118. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  119. screen.getByText('Previous Period');
  120. screen.getByText('event.type:error');
  121. expect(screen.queryByText('Dataset')).not.toBeInTheDocument();
  122. });
  123. it('renders event view from URL params over homepage query', async () => {
  124. initialData = initializeOrg({
  125. organization,
  126. router: {
  127. location: {
  128. ...LocationFixture(),
  129. query: {
  130. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  131. field: ['project'],
  132. },
  133. },
  134. },
  135. });
  136. render(
  137. <Homepage
  138. organization={organization}
  139. location={initialData.router.location}
  140. router={initialData.router}
  141. setSavedQuery={jest.fn()}
  142. loading={false}
  143. />,
  144. {router: initialData.router, organization: initialData.organization}
  145. );
  146. expect(mockHomepage).toHaveBeenCalled();
  147. await screen.findByText('project');
  148. // This is the field in the mocked response for the homepage
  149. expect(screen.queryByText('environment')).not.toBeInTheDocument();
  150. });
  151. it('applies URL changes with the homepage pathname', async () => {
  152. render(
  153. <Homepage
  154. organization={organization}
  155. location={initialData.router.location}
  156. router={initialData.router}
  157. setSavedQuery={jest.fn()}
  158. loading={false}
  159. />,
  160. {router: initialData.router, organization: initialData.organization}
  161. );
  162. renderGlobalModal();
  163. await userEvent.click(await screen.findByText('Columns'));
  164. await userEvent.click(screen.getByTestId('label'));
  165. await userEvent.click(screen.getByText('event.type'));
  166. await userEvent.click(screen.getByText('Apply'));
  167. expect(browserHistory.push).toHaveBeenCalledWith(
  168. expect.objectContaining({
  169. pathname: '/organizations/org-slug/discover/homepage/',
  170. query: expect.objectContaining({
  171. field: 'event.type',
  172. }),
  173. })
  174. );
  175. });
  176. it('does not show an editable header or author information', async () => {
  177. render(
  178. <Homepage
  179. organization={organization}
  180. location={initialData.router.location}
  181. router={initialData.router}
  182. setSavedQuery={jest.fn()}
  183. loading={false}
  184. />,
  185. {router: initialData.router, organization: initialData.organization}
  186. );
  187. await waitFor(() => {
  188. expect(measurementsMetaMock).toHaveBeenCalled();
  189. });
  190. // 'Discover' is the header for the homepage
  191. expect(screen.getByText('Discover')).toBeInTheDocument();
  192. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  193. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  194. });
  195. it('shows the Remove Default button on initial load', async () => {
  196. MockApiClient.addMockResponse({
  197. url: '/organizations/org-slug/discover/homepage/',
  198. method: 'GET',
  199. statusCode: 200,
  200. body: {
  201. id: '2',
  202. name: 'homepage query',
  203. projects: [],
  204. version: 2,
  205. expired: false,
  206. dateCreated: '2021-04-08T17:53:25.195782Z',
  207. dateUpdated: '2021-04-09T12:13:18.567264Z',
  208. createdBy: {
  209. id: '2',
  210. },
  211. environment: [],
  212. fields: ['environment'],
  213. widths: ['-1'],
  214. range: '14d',
  215. orderby: '-environment',
  216. display: 'previous',
  217. query: 'event.type:error',
  218. topEvents: '5',
  219. },
  220. });
  221. render(
  222. <Homepage
  223. organization={organization}
  224. location={initialData.router.location}
  225. router={initialData.router}
  226. setSavedQuery={jest.fn()}
  227. loading={false}
  228. />,
  229. {router: initialData.router, organization: initialData.organization}
  230. );
  231. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  232. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  233. });
  234. it('Disables the Set as Default button when no saved homepage', async () => {
  235. initialData = initializeOrg({
  236. organization,
  237. router: {
  238. location: {
  239. ...LocationFixture(),
  240. query: {
  241. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  242. },
  243. },
  244. },
  245. });
  246. mockHomepage = MockApiClient.addMockResponse({
  247. url: '/organizations/org-slug/discover/homepage/',
  248. method: 'GET',
  249. statusCode: 200,
  250. });
  251. render(
  252. <Homepage
  253. organization={organization}
  254. location={initialData.router.location}
  255. router={initialData.router}
  256. setSavedQuery={jest.fn()}
  257. loading={false}
  258. />,
  259. {router: initialData.router, organization: initialData.organization}
  260. );
  261. expect(mockHomepage).toHaveBeenCalled();
  262. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  263. await waitFor(() => {
  264. expect(measurementsMetaMock).toHaveBeenCalled();
  265. });
  266. });
  267. it('follows absolute date selection', async () => {
  268. initialData = initializeOrg({
  269. organization,
  270. router: {
  271. location: {
  272. ...LocationFixture(),
  273. query: {
  274. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  275. },
  276. },
  277. },
  278. });
  279. MockApiClient.addMockResponse({
  280. url: '/organizations/org-slug/discover/homepage/',
  281. method: 'GET',
  282. statusCode: 200,
  283. });
  284. render(
  285. <Homepage
  286. organization={organization}
  287. location={initialData.router.location}
  288. router={initialData.router}
  289. setSavedQuery={jest.fn()}
  290. loading={false}
  291. />,
  292. {router: initialData.router, organization: initialData.organization}
  293. );
  294. await userEvent.click(await screen.findByText('24H'));
  295. await userEvent.click(await screen.findByText('Absolute date'));
  296. await userEvent.click(screen.getByText('Apply'));
  297. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  298. });
  299. it('renders changes to the discover query when no homepage', async () => {
  300. initialData = initializeOrg({
  301. organization,
  302. router: {
  303. location: {
  304. ...LocationFixture(),
  305. query: {
  306. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  307. field: ['title'],
  308. },
  309. },
  310. },
  311. });
  312. MockApiClient.addMockResponse({
  313. url: '/organizations/org-slug/discover/homepage/',
  314. method: 'GET',
  315. statusCode: 200,
  316. body: '',
  317. });
  318. const {rerender} = render(
  319. <Homepage
  320. organization={organization}
  321. location={initialData.router.location}
  322. router={initialData.router}
  323. setSavedQuery={jest.fn()}
  324. loading={false}
  325. />,
  326. {router: initialData.router, organization: initialData.organization}
  327. );
  328. renderGlobalModal();
  329. // Simulate an update to the columns by changing the URL params
  330. const rerenderData = initializeOrg({
  331. organization,
  332. router: {
  333. location: {
  334. ...LocationFixture(),
  335. query: {
  336. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  337. field: ['event.type'],
  338. },
  339. },
  340. },
  341. });
  342. rerender(
  343. <Homepage
  344. organization={organization}
  345. location={rerenderData.router.location}
  346. router={rerenderData.router}
  347. setSavedQuery={jest.fn()}
  348. loading={false}
  349. />
  350. );
  351. await waitFor(() => {
  352. expect(measurementsMetaMock).toHaveBeenCalled();
  353. });
  354. expect(screen.getByText('event.type')).toBeInTheDocument();
  355. });
  356. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  357. initialData = initializeOrg({
  358. organization,
  359. router: {
  360. location: {
  361. ...LocationFixture(),
  362. query: {
  363. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  364. field: ['title'],
  365. },
  366. },
  367. },
  368. });
  369. const {rerender} = render(
  370. <Homepage
  371. organization={organization}
  372. location={initialData.router.location}
  373. router={initialData.router}
  374. setSavedQuery={jest.fn()}
  375. loading={false}
  376. />,
  377. {router: initialData.router, organization: initialData.organization}
  378. );
  379. renderGlobalModal();
  380. // Simulate an update to the columns by changing the URL params
  381. const rerenderData = initializeOrg({
  382. organization,
  383. router: {
  384. location: {
  385. ...LocationFixture(),
  386. query: {
  387. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  388. field: ['event.type'],
  389. },
  390. },
  391. },
  392. });
  393. rerender(
  394. <Homepage
  395. organization={organization}
  396. location={rerenderData.router.location}
  397. router={rerenderData.router}
  398. setSavedQuery={jest.fn()}
  399. loading={false}
  400. />
  401. );
  402. await waitFor(() => {
  403. expect(measurementsMetaMock).toHaveBeenCalled();
  404. });
  405. expect(screen.getByText('event.type')).toBeInTheDocument();
  406. });
  407. it('overrides homepage filters with pinned filters if they exist', async () => {
  408. ProjectsStore.loadInitialData([ProjectFixture({id: '1'}), ProjectFixture({id: '2'})]);
  409. jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
  410. pinnedFilters: new Set(['projects']),
  411. state: {
  412. project: [2],
  413. environment: [],
  414. start: null,
  415. end: null,
  416. period: '14d',
  417. utc: null,
  418. },
  419. });
  420. render(
  421. <Homepage
  422. organization={organization}
  423. location={initialData.router.location}
  424. router={initialData.router}
  425. setSavedQuery={jest.fn()}
  426. loading={false}
  427. />,
  428. {router: initialData.router, organization: initialData.organization}
  429. );
  430. await waitFor(() => {
  431. expect(measurementsMetaMock).toHaveBeenCalled();
  432. });
  433. expect(screen.getByText('project-slug')).toBeInTheDocument();
  434. });
  435. it('allows users to set the All Events query as default', async () => {
  436. initialData = initializeOrg({
  437. organization,
  438. router: {
  439. location: {
  440. ...LocationFixture(),
  441. query: {
  442. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  443. },
  444. },
  445. },
  446. });
  447. mockHomepage = MockApiClient.addMockResponse({
  448. url: '/organizations/org-slug/discover/homepage/',
  449. method: 'GET',
  450. statusCode: 200,
  451. });
  452. render(
  453. <Homepage
  454. organization={organization}
  455. location={initialData.router.location}
  456. router={initialData.router}
  457. setSavedQuery={jest.fn()}
  458. loading={false}
  459. />,
  460. {router: initialData.router, organization: initialData.organization}
  461. );
  462. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  463. });
  464. it('uses split decision for homepage query', async () => {
  465. organization = OrganizationFixture({
  466. features: [
  467. 'discover-basic',
  468. 'discover-query',
  469. 'performance-discover-dataset-selector',
  470. ],
  471. });
  472. initialData = initializeOrg({
  473. organization: organization,
  474. router: {
  475. location: LocationFixture(),
  476. },
  477. });
  478. MockApiClient.addMockResponse({
  479. url: '/organizations/org-slug/events/',
  480. body: {
  481. meta: {
  482. discoverSplitDecision: 'error-events',
  483. },
  484. data: [],
  485. },
  486. });
  487. MockApiClient.addMockResponse({
  488. url: '/organizations/org-slug/discover/homepage/',
  489. method: 'GET',
  490. statusCode: 200,
  491. body: {
  492. id: '2',
  493. name: 'homepage query',
  494. projects: [],
  495. version: 2,
  496. expired: false,
  497. dateCreated: '2021-04-08T17:53:25.195782Z',
  498. dateUpdated: '2021-04-09T12:13:18.567264Z',
  499. createdBy: {
  500. id: '2',
  501. },
  502. environment: [],
  503. fields: ['environment'],
  504. widths: ['-1'],
  505. range: '14d',
  506. orderby: '-environment',
  507. display: 'previous',
  508. query: 'event.type:error',
  509. topEvents: '5',
  510. queryDataset: 'discover',
  511. },
  512. });
  513. render(
  514. <Homepage
  515. organization={organization}
  516. location={initialData.router.location}
  517. router={initialData.router}
  518. setSavedQuery={jest.fn()}
  519. loading={false}
  520. />,
  521. {router: initialData.router, organization: initialData.organization}
  522. );
  523. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  524. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  525. await screen.findByText('environment');
  526. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  527. screen.getByText('event.type:error');
  528. expect(screen.getByRole('button', {name: 'Dataset Errors'})).toBeInTheDocument();
  529. expect(
  530. screen.getByText(
  531. "We're splitting our datasets up to make it a bit easier to digest. We defaulted this query to Errors. Edit as you see fit."
  532. )
  533. ).toBeInTheDocument();
  534. });
  535. it('saves homepage with dataset selection', async () => {
  536. organization = OrganizationFixture({
  537. features: [
  538. 'discover-basic',
  539. 'discover-query',
  540. 'performance-discover-dataset-selector',
  541. ],
  542. });
  543. initialData = initializeOrg({
  544. organization: organization,
  545. router: {
  546. location: LocationFixture(),
  547. },
  548. });
  549. MockApiClient.addMockResponse({
  550. url: '/organizations/org-slug/events/',
  551. body: {
  552. meta: {
  553. discoverSplitDecision: 'error-events',
  554. },
  555. data: [],
  556. },
  557. });
  558. MockApiClient.addMockResponse({
  559. url: '/organizations/org-slug/discover/homepage/',
  560. method: 'GET',
  561. statusCode: 200,
  562. body: {
  563. id: '2',
  564. name: 'homepage query',
  565. projects: [],
  566. version: 2,
  567. expired: false,
  568. dateCreated: '2021-04-08T17:53:25.195782Z',
  569. dateUpdated: '2021-04-09T12:13:18.567264Z',
  570. createdBy: {
  571. id: '2',
  572. },
  573. environment: [],
  574. fields: ['environment'],
  575. widths: ['-1'],
  576. range: '14d',
  577. orderby: '-environment',
  578. display: 'previous',
  579. query: 'event.type:error',
  580. topEvents: '5',
  581. queryDataset: 'discover',
  582. },
  583. });
  584. render(
  585. <Homepage
  586. organization={organization}
  587. location={initialData.router.location}
  588. router={initialData.router}
  589. setSavedQuery={jest.fn()}
  590. loading={false}
  591. />,
  592. {router: initialData.router, organization: initialData.organization}
  593. );
  594. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  595. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  596. await userEvent.click(screen.getByText('Dataset'));
  597. await userEvent.click(screen.getByRole('option', {name: 'Transactions'}));
  598. expect(initialData.router.push).toHaveBeenCalledWith(
  599. expect.objectContaining({
  600. query: expect.objectContaining({
  601. dataset: 'transactions',
  602. name: 'homepage query',
  603. project: undefined,
  604. query: 'event.type:error',
  605. queryDataset: 'transaction-like',
  606. }),
  607. })
  608. );
  609. });
  610. });