homepage.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. import {browserHistory} from 'react-router';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {
  5. render,
  6. renderGlobalModal,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import * as pageFilterUtils from 'sentry/components/organizations/pageFilters/persistence';
  12. import ProjectsStore from 'sentry/stores/projectsStore';
  13. import EventView from 'sentry/utils/discover/eventView';
  14. import {DEFAULT_EVENT_VIEW} from './data';
  15. import Homepage from './homepage';
  16. describe('Discover > Homepage', () => {
  17. const features = ['global-views', 'discover-query'];
  18. let initialData, organization, mockHomepage, measurementsMetaMock;
  19. beforeEach(() => {
  20. organization = Organization({
  21. features,
  22. });
  23. initialData = initializeOrg({
  24. organization,
  25. router: {
  26. location: TestStubs.location(),
  27. },
  28. });
  29. ProjectsStore.loadInitialData(organization.projects);
  30. MockApiClient.addMockResponse({
  31. url: '/organizations/org-slug/events/',
  32. body: [],
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/events-meta/',
  36. body: {
  37. count: 2,
  38. },
  39. });
  40. MockApiClient.addMockResponse({
  41. url: '/organizations/org-slug/events-stats/',
  42. body: {data: [[123, []]]},
  43. });
  44. MockApiClient.addMockResponse({
  45. url: '/organizations/org-slug/tags/',
  46. body: [],
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/releases/stats/',
  50. body: [],
  51. });
  52. mockHomepage = MockApiClient.addMockResponse({
  53. url: '/organizations/org-slug/discover/homepage/',
  54. method: 'GET',
  55. statusCode: 200,
  56. body: {
  57. id: '2',
  58. name: 'homepage query',
  59. projects: [],
  60. version: 2,
  61. expired: false,
  62. dateCreated: '2021-04-08T17:53:25.195782Z',
  63. dateUpdated: '2021-04-09T12:13:18.567264Z',
  64. createdBy: {
  65. id: '2',
  66. },
  67. environment: ['alpha'],
  68. fields: ['environment'],
  69. widths: ['-1'],
  70. range: '24h',
  71. orderby: '-environment',
  72. display: 'previous',
  73. query: 'event.type:error',
  74. },
  75. });
  76. measurementsMetaMock = MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/measurements-meta/',
  78. method: 'GET',
  79. body: {},
  80. });
  81. });
  82. it('renders the Discover banner', async () => {
  83. render(
  84. <Homepage
  85. organization={organization}
  86. location={initialData.router.location}
  87. router={initialData.router}
  88. setSavedQuery={jest.fn()}
  89. loading={false}
  90. />,
  91. {context: initialData.routerContext, organization: initialData.organization}
  92. );
  93. await screen.findByText('Discover Trends');
  94. screen.getByText('Get a Tour');
  95. expect(screen.queryByText('Build a new query')).not.toBeInTheDocument();
  96. });
  97. it('fetches from the homepage URL and renders fields, async page filters, async and chart information', async () => {
  98. render(
  99. <Homepage
  100. organization={organization}
  101. location={initialData.router.location}
  102. router={initialData.router}
  103. setSavedQuery={jest.fn()}
  104. loading={false}
  105. />,
  106. {context: initialData.routerContext, organization: initialData.organization}
  107. );
  108. expect(mockHomepage).toHaveBeenCalled();
  109. await screen.findByText('environment');
  110. // Only the environment field
  111. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  112. screen.getByText('Previous Period');
  113. screen.getByText('event.type:error');
  114. });
  115. it('renders event view from URL params over homepage query', async () => {
  116. initialData = initializeOrg({
  117. organization,
  118. router: {
  119. location: {
  120. ...TestStubs.location(),
  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. {context: initialData.routerContext, 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. {context: initialData.routerContext, organization: initialData.organization}
  153. );
  154. renderGlobalModal();
  155. await userEvent.click(await screen.findByText('Columns'));
  156. await userEvent.click(screen.getByTestId('label'));
  157. await userEvent.click(screen.getByText('event.type'));
  158. await userEvent.click(screen.getByText('Apply'));
  159. expect(browserHistory.push).toHaveBeenCalledWith(
  160. expect.objectContaining({
  161. pathname: '/organizations/org-slug/discover/homepage/',
  162. query: expect.objectContaining({
  163. field: ['event.type'],
  164. }),
  165. })
  166. );
  167. });
  168. it('does not show an editable header or author information', async () => {
  169. render(
  170. <Homepage
  171. organization={organization}
  172. location={initialData.router.location}
  173. router={initialData.router}
  174. setSavedQuery={jest.fn()}
  175. loading={false}
  176. />,
  177. {context: initialData.routerContext, organization: initialData.organization}
  178. );
  179. await waitFor(() => {
  180. expect(measurementsMetaMock).toHaveBeenCalled();
  181. });
  182. // 'Discover' is the header for the homepage
  183. expect(screen.getByText('Discover')).toBeInTheDocument();
  184. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  185. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  186. });
  187. it('shows the Remove Default button on initial load', async () => {
  188. MockApiClient.addMockResponse({
  189. url: '/organizations/org-slug/discover/homepage/',
  190. method: 'GET',
  191. statusCode: 200,
  192. body: {
  193. id: '2',
  194. name: 'homepage query',
  195. projects: [],
  196. version: 2,
  197. expired: false,
  198. dateCreated: '2021-04-08T17:53:25.195782Z',
  199. dateUpdated: '2021-04-09T12:13:18.567264Z',
  200. createdBy: {
  201. id: '2',
  202. },
  203. environment: [],
  204. fields: ['environment'],
  205. widths: ['-1'],
  206. range: '14d',
  207. orderby: '-environment',
  208. display: 'previous',
  209. query: 'event.type:error',
  210. topEvents: '5',
  211. },
  212. });
  213. render(
  214. <Homepage
  215. organization={organization}
  216. location={initialData.router.location}
  217. router={initialData.router}
  218. setSavedQuery={jest.fn()}
  219. loading={false}
  220. />,
  221. {context: initialData.routerContext, organization: initialData.organization}
  222. );
  223. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  224. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  225. });
  226. it('Disables the Set as Default button when no saved homepage', async () => {
  227. initialData = initializeOrg({
  228. organization,
  229. router: {
  230. location: {
  231. ...TestStubs.location(),
  232. query: {
  233. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  234. },
  235. },
  236. },
  237. });
  238. mockHomepage = MockApiClient.addMockResponse({
  239. url: '/organizations/org-slug/discover/homepage/',
  240. method: 'GET',
  241. statusCode: 200,
  242. });
  243. render(
  244. <Homepage
  245. organization={organization}
  246. location={initialData.router.location}
  247. router={initialData.router}
  248. setSavedQuery={jest.fn()}
  249. loading={false}
  250. />,
  251. {context: initialData.routerContext, organization: initialData.organization}
  252. );
  253. expect(mockHomepage).toHaveBeenCalled();
  254. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  255. await waitFor(() => {
  256. expect(measurementsMetaMock).toHaveBeenCalled();
  257. });
  258. });
  259. it('follows absolute date selection', async () => {
  260. initialData = initializeOrg({
  261. organization,
  262. router: {
  263. location: {
  264. ...TestStubs.location(),
  265. query: {
  266. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  267. },
  268. },
  269. },
  270. });
  271. MockApiClient.addMockResponse({
  272. url: '/organizations/org-slug/discover/homepage/',
  273. method: 'GET',
  274. statusCode: 200,
  275. });
  276. render(
  277. <Homepage
  278. organization={organization}
  279. location={initialData.router.location}
  280. router={initialData.router}
  281. setSavedQuery={jest.fn()}
  282. loading={false}
  283. />,
  284. {context: initialData.routerContext, organization: initialData.organization}
  285. );
  286. await userEvent.click(await screen.findByText('24H'));
  287. await userEvent.click(await screen.findByText('Absolute date'));
  288. await userEvent.click(screen.getByText('Apply'));
  289. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  290. });
  291. it('renders changes to the discover query when no homepage', async () => {
  292. initialData = initializeOrg({
  293. organization,
  294. router: {
  295. location: {
  296. ...TestStubs.location(),
  297. query: {
  298. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  299. field: ['title'],
  300. },
  301. },
  302. },
  303. });
  304. MockApiClient.addMockResponse({
  305. url: '/organizations/org-slug/discover/homepage/',
  306. method: 'GET',
  307. statusCode: 200,
  308. body: '',
  309. });
  310. const {rerender} = render(
  311. <Homepage
  312. organization={organization}
  313. location={initialData.router.location}
  314. router={initialData.router}
  315. setSavedQuery={jest.fn()}
  316. loading={false}
  317. />,
  318. {context: initialData.routerContext, organization: initialData.organization}
  319. );
  320. renderGlobalModal();
  321. // Simulate an update to the columns by changing the URL params
  322. const rerenderData = initializeOrg({
  323. organization,
  324. router: {
  325. location: {
  326. ...TestStubs.location(),
  327. query: {
  328. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  329. field: ['event.type'],
  330. },
  331. },
  332. },
  333. });
  334. rerender(
  335. <Homepage
  336. organization={organization}
  337. location={rerenderData.router.location}
  338. router={rerenderData.router}
  339. setSavedQuery={jest.fn()}
  340. loading={false}
  341. />
  342. );
  343. await waitFor(() => {
  344. expect(measurementsMetaMock).toHaveBeenCalled();
  345. });
  346. expect(screen.getByText('event.type')).toBeInTheDocument();
  347. });
  348. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  349. initialData = initializeOrg({
  350. organization,
  351. router: {
  352. location: {
  353. ...TestStubs.location(),
  354. query: {
  355. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  356. field: ['title'],
  357. },
  358. },
  359. },
  360. });
  361. const {rerender} = render(
  362. <Homepage
  363. organization={organization}
  364. location={initialData.router.location}
  365. router={initialData.router}
  366. setSavedQuery={jest.fn()}
  367. loading={false}
  368. />,
  369. {context: initialData.routerContext, organization: initialData.organization}
  370. );
  371. renderGlobalModal();
  372. // Simulate an update to the columns by changing the URL params
  373. const rerenderData = initializeOrg({
  374. organization,
  375. router: {
  376. location: {
  377. ...TestStubs.location(),
  378. query: {
  379. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  380. field: ['event.type'],
  381. },
  382. },
  383. },
  384. });
  385. rerender(
  386. <Homepage
  387. organization={organization}
  388. location={rerenderData.router.location}
  389. router={rerenderData.router}
  390. setSavedQuery={jest.fn()}
  391. loading={false}
  392. />
  393. );
  394. await waitFor(() => {
  395. expect(measurementsMetaMock).toHaveBeenCalled();
  396. });
  397. expect(screen.getByText('event.type')).toBeInTheDocument();
  398. });
  399. it('overrides homepage filters with pinned filters if they exist', async () => {
  400. ProjectsStore.loadInitialData([
  401. TestStubs.Project({id: 1}),
  402. TestStubs.Project({id: 2}),
  403. ]);
  404. jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
  405. pinnedFilters: new Set(['projects']),
  406. state: {
  407. project: [2],
  408. environment: [],
  409. start: null,
  410. end: null,
  411. period: '14d',
  412. utc: null,
  413. },
  414. });
  415. render(
  416. <Homepage
  417. organization={organization}
  418. location={initialData.router.location}
  419. router={initialData.router}
  420. setSavedQuery={jest.fn()}
  421. loading={false}
  422. />,
  423. {context: initialData.routerContext, organization: initialData.organization}
  424. );
  425. await waitFor(() => {
  426. expect(measurementsMetaMock).toHaveBeenCalled();
  427. });
  428. expect(screen.getByText('project-slug')).toBeInTheDocument();
  429. });
  430. it('allows users to set the All Events query as default', async () => {
  431. initialData = initializeOrg({
  432. organization,
  433. router: {
  434. location: {
  435. ...TestStubs.location(),
  436. query: {
  437. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  438. },
  439. },
  440. },
  441. });
  442. mockHomepage = MockApiClient.addMockResponse({
  443. url: '/organizations/org-slug/discover/homepage/',
  444. method: 'GET',
  445. statusCode: 200,
  446. });
  447. render(
  448. <Homepage
  449. organization={organization}
  450. location={initialData.router.location}
  451. router={initialData.router}
  452. setSavedQuery={jest.fn()}
  453. loading={false}
  454. />,
  455. {context: initialData.routerContext, organization: initialData.organization}
  456. );
  457. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  458. });
  459. });