homepage.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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/events/',
  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. it('renders changes to the discover query when no homepage', () => {
  289. initialData = initializeOrg({
  290. ...initializeOrg(),
  291. organization,
  292. router: {
  293. location: {
  294. ...TestStubs.location(),
  295. query: {
  296. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  297. field: ['title'],
  298. },
  299. },
  300. },
  301. });
  302. MockApiClient.addMockResponse({
  303. url: '/organizations/org-slug/discover/homepage/',
  304. method: 'GET',
  305. statusCode: 200,
  306. body: '',
  307. });
  308. const {rerender} = render(
  309. <Homepage
  310. organization={organization}
  311. location={initialData.router.location}
  312. router={initialData.router}
  313. setSavedQuery={jest.fn()}
  314. loading={false}
  315. />,
  316. {context: initialData.routerContext, organization: initialData.organization}
  317. );
  318. renderGlobalModal();
  319. // Simulate an update to the columns by changing the URL params
  320. const rerenderData = initializeOrg({
  321. ...initializeOrg(),
  322. organization,
  323. router: {
  324. location: {
  325. ...TestStubs.location(),
  326. query: {
  327. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  328. field: ['event.type'],
  329. },
  330. },
  331. },
  332. });
  333. rerender(
  334. <Homepage
  335. organization={organization}
  336. location={rerenderData.router.location}
  337. router={rerenderData.router}
  338. setSavedQuery={jest.fn()}
  339. loading={false}
  340. />
  341. );
  342. expect(screen.getByText('event.type')).toBeInTheDocument();
  343. });
  344. it('renders changes to the discover query when loaded with valid event view in url params', () => {
  345. initialData = initializeOrg({
  346. ...initializeOrg(),
  347. organization,
  348. router: {
  349. location: {
  350. ...TestStubs.location(),
  351. query: {
  352. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  353. field: ['title'],
  354. },
  355. },
  356. },
  357. });
  358. const {rerender} = render(
  359. <Homepage
  360. organization={organization}
  361. location={initialData.router.location}
  362. router={initialData.router}
  363. setSavedQuery={jest.fn()}
  364. loading={false}
  365. />,
  366. {context: initialData.routerContext, organization: initialData.organization}
  367. );
  368. renderGlobalModal();
  369. // Simulate an update to the columns by changing the URL params
  370. const rerenderData = initializeOrg({
  371. ...initializeOrg(),
  372. organization,
  373. router: {
  374. location: {
  375. ...TestStubs.location(),
  376. query: {
  377. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  378. field: ['event.type'],
  379. },
  380. },
  381. },
  382. });
  383. rerender(
  384. <Homepage
  385. organization={organization}
  386. location={rerenderData.router.location}
  387. router={rerenderData.router}
  388. setSavedQuery={jest.fn()}
  389. loading={false}
  390. />
  391. );
  392. expect(screen.getByText('event.type')).toBeInTheDocument();
  393. });
  394. it('overrides homepage filters with pinned filters if they exist', () => {
  395. ProjectsStore.loadInitialData([TestStubs.Project({id: 2})]);
  396. jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
  397. pinnedFilters: new Set(['projects']),
  398. state: {
  399. project: [2],
  400. environment: [],
  401. start: null,
  402. end: null,
  403. period: '14d',
  404. utc: null,
  405. },
  406. });
  407. render(
  408. <Homepage
  409. organization={organization}
  410. location={initialData.router.location}
  411. router={initialData.router}
  412. setSavedQuery={jest.fn()}
  413. loading={false}
  414. />,
  415. {context: initialData.routerContext, organization: initialData.organization}
  416. );
  417. expect(screen.getByText('project-slug')).toBeInTheDocument();
  418. });
  419. it('allows users to set the All Events query as default', async () => {
  420. initialData = initializeOrg({
  421. ...initializeOrg(),
  422. organization,
  423. router: {
  424. location: {
  425. ...TestStubs.location(),
  426. query: {
  427. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  428. },
  429. },
  430. },
  431. });
  432. mockHomepage = MockApiClient.addMockResponse({
  433. url: '/organizations/org-slug/discover/homepage/',
  434. method: 'GET',
  435. statusCode: 200,
  436. });
  437. render(
  438. <Homepage
  439. organization={organization}
  440. location={initialData.router.location}
  441. router={initialData.router}
  442. setSavedQuery={jest.fn()}
  443. loading={false}
  444. />,
  445. {context: initialData.routerContext, organization: initialData.organization}
  446. );
  447. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  448. });
  449. });