homepage.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. MockApiClient.addMockResponse({
  30. url: '/organizations/org-slug/events/',
  31. body: [],
  32. });
  33. MockApiClient.addMockResponse({
  34. url: '/organizations/org-slug/events-meta/',
  35. body: {
  36. count: 2,
  37. },
  38. });
  39. MockApiClient.addMockResponse({
  40. url: '/organizations/org-slug/events-stats/',
  41. body: {data: [[123, []]]},
  42. });
  43. MockApiClient.addMockResponse({
  44. url: '/organizations/org-slug/tags/',
  45. body: [],
  46. });
  47. MockApiClient.addMockResponse({
  48. url: '/organizations/org-slug/releases/stats/',
  49. body: [],
  50. });
  51. mockHomepage = MockApiClient.addMockResponse({
  52. url: '/organizations/org-slug/discover/homepage/',
  53. method: 'GET',
  54. statusCode: 200,
  55. body: {
  56. id: '2',
  57. name: 'homepage query',
  58. projects: [],
  59. version: 2,
  60. expired: false,
  61. dateCreated: '2021-04-08T17:53:25.195782Z',
  62. dateUpdated: '2021-04-09T12:13:18.567264Z',
  63. createdBy: {
  64. id: '2',
  65. },
  66. environment: ['alpha'],
  67. fields: ['environment'],
  68. widths: ['-1'],
  69. range: '24h',
  70. orderby: '-environment',
  71. display: 'previous',
  72. query: 'event.type:error',
  73. },
  74. });
  75. measurementsMetaMock = MockApiClient.addMockResponse({
  76. url: '/organizations/org-slug/measurements-meta/',
  77. method: 'GET',
  78. body: {},
  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, 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. {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. organization,
  117. router: {
  118. location: {
  119. ...TestStubs.location(),
  120. query: {
  121. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  122. field: ['project'],
  123. },
  124. },
  125. },
  126. });
  127. render(
  128. <Homepage
  129. organization={organization}
  130. location={initialData.router.location}
  131. router={initialData.router}
  132. setSavedQuery={jest.fn()}
  133. loading={false}
  134. />,
  135. {context: initialData.routerContext, organization: initialData.organization}
  136. );
  137. expect(mockHomepage).toHaveBeenCalled();
  138. await screen.findByText('project');
  139. // This is the field in the mocked response for the homepage
  140. expect(screen.queryByText('environment')).not.toBeInTheDocument();
  141. });
  142. it('applies URL changes with the homepage pathname', async () => {
  143. render(
  144. <Homepage
  145. organization={organization}
  146. location={initialData.router.location}
  147. router={initialData.router}
  148. setSavedQuery={jest.fn()}
  149. loading={false}
  150. />,
  151. {context: initialData.routerContext, organization: initialData.organization}
  152. );
  153. renderGlobalModal();
  154. await userEvent.click(await screen.findByText('Columns'));
  155. await userEvent.click(screen.getByTestId('label'));
  156. await userEvent.click(screen.getByText('event.type'));
  157. await userEvent.click(screen.getByText('Apply'));
  158. expect(browserHistory.push).toHaveBeenCalledWith(
  159. expect.objectContaining({
  160. pathname: '/organizations/org-slug/discover/homepage/',
  161. query: expect.objectContaining({
  162. field: ['event.type'],
  163. }),
  164. })
  165. );
  166. });
  167. it('does not show an editable header or author information', async () => {
  168. render(
  169. <Homepage
  170. organization={organization}
  171. location={initialData.router.location}
  172. router={initialData.router}
  173. setSavedQuery={jest.fn()}
  174. loading={false}
  175. />,
  176. {context: initialData.routerContext, organization: initialData.organization}
  177. );
  178. await waitFor(() => {
  179. expect(measurementsMetaMock).toHaveBeenCalled();
  180. });
  181. // 'Discover' is the header for the homepage
  182. expect(screen.getByText('Discover')).toBeInTheDocument();
  183. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  184. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  185. });
  186. it('shows the Remove Default button on initial load', async () => {
  187. MockApiClient.addMockResponse({
  188. url: '/organizations/org-slug/discover/homepage/',
  189. method: 'GET',
  190. statusCode: 200,
  191. body: {
  192. id: '2',
  193. name: 'homepage query',
  194. projects: [],
  195. version: 2,
  196. expired: false,
  197. dateCreated: '2021-04-08T17:53:25.195782Z',
  198. dateUpdated: '2021-04-09T12:13:18.567264Z',
  199. createdBy: {
  200. id: '2',
  201. },
  202. environment: [],
  203. fields: ['environment'],
  204. widths: ['-1'],
  205. range: '14d',
  206. orderby: '-environment',
  207. display: 'previous',
  208. query: 'event.type:error',
  209. topEvents: '5',
  210. },
  211. });
  212. render(
  213. <Homepage
  214. organization={organization}
  215. location={initialData.router.location}
  216. router={initialData.router}
  217. setSavedQuery={jest.fn()}
  218. loading={false}
  219. />,
  220. {context: initialData.routerContext, organization: initialData.organization}
  221. );
  222. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  223. expect(screen.queryByText('Set as Default')).not.toBeInTheDocument();
  224. });
  225. it('Disables the Set as Default button when no saved homepage', async () => {
  226. initialData = 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. await waitFor(() => {
  255. expect(measurementsMetaMock).toHaveBeenCalled();
  256. });
  257. });
  258. it('follows absolute date selection', async () => {
  259. initialData = initializeOrg({
  260. organization,
  261. router: {
  262. location: {
  263. ...TestStubs.location(),
  264. query: {
  265. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  266. },
  267. },
  268. },
  269. });
  270. MockApiClient.addMockResponse({
  271. url: '/organizations/org-slug/discover/homepage/',
  272. method: 'GET',
  273. statusCode: 200,
  274. });
  275. render(
  276. <Homepage
  277. organization={organization}
  278. location={initialData.router.location}
  279. router={initialData.router}
  280. setSavedQuery={jest.fn()}
  281. loading={false}
  282. />,
  283. {context: initialData.routerContext, organization: initialData.organization}
  284. );
  285. await userEvent.click(await screen.findByText('24H'));
  286. await userEvent.click(await screen.findByText('Absolute date'));
  287. await userEvent.click(screen.getByText('Apply'));
  288. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  289. });
  290. it('renders changes to the discover query when no homepage', async () => {
  291. initialData = 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. // Simulate an update to the columns by changing the URL params
  321. const rerenderData = 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. await waitFor(() => {
  343. expect(measurementsMetaMock).toHaveBeenCalled();
  344. });
  345. expect(screen.getByText('event.type')).toBeInTheDocument();
  346. });
  347. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  348. initialData = initializeOrg({
  349. organization,
  350. router: {
  351. location: {
  352. ...TestStubs.location(),
  353. query: {
  354. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  355. field: ['title'],
  356. },
  357. },
  358. },
  359. });
  360. const {rerender} = render(
  361. <Homepage
  362. organization={organization}
  363. location={initialData.router.location}
  364. router={initialData.router}
  365. setSavedQuery={jest.fn()}
  366. loading={false}
  367. />,
  368. {context: initialData.routerContext, organization: initialData.organization}
  369. );
  370. renderGlobalModal();
  371. // Simulate an update to the columns by changing the URL params
  372. const rerenderData = initializeOrg({
  373. organization,
  374. router: {
  375. location: {
  376. ...TestStubs.location(),
  377. query: {
  378. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  379. field: ['event.type'],
  380. },
  381. },
  382. },
  383. });
  384. rerender(
  385. <Homepage
  386. organization={organization}
  387. location={rerenderData.router.location}
  388. router={rerenderData.router}
  389. setSavedQuery={jest.fn()}
  390. loading={false}
  391. />
  392. );
  393. await waitFor(() => {
  394. expect(measurementsMetaMock).toHaveBeenCalled();
  395. });
  396. expect(screen.getByText('event.type')).toBeInTheDocument();
  397. });
  398. it('overrides homepage filters with pinned filters if they exist', async () => {
  399. ProjectsStore.loadInitialData([TestStubs.Project({id: 2})]);
  400. jest.spyOn(pageFilterUtils, 'getPageFilterStorage').mockReturnValueOnce({
  401. pinnedFilters: new Set(['projects']),
  402. state: {
  403. project: [2],
  404. environment: [],
  405. start: null,
  406. end: null,
  407. period: '14d',
  408. utc: null,
  409. },
  410. });
  411. render(
  412. <Homepage
  413. organization={organization}
  414. location={initialData.router.location}
  415. router={initialData.router}
  416. setSavedQuery={jest.fn()}
  417. loading={false}
  418. />,
  419. {context: initialData.routerContext, organization: initialData.organization}
  420. );
  421. await waitFor(() => {
  422. expect(measurementsMetaMock).toHaveBeenCalled();
  423. });
  424. expect(screen.getByText('project-slug')).toBeInTheDocument();
  425. });
  426. it('allows users to set the All Events query as default', async () => {
  427. initialData = initializeOrg({
  428. organization,
  429. router: {
  430. location: {
  431. ...TestStubs.location(),
  432. query: {
  433. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  434. },
  435. },
  436. },
  437. });
  438. mockHomepage = MockApiClient.addMockResponse({
  439. url: '/organizations/org-slug/discover/homepage/',
  440. method: 'GET',
  441. statusCode: 200,
  442. });
  443. render(
  444. <Homepage
  445. organization={organization}
  446. location={initialData.router.location}
  447. router={initialData.router}
  448. setSavedQuery={jest.fn()}
  449. loading={false}
  450. />,
  451. {context: initialData.routerContext, organization: initialData.organization}
  452. );
  453. await waitFor(() => expect(screen.getByTestId('set-as-default')).toBeEnabled());
  454. });
  455. });