homepage.spec.tsx 18 KB

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