homepage.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. waitFor,
  9. } from 'sentry-test/reactTestingLibrary';
  10. import * as pageFilterUtils from 'sentry/components/organizations/pageFilters/persistence';
  11. import ProjectsStore from 'sentry/stores/projectsStore';
  12. import EventView from 'sentry/utils/discover/eventView';
  13. import {DEFAULT_EVENT_VIEW} from './data';
  14. import Homepage from './homepage';
  15. describe('Discover > Homepage', () => {
  16. const features = ['global-views', 'discover-query'];
  17. let initialData, organization, mockHomepage, measurementsMetaMock;
  18. beforeEach(() => {
  19. organization = TestStubs.Organization({
  20. features,
  21. });
  22. initialData = initializeOrg({
  23. organization,
  24. router: {
  25. location: TestStubs.location(),
  26. },
  27. });
  28. MockApiClient.addMockResponse({
  29. url: '/organizations/org-slug/events/',
  30. body: [],
  31. });
  32. MockApiClient.addMockResponse({
  33. url: '/organizations/org-slug/events-meta/',
  34. body: {
  35. count: 2,
  36. },
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/events-stats/',
  40. body: {data: [[123, []]]},
  41. });
  42. MockApiClient.addMockResponse({
  43. url: '/organizations/org-slug/tags/',
  44. body: [],
  45. });
  46. MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/releases/stats/',
  48. body: [],
  49. });
  50. mockHomepage = MockApiClient.addMockResponse({
  51. url: '/organizations/org-slug/discover/homepage/',
  52. method: 'GET',
  53. statusCode: 200,
  54. body: {
  55. id: '2',
  56. name: 'homepage query',
  57. projects: [],
  58. version: 2,
  59. expired: false,
  60. dateCreated: '2021-04-08T17:53:25.195782Z',
  61. dateUpdated: '2021-04-09T12:13:18.567264Z',
  62. createdBy: {
  63. id: '2',
  64. },
  65. environment: ['alpha'],
  66. fields: ['environment'],
  67. widths: ['-1'],
  68. range: '24h',
  69. orderby: '-environment',
  70. display: 'previous',
  71. query: 'event.type:error',
  72. },
  73. });
  74. measurementsMetaMock = MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/measurements-meta/',
  76. method: 'GET',
  77. body: {},
  78. });
  79. });
  80. it('renders the Discover banner', async () => {
  81. render(
  82. <Homepage
  83. organization={organization}
  84. location={initialData.router.location}
  85. router={initialData.router}
  86. setSavedQuery={jest.fn()}
  87. loading={false}
  88. />,
  89. {context: initialData.routerContext, organization: initialData.organization}
  90. );
  91. await screen.findByText('Discover Trends');
  92. screen.getByText('Get a Tour');
  93. expect(screen.queryByText('Build a new query')).not.toBeInTheDocument();
  94. });
  95. it('fetches from the homepage URL and renders fields, async page filters, async and chart information', async () => {
  96. render(
  97. <Homepage
  98. organization={organization}
  99. location={initialData.router.location}
  100. router={initialData.router}
  101. setSavedQuery={jest.fn()}
  102. loading={false}
  103. />,
  104. {context: initialData.routerContext, organization: initialData.organization}
  105. );
  106. expect(mockHomepage).toHaveBeenCalled();
  107. await screen.findByText('environment');
  108. // Only the environment field
  109. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  110. screen.getByText('Previous Period');
  111. screen.getByText('event.type:error');
  112. });
  113. it('renders event view from URL params over homepage query', async () => {
  114. initialData = initializeOrg({
  115. organization,
  116. router: {
  117. location: {
  118. ...TestStubs.location(),
  119. query: {
  120. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  121. field: ['project'],
  122. },
  123. },
  124. },
  125. });
  126. render(
  127. <Homepage
  128. organization={organization}
  129. location={initialData.router.location}
  130. router={initialData.router}
  131. setSavedQuery={jest.fn()}
  132. loading={false}
  133. />,
  134. {context: initialData.routerContext, organization: initialData.organization}
  135. );
  136. expect(mockHomepage).toHaveBeenCalled();
  137. await screen.findByText('project');
  138. // This is the field in the mocked response for the homepage
  139. expect(screen.queryByText('environment')).not.toBeInTheDocument();
  140. });
  141. it('applies URL changes with the homepage pathname', async () => {
  142. render(
  143. <Homepage
  144. organization={organization}
  145. location={initialData.router.location}
  146. router={initialData.router}
  147. setSavedQuery={jest.fn()}
  148. loading={false}
  149. />,
  150. {context: initialData.routerContext, organization: initialData.organization}
  151. );
  152. renderGlobalModal();
  153. await userEvent.click(await screen.findByText('Columns'));
  154. await userEvent.click(screen.getByTestId('label'));
  155. await userEvent.click(screen.getByText('event.type'));
  156. await userEvent.click(screen.getByText('Apply'));
  157. expect(browserHistory.push).toHaveBeenCalledWith(
  158. expect.objectContaining({
  159. pathname: '/organizations/org-slug/discover/homepage/',
  160. query: expect.objectContaining({
  161. field: ['event.type'],
  162. }),
  163. })
  164. );
  165. });
  166. it('does not show an editable header or author information', async () => {
  167. render(
  168. <Homepage
  169. organization={organization}
  170. location={initialData.router.location}
  171. router={initialData.router}
  172. setSavedQuery={jest.fn()}
  173. loading={false}
  174. />,
  175. {context: initialData.routerContext, organization: initialData.organization}
  176. );
  177. await waitFor(() => {
  178. expect(measurementsMetaMock).toHaveBeenCalled();
  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', async () => {
  225. initialData = initializeOrg({
  226. organization,
  227. router: {
  228. location: {
  229. ...TestStubs.location(),
  230. query: {
  231. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  232. },
  233. },
  234. },
  235. });
  236. mockHomepage = MockApiClient.addMockResponse({
  237. url: '/organizations/org-slug/discover/homepage/',
  238. method: 'GET',
  239. statusCode: 200,
  240. });
  241. render(
  242. <Homepage
  243. organization={organization}
  244. location={initialData.router.location}
  245. router={initialData.router}
  246. setSavedQuery={jest.fn()}
  247. loading={false}
  248. />,
  249. {context: initialData.routerContext, organization: initialData.organization}
  250. );
  251. expect(mockHomepage).toHaveBeenCalled();
  252. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  253. await waitFor(() => {
  254. expect(measurementsMetaMock).toHaveBeenCalled();
  255. });
  256. });
  257. it('follows absolute date selection', async () => {
  258. initialData = initializeOrg({
  259. organization,
  260. router: {
  261. location: {
  262. ...TestStubs.location(),
  263. query: {
  264. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  265. },
  266. },
  267. },
  268. });
  269. MockApiClient.addMockResponse({
  270. url: '/organizations/org-slug/discover/homepage/',
  271. method: 'GET',
  272. statusCode: 200,
  273. });
  274. render(
  275. <Homepage
  276. organization={organization}
  277. location={initialData.router.location}
  278. router={initialData.router}
  279. setSavedQuery={jest.fn()}
  280. loading={false}
  281. />,
  282. {context: initialData.routerContext, organization: initialData.organization}
  283. );
  284. await userEvent.click(await screen.findByText('24H'));
  285. await userEvent.click(await screen.findByText('Absolute date'));
  286. await userEvent.click(screen.getByText('Apply'));
  287. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  288. });
  289. it('renders changes to the discover query when no homepage', async () => {
  290. initialData = 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. organization,
  322. router: {
  323. location: {
  324. ...TestStubs.location(),
  325. query: {
  326. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  327. field: ['event.type'],
  328. },
  329. },
  330. },
  331. });
  332. rerender(
  333. <Homepage
  334. organization={organization}
  335. location={rerenderData.router.location}
  336. router={rerenderData.router}
  337. setSavedQuery={jest.fn()}
  338. loading={false}
  339. />
  340. );
  341. await waitFor(() => {
  342. expect(measurementsMetaMock).toHaveBeenCalled();
  343. });
  344. expect(screen.getByText('event.type')).toBeInTheDocument();
  345. });
  346. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  347. initialData = initializeOrg({
  348. organization,
  349. router: {
  350. location: {
  351. ...TestStubs.location(),
  352. query: {
  353. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  354. field: ['title'],
  355. },
  356. },
  357. },
  358. });
  359. const {rerender} = render(
  360. <Homepage
  361. organization={organization}
  362. location={initialData.router.location}
  363. router={initialData.router}
  364. setSavedQuery={jest.fn()}
  365. loading={false}
  366. />,
  367. {context: initialData.routerContext, organization: initialData.organization}
  368. );
  369. renderGlobalModal();
  370. // Simulate an update to the columns by changing the URL params
  371. const rerenderData = 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. await waitFor(() => {
  393. expect(measurementsMetaMock).toHaveBeenCalled();
  394. });
  395. expect(screen.getByText('event.type')).toBeInTheDocument();
  396. });
  397. it('overrides homepage filters with pinned filters if they exist', async () => {
  398. ProjectsStore.loadInitialData([TestStubs.Project({id: 2})]);
  399. jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
  400. pinnedFilters: new Set(['projects']),
  401. state: {
  402. project: [2],
  403. environment: [],
  404. start: null,
  405. end: null,
  406. period: '14d',
  407. utc: null,
  408. },
  409. });
  410. render(
  411. <Homepage
  412. organization={organization}
  413. location={initialData.router.location}
  414. router={initialData.router}
  415. setSavedQuery={jest.fn()}
  416. loading={false}
  417. />,
  418. {context: initialData.routerContext, organization: initialData.organization}
  419. );
  420. await waitFor(() => {
  421. expect(measurementsMetaMock).toHaveBeenCalled();
  422. });
  423. expect(screen.getByText('project-slug')).toBeInTheDocument();
  424. });
  425. it('allows users to set the All Events query as default', async () => {
  426. initialData = initializeOrg({
  427. organization,
  428. router: {
  429. location: {
  430. ...TestStubs.location(),
  431. query: {
  432. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  433. },
  434. },
  435. },
  436. });
  437. mockHomepage = MockApiClient.addMockResponse({
  438. url: '/organizations/org-slug/discover/homepage/',
  439. method: 'GET',
  440. statusCode: 200,
  441. });
  442. render(
  443. <Homepage
  444. organization={organization}
  445. location={initialData.router.location}
  446. router={initialData.router}
  447. setSavedQuery={jest.fn()}
  448. loading={false}
  449. />,
  450. {context: initialData.routerContext, organization: initialData.organization}
  451. );
  452. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  453. });
  454. });