homepage.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. act,
  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 = [
  18. 'global-views',
  19. 'discover-query',
  20. 'discover-query-builder-as-landing-page',
  21. ];
  22. let initialData, organization, mockHomepage;
  23. beforeEach(() => {
  24. organization = TestStubs.Organization({
  25. features,
  26. });
  27. initialData = initializeOrg({
  28. ...initializeOrg(),
  29. organization,
  30. router: {
  31. location: TestStubs.location(),
  32. },
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/eventsv2/',
  36. body: [],
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/events-meta/',
  40. body: {
  41. count: 2,
  42. },
  43. });
  44. MockApiClient.addMockResponse({
  45. url: '/organizations/org-slug/events-stats/',
  46. body: {data: [[123, []]]},
  47. });
  48. MockApiClient.addMockResponse({
  49. url: '/organizations/org-slug/tags/',
  50. body: [],
  51. });
  52. MockApiClient.addMockResponse({
  53. url: '/organizations/org-slug/releases/stats/',
  54. body: [],
  55. });
  56. mockHomepage = MockApiClient.addMockResponse({
  57. url: '/organizations/org-slug/discover/homepage/',
  58. method: 'GET',
  59. statusCode: 200,
  60. body: {
  61. id: '2',
  62. name: 'homepage query',
  63. projects: [],
  64. version: 2,
  65. expired: false,
  66. dateCreated: '2021-04-08T17:53:25.195782Z',
  67. dateUpdated: '2021-04-09T12:13:18.567264Z',
  68. createdBy: {
  69. id: '2',
  70. },
  71. environment: ['alpha'],
  72. fields: ['environment'],
  73. widths: ['-1'],
  74. range: '24h',
  75. orderby: '-environment',
  76. display: 'previous',
  77. query: 'event.type:error',
  78. },
  79. });
  80. });
  81. it('renders the Discover banner', async () => {
  82. render(
  83. <Homepage
  84. organization={organization}
  85. location={initialData.router.location}
  86. router={initialData.router}
  87. setSavedQuery={jest.fn()}
  88. loading={false}
  89. />,
  90. {context: initialData.routerContext, organization: initialData.organization}
  91. );
  92. await screen.findByText('Discover Trends');
  93. screen.getByText('Get a Tour');
  94. expect(screen.queryByText('Build a new query')).not.toBeInTheDocument();
  95. });
  96. it('fetches from the homepage URL and renders fields, page filters, 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. {context: initialData.routerContext, 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').length).toEqual(1);
  111. screen.getByText('Previous Period');
  112. screen.getByText('event.type:error');
  113. });
  114. it('renders event view from URL params over homepage query', async () => {
  115. initialData = initializeOrg({
  116. ...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. await act(tick);
  155. renderGlobalModal();
  156. userEvent.click(screen.getByText('Columns'));
  157. userEvent.click(screen.getByTestId('label'));
  158. userEvent.click(screen.getByText('event.type'));
  159. userEvent.click(screen.getByText('Apply'));
  160. expect(browserHistory.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', () => {
  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. {context: initialData.routerContext, organization: initialData.organization}
  179. );
  180. // 'Discover' is the header for the homepage
  181. expect(screen.getByText('Discover')).toBeInTheDocument();
  182. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  183. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  184. });
  185. it('shows the Remove Default button on initial load', async () => {
  186. MockApiClient.addMockResponse({
  187. url: '/organizations/org-slug/discover/homepage/',
  188. method: 'GET',
  189. statusCode: 200,
  190. body: {
  191. id: '2',
  192. name: 'homepage query',
  193. projects: [],
  194. version: 2,
  195. expired: false,
  196. dateCreated: '2021-04-08T17:53:25.195782Z',
  197. dateUpdated: '2021-04-09T12:13:18.567264Z',
  198. createdBy: {
  199. id: '2',
  200. },
  201. environment: [],
  202. fields: ['environment'],
  203. widths: ['-1'],
  204. range: '14d',
  205. orderby: '-environment',
  206. display: 'previous',
  207. query: 'event.type:error',
  208. topEvents: '5',
  209. },
  210. });
  211. render(
  212. <Homepage
  213. organization={organization}
  214. location={initialData.router.location}
  215. router={initialData.router}
  216. setSavedQuery={jest.fn()}
  217. loading={false}
  218. />,
  219. {context: initialData.routerContext, organization: initialData.organization}
  220. );
  221. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  222. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  223. });
  224. it('Disables the Set as Default button when no saved homepage', () => {
  225. initialData = initializeOrg({
  226. ...initializeOrg(),
  227. organization,
  228. router: {
  229. location: {
  230. ...TestStubs.location(),
  231. query: {
  232. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  233. },
  234. },
  235. },
  236. });
  237. mockHomepage = MockApiClient.addMockResponse({
  238. url: '/organizations/org-slug/discover/homepage/',
  239. method: 'GET',
  240. statusCode: 200,
  241. });
  242. render(
  243. <Homepage
  244. organization={organization}
  245. location={initialData.router.location}
  246. router={initialData.router}
  247. setSavedQuery={jest.fn()}
  248. loading={false}
  249. />,
  250. {context: initialData.routerContext, organization: initialData.organization}
  251. );
  252. expect(mockHomepage).toHaveBeenCalled();
  253. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  254. });
  255. it('follows absolute date selection', async () => {
  256. initialData = initializeOrg({
  257. ...initializeOrg(),
  258. organization,
  259. router: {
  260. location: {
  261. ...TestStubs.location(),
  262. query: {
  263. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  264. },
  265. },
  266. },
  267. });
  268. MockApiClient.addMockResponse({
  269. url: '/organizations/org-slug/discover/homepage/',
  270. method: 'GET',
  271. statusCode: 200,
  272. });
  273. render(
  274. <Homepage
  275. organization={organization}
  276. location={initialData.router.location}
  277. router={initialData.router}
  278. setSavedQuery={jest.fn()}
  279. loading={false}
  280. />,
  281. {context: initialData.routerContext, organization: initialData.organization}
  282. );
  283. userEvent.click(await screen.findByText('24H'));
  284. userEvent.click(await screen.findByText('Absolute date'));
  285. userEvent.click(screen.getByText('Apply'));
  286. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  287. });
  288. // eslint-disable-next-line jest/no-disabled-tests
  289. it.skip('flaky: DD-1151: renders changes to the discover query when no homepage', async () => {
  290. initialData = initializeOrg({
  291. ...initializeOrg(),
  292. organization,
  293. router: {
  294. location: {
  295. ...TestStubs.location(),
  296. query: {
  297. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  298. field: ['title'],
  299. },
  300. },
  301. },
  302. });
  303. MockApiClient.addMockResponse({
  304. url: '/organizations/org-slug/discover/homepage/',
  305. method: 'GET',
  306. statusCode: 200,
  307. body: '',
  308. });
  309. const {rerender} = render(
  310. <Homepage
  311. organization={organization}
  312. location={initialData.router.location}
  313. router={initialData.router}
  314. setSavedQuery={jest.fn()}
  315. loading={false}
  316. />,
  317. {context: initialData.routerContext, organization: initialData.organization}
  318. );
  319. renderGlobalModal();
  320. userEvent.click(screen.getByText('Columns'));
  321. userEvent.click(screen.getByTestId('label'));
  322. userEvent.click(screen.getByText('event.type'));
  323. userEvent.click(screen.getByText('Apply'));
  324. const rerenderData = initializeOrg({
  325. ...initializeOrg(),
  326. organization,
  327. router: {
  328. location: {
  329. ...TestStubs.location(),
  330. query: {
  331. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  332. field: ['event.type'],
  333. },
  334. },
  335. },
  336. });
  337. rerender(
  338. <Homepage
  339. organization={organization}
  340. location={rerenderData.router.location}
  341. router={rerenderData.router}
  342. setSavedQuery={jest.fn()}
  343. loading={false}
  344. />
  345. );
  346. await waitFor(() =>
  347. expect(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  348. );
  349. expect(screen.getByText('event.type')).toBeInTheDocument();
  350. });
  351. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  352. initialData = initializeOrg({
  353. ...initializeOrg(),
  354. organization,
  355. router: {
  356. location: {
  357. ...TestStubs.location(),
  358. query: {
  359. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  360. field: ['title'],
  361. },
  362. },
  363. },
  364. });
  365. const {rerender} = render(
  366. <Homepage
  367. organization={organization}
  368. location={initialData.router.location}
  369. router={initialData.router}
  370. setSavedQuery={jest.fn()}
  371. loading={false}
  372. />,
  373. {context: initialData.routerContext, organization: initialData.organization}
  374. );
  375. renderGlobalModal();
  376. userEvent.click(screen.getByText('Columns'));
  377. userEvent.click(screen.getByTestId('label'));
  378. userEvent.click(screen.getByText('event.type'));
  379. userEvent.click(screen.getByText('Apply'));
  380. const rerenderData = initializeOrg({
  381. ...initializeOrg(),
  382. organization,
  383. router: {
  384. location: {
  385. ...TestStubs.location(),
  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(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  404. );
  405. expect(screen.getByText('event.type')).toBeInTheDocument();
  406. });
  407. it('overrides homepage filters with pinned filters if they exist', () => {
  408. ProjectsStore.loadInitialData([TestStubs.Project({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. {context: initialData.routerContext, organization: initialData.organization}
  429. );
  430. expect(screen.getByText('project-slug')).toBeInTheDocument();
  431. });
  432. it('allows users to set the All Events query as default', async () => {
  433. initialData = initializeOrg({
  434. ...initializeOrg(),
  435. organization,
  436. router: {
  437. location: {
  438. ...TestStubs.location(),
  439. query: {
  440. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  441. },
  442. },
  443. },
  444. });
  445. mockHomepage = MockApiClient.addMockResponse({
  446. url: '/organizations/org-slug/discover/homepage/',
  447. method: 'GET',
  448. statusCode: 200,
  449. });
  450. render(
  451. <Homepage
  452. organization={organization}
  453. location={initialData.router.location}
  454. router={initialData.router}
  455. setSavedQuery={jest.fn()}
  456. loading={false}
  457. />,
  458. {context: initialData.routerContext, organization: initialData.organization}
  459. );
  460. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  461. });
  462. });