homepage.spec.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {mountGlobalModal} from 'sentry-test/modal';
  4. import {
  5. act,
  6. render,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. within,
  11. } from 'sentry-test/reactTestingLibrary';
  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 = [
  17. 'global-views',
  18. 'discover-query',
  19. 'discover-query-builder-as-landing-page',
  20. ];
  21. let initialData, organization, mockHomepage;
  22. beforeEach(() => {
  23. organization = TestStubs.Organization({
  24. features,
  25. });
  26. initialData = initializeOrg({
  27. ...initializeOrg(),
  28. organization,
  29. router: {
  30. location: TestStubs.location(),
  31. },
  32. });
  33. MockApiClient.addMockResponse({
  34. url: '/organizations/org-slug/eventsv2/',
  35. body: [],
  36. });
  37. MockApiClient.addMockResponse({
  38. url: '/organizations/org-slug/events-meta/',
  39. body: {
  40. count: 2,
  41. },
  42. });
  43. MockApiClient.addMockResponse({
  44. url: '/organizations/org-slug/events-stats/',
  45. body: {data: [[123, []]]},
  46. });
  47. MockApiClient.addMockResponse({
  48. url: '/organizations/org-slug/tags/',
  49. body: [],
  50. });
  51. MockApiClient.addMockResponse({
  52. url: '/organizations/org-slug/releases/stats/',
  53. body: [],
  54. });
  55. mockHomepage = MockApiClient.addMockResponse({
  56. url: '/organizations/org-slug/discover/homepage/',
  57. method: 'GET',
  58. statusCode: 200,
  59. body: {
  60. id: '2',
  61. name: 'homepage query',
  62. projects: [],
  63. version: 2,
  64. expired: false,
  65. dateCreated: '2021-04-08T17:53:25.195782Z',
  66. dateUpdated: '2021-04-09T12:13:18.567264Z',
  67. createdBy: {
  68. id: '2',
  69. },
  70. environment: ['alpha'],
  71. fields: ['environment'],
  72. widths: ['-1'],
  73. range: '24h',
  74. orderby: '-environment',
  75. display: 'previous',
  76. query: 'event.type:error',
  77. },
  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, page filters, 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('alpha');
  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. userEvent.click(screen.getByText('Columns'));
  155. await act(async () => {
  156. await mountGlobalModal();
  157. });
  158. userEvent.click(screen.getByTestId('label'));
  159. userEvent.click(screen.getByText('event.type'));
  160. userEvent.click(screen.getByText('Apply'));
  161. expect(browserHistory.push).toHaveBeenCalledWith(
  162. expect.objectContaining({
  163. pathname: '/organizations/org-slug/discover/homepage/',
  164. query: expect.objectContaining({
  165. field: ['event.type'],
  166. }),
  167. })
  168. );
  169. });
  170. it('does not show an editable header or author information', () => {
  171. render(
  172. <Homepage
  173. organization={organization}
  174. location={initialData.router.location}
  175. router={initialData.router}
  176. setSavedQuery={jest.fn()}
  177. loading={false}
  178. />,
  179. {context: initialData.routerContext, organization: initialData.organization}
  180. );
  181. userEvent.click(screen.getByTestId('editable-text-label'));
  182. // Check that clicking the label didn't render a textbox for editing
  183. expect(
  184. within(screen.getByTestId('editable-text-label')).queryByRole('textbox')
  185. ).not.toBeInTheDocument();
  186. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  187. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  188. });
  189. it('shows the Remove Default button on initial load', async () => {
  190. MockApiClient.addMockResponse({
  191. url: '/organizations/org-slug/discover/homepage/',
  192. method: 'GET',
  193. statusCode: 200,
  194. body: {
  195. id: '2',
  196. name: 'homepage query',
  197. projects: [],
  198. version: 2,
  199. expired: false,
  200. dateCreated: '2021-04-08T17:53:25.195782Z',
  201. dateUpdated: '2021-04-09T12:13:18.567264Z',
  202. createdBy: {
  203. id: '2',
  204. },
  205. environment: [],
  206. fields: ['environment'],
  207. widths: ['-1'],
  208. range: '14d',
  209. orderby: '-environment',
  210. display: 'previous',
  211. query: 'event.type:error',
  212. topEvents: '5',
  213. },
  214. });
  215. render(
  216. <Homepage
  217. organization={organization}
  218. location={initialData.router.location}
  219. router={initialData.router}
  220. setSavedQuery={jest.fn()}
  221. loading={false}
  222. />,
  223. {context: initialData.routerContext, organization: initialData.organization}
  224. );
  225. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  226. expect(screen.queryByText('Set As Default')).not.toBeInTheDocument();
  227. });
  228. it('Disables the Set As Default button when no saved homepage', () => {
  229. initialData = initializeOrg({
  230. ...initializeOrg(),
  231. organization,
  232. router: {
  233. location: {
  234. ...TestStubs.location(),
  235. query: {
  236. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  237. },
  238. },
  239. },
  240. });
  241. mockHomepage = MockApiClient.addMockResponse({
  242. url: '/organizations/org-slug/discover/homepage/',
  243. method: 'GET',
  244. statusCode: 200,
  245. });
  246. render(
  247. <Homepage
  248. organization={organization}
  249. location={initialData.router.location}
  250. router={initialData.router}
  251. setSavedQuery={jest.fn()}
  252. loading={false}
  253. />,
  254. {context: initialData.routerContext, organization: initialData.organization}
  255. );
  256. expect(mockHomepage).toHaveBeenCalled();
  257. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  258. });
  259. it('follows absolute date selection', async () => {
  260. initialData = initializeOrg({
  261. ...initializeOrg(),
  262. organization,
  263. router: {
  264. location: {
  265. ...TestStubs.location(),
  266. query: {
  267. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  268. },
  269. },
  270. },
  271. });
  272. MockApiClient.addMockResponse({
  273. url: '/organizations/org-slug/discover/homepage/',
  274. method: 'GET',
  275. statusCode: 200,
  276. });
  277. render(
  278. <Homepage
  279. organization={organization}
  280. location={initialData.router.location}
  281. router={initialData.router}
  282. setSavedQuery={jest.fn()}
  283. loading={false}
  284. />,
  285. {context: initialData.routerContext, organization: initialData.organization}
  286. );
  287. userEvent.click(await screen.findByText('24H'));
  288. userEvent.click(await screen.findByText('Absolute date'));
  289. userEvent.click(screen.getByText('Apply'));
  290. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  291. });
  292. it('renders changes to the discover query when no homepage', async () => {
  293. initialData = initializeOrg({
  294. ...initializeOrg(),
  295. organization,
  296. router: {
  297. location: {
  298. ...TestStubs.location(),
  299. query: {
  300. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  301. field: ['title'],
  302. },
  303. },
  304. },
  305. });
  306. MockApiClient.addMockResponse({
  307. url: '/organizations/org-slug/discover/homepage/',
  308. method: 'GET',
  309. statusCode: 200,
  310. body: '',
  311. });
  312. const {rerender} = render(
  313. <Homepage
  314. organization={organization}
  315. location={initialData.router.location}
  316. router={initialData.router}
  317. setSavedQuery={jest.fn()}
  318. loading={false}
  319. />,
  320. {context: initialData.routerContext, organization: initialData.organization}
  321. );
  322. userEvent.click(screen.getByText('Columns'));
  323. await act(async () => {
  324. await mountGlobalModal();
  325. });
  326. userEvent.click(screen.getByTestId('label'));
  327. userEvent.click(screen.getByText('event.type'));
  328. userEvent.click(screen.getByText('Apply'));
  329. const rerenderData = initializeOrg({
  330. ...initializeOrg(),
  331. organization,
  332. router: {
  333. location: {
  334. ...TestStubs.location(),
  335. query: {
  336. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  337. field: ['event.type'],
  338. },
  339. },
  340. },
  341. });
  342. rerender(
  343. <Homepage
  344. organization={organization}
  345. location={rerenderData.router.location}
  346. router={rerenderData.router}
  347. setSavedQuery={jest.fn()}
  348. loading={false}
  349. />
  350. );
  351. await waitFor(() =>
  352. expect(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  353. );
  354. expect(screen.getByText('event.type')).toBeInTheDocument();
  355. });
  356. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  357. initialData = initializeOrg({
  358. ...initializeOrg(),
  359. organization,
  360. router: {
  361. location: {
  362. ...TestStubs.location(),
  363. query: {
  364. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  365. field: ['title'],
  366. },
  367. },
  368. },
  369. });
  370. const {rerender} = render(
  371. <Homepage
  372. organization={organization}
  373. location={initialData.router.location}
  374. router={initialData.router}
  375. setSavedQuery={jest.fn()}
  376. loading={false}
  377. />,
  378. {context: initialData.routerContext, organization: initialData.organization}
  379. );
  380. userEvent.click(screen.getByText('Columns'));
  381. await act(async () => {
  382. await mountGlobalModal();
  383. });
  384. userEvent.click(screen.getByTestId('label'));
  385. userEvent.click(screen.getByText('event.type'));
  386. userEvent.click(screen.getByText('Apply'));
  387. const rerenderData = initializeOrg({
  388. ...initializeOrg(),
  389. organization,
  390. router: {
  391. location: {
  392. ...TestStubs.location(),
  393. query: {
  394. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  395. field: ['event.type'],
  396. },
  397. },
  398. },
  399. });
  400. rerender(
  401. <Homepage
  402. organization={organization}
  403. location={rerenderData.router.location}
  404. router={rerenderData.router}
  405. setSavedQuery={jest.fn()}
  406. loading={false}
  407. />
  408. );
  409. await waitFor(() =>
  410. expect(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  411. );
  412. expect(screen.getByText('event.type')).toBeInTheDocument();
  413. });
  414. });