homepage.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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('fetches from the homepage URL and renders fields, page filters, and chart information', 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. expect(mockHomepage).toHaveBeenCalled();
  92. await screen.findByText('environment');
  93. // Only the environment field
  94. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  95. screen.getByText('Previous Period');
  96. screen.getByText('alpha');
  97. screen.getByText('event.type:error');
  98. });
  99. it('renders event view from URL params over homepage query', async () => {
  100. initialData = initializeOrg({
  101. ...initializeOrg(),
  102. organization,
  103. router: {
  104. location: {
  105. ...TestStubs.location(),
  106. query: {
  107. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  108. field: ['project'],
  109. },
  110. },
  111. },
  112. });
  113. render(
  114. <Homepage
  115. organization={organization}
  116. location={initialData.router.location}
  117. router={initialData.router}
  118. setSavedQuery={jest.fn()}
  119. loading={false}
  120. />,
  121. {context: initialData.routerContext, organization: initialData.organization}
  122. );
  123. expect(mockHomepage).toHaveBeenCalled();
  124. await screen.findByText('project');
  125. // This is the field in the mocked response for the homepage
  126. expect(screen.queryByText('environment')).not.toBeInTheDocument();
  127. });
  128. it('applies URL changes with the homepage pathname', async () => {
  129. render(
  130. <Homepage
  131. organization={organization}
  132. location={initialData.router.location}
  133. router={initialData.router}
  134. setSavedQuery={jest.fn()}
  135. loading={false}
  136. />,
  137. {context: initialData.routerContext, organization: initialData.organization}
  138. );
  139. userEvent.click(screen.getByText('Columns'));
  140. await act(async () => {
  141. await mountGlobalModal();
  142. });
  143. userEvent.click(screen.getByTestId('label'));
  144. userEvent.click(screen.getByText('event.type'));
  145. userEvent.click(screen.getByText('Apply'));
  146. expect(browserHistory.push).toHaveBeenCalledWith(
  147. expect.objectContaining({
  148. pathname: '/organizations/org-slug/discover/homepage/',
  149. query: expect.objectContaining({
  150. field: ['event.type'],
  151. }),
  152. })
  153. );
  154. });
  155. it('does not show an editable header or author information', () => {
  156. render(
  157. <Homepage
  158. organization={organization}
  159. location={initialData.router.location}
  160. router={initialData.router}
  161. setSavedQuery={jest.fn()}
  162. loading={false}
  163. />,
  164. {context: initialData.routerContext, organization: initialData.organization}
  165. );
  166. userEvent.click(screen.getByTestId('editable-text-label'));
  167. // Check that clicking the label didn't render a textbox for editing
  168. expect(
  169. within(screen.getByTestId('editable-text-label')).queryByRole('textbox')
  170. ).not.toBeInTheDocument();
  171. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  172. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  173. });
  174. it('shows the Remove Default button on initial load', async () => {
  175. MockApiClient.addMockResponse({
  176. url: '/organizations/org-slug/discover/homepage/',
  177. method: 'GET',
  178. statusCode: 200,
  179. body: {
  180. id: '2',
  181. name: 'homepage query',
  182. projects: [],
  183. version: 2,
  184. expired: false,
  185. dateCreated: '2021-04-08T17:53:25.195782Z',
  186. dateUpdated: '2021-04-09T12:13:18.567264Z',
  187. createdBy: {
  188. id: '2',
  189. },
  190. environment: [],
  191. fields: ['environment'],
  192. widths: ['-1'],
  193. range: '14d',
  194. orderby: '-environment',
  195. display: 'previous',
  196. query: 'event.type:error',
  197. topEvents: '5',
  198. },
  199. });
  200. render(
  201. <Homepage
  202. organization={organization}
  203. location={initialData.router.location}
  204. router={initialData.router}
  205. setSavedQuery={jest.fn()}
  206. loading={false}
  207. />,
  208. {context: initialData.routerContext, organization: initialData.organization}
  209. );
  210. expect(await screen.findByText('Remove Default')).toBeInTheDocument();
  211. expect(screen.queryByText('Set As Default')).not.toBeInTheDocument();
  212. });
  213. it('Disables the Set As Default button when no saved homepage', () => {
  214. initialData = initializeOrg({
  215. ...initializeOrg(),
  216. organization,
  217. router: {
  218. location: {
  219. ...TestStubs.location(),
  220. query: {
  221. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  222. },
  223. },
  224. },
  225. });
  226. mockHomepage = MockApiClient.addMockResponse({
  227. url: '/organizations/org-slug/discover/homepage/',
  228. method: 'GET',
  229. statusCode: 200,
  230. });
  231. render(
  232. <Homepage
  233. organization={organization}
  234. location={initialData.router.location}
  235. router={initialData.router}
  236. setSavedQuery={jest.fn()}
  237. loading={false}
  238. />,
  239. {context: initialData.routerContext, organization: initialData.organization}
  240. );
  241. expect(mockHomepage).toHaveBeenCalled();
  242. expect(screen.getByRole('button', {name: /set as default/i})).toBeDisabled();
  243. });
  244. it('follows absolute date selection', async () => {
  245. initialData = initializeOrg({
  246. ...initializeOrg(),
  247. organization,
  248. router: {
  249. location: {
  250. ...TestStubs.location(),
  251. query: {
  252. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  253. },
  254. },
  255. },
  256. });
  257. MockApiClient.addMockResponse({
  258. url: '/organizations/org-slug/discover/homepage/',
  259. method: 'GET',
  260. statusCode: 200,
  261. });
  262. render(
  263. <Homepage
  264. organization={organization}
  265. location={initialData.router.location}
  266. router={initialData.router}
  267. setSavedQuery={jest.fn()}
  268. loading={false}
  269. />,
  270. {context: initialData.routerContext, organization: initialData.organization}
  271. );
  272. userEvent.click(await screen.findByText('24H'));
  273. userEvent.click(await screen.findByText('Absolute date'));
  274. userEvent.click(screen.getByText('Apply'));
  275. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  276. });
  277. it('renders changes to the discover query when no homepage', async () => {
  278. initialData = initializeOrg({
  279. ...initializeOrg(),
  280. organization,
  281. router: {
  282. location: {
  283. ...TestStubs.location(),
  284. query: {
  285. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  286. field: ['title'],
  287. },
  288. },
  289. },
  290. });
  291. MockApiClient.addMockResponse({
  292. url: '/organizations/org-slug/discover/homepage/',
  293. method: 'GET',
  294. statusCode: 200,
  295. body: '',
  296. });
  297. const {rerender} = render(
  298. <Homepage
  299. organization={organization}
  300. location={initialData.router.location}
  301. router={initialData.router}
  302. setSavedQuery={jest.fn()}
  303. loading={false}
  304. />,
  305. {context: initialData.routerContext, organization: initialData.organization}
  306. );
  307. userEvent.click(screen.getByText('Columns'));
  308. await act(async () => {
  309. await mountGlobalModal();
  310. });
  311. userEvent.click(screen.getByTestId('label'));
  312. userEvent.click(screen.getByText('event.type'));
  313. userEvent.click(screen.getByText('Apply'));
  314. const rerenderData = initializeOrg({
  315. ...initializeOrg(),
  316. organization,
  317. router: {
  318. location: {
  319. ...TestStubs.location(),
  320. query: {
  321. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  322. field: ['event.type'],
  323. },
  324. },
  325. },
  326. });
  327. rerender(
  328. <Homepage
  329. organization={organization}
  330. location={rerenderData.router.location}
  331. router={rerenderData.router}
  332. setSavedQuery={jest.fn()}
  333. loading={false}
  334. />
  335. );
  336. await waitFor(() =>
  337. expect(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  338. );
  339. expect(screen.getByText('event.type')).toBeInTheDocument();
  340. });
  341. it('renders changes to the discover query when loaded with valid event view in url params', async () => {
  342. initialData = initializeOrg({
  343. ...initializeOrg(),
  344. organization,
  345. router: {
  346. location: {
  347. ...TestStubs.location(),
  348. query: {
  349. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  350. field: ['title'],
  351. },
  352. },
  353. },
  354. });
  355. const {rerender} = render(
  356. <Homepage
  357. organization={organization}
  358. location={initialData.router.location}
  359. router={initialData.router}
  360. setSavedQuery={jest.fn()}
  361. loading={false}
  362. />,
  363. {context: initialData.routerContext, organization: initialData.organization}
  364. );
  365. userEvent.click(screen.getByText('Columns'));
  366. await act(async () => {
  367. await mountGlobalModal();
  368. });
  369. userEvent.click(screen.getByTestId('label'));
  370. userEvent.click(screen.getByText('event.type'));
  371. userEvent.click(screen.getByText('Apply'));
  372. const rerenderData = initializeOrg({
  373. ...initializeOrg(),
  374. organization,
  375. router: {
  376. location: {
  377. ...TestStubs.location(),
  378. query: {
  379. ...EventView.fromSavedQuery(DEFAULT_EVENT_VIEW).generateQueryStringObject(),
  380. field: ['event.type'],
  381. },
  382. },
  383. },
  384. });
  385. rerender(
  386. <Homepage
  387. organization={organization}
  388. location={rerenderData.router.location}
  389. router={rerenderData.router}
  390. setSavedQuery={jest.fn()}
  391. loading={false}
  392. />
  393. );
  394. await waitFor(() =>
  395. expect(screen.queryByText('Edit Columns')).not.toBeInTheDocument()
  396. );
  397. expect(screen.getByText('event.type')).toBeInTheDocument();
  398. });
  399. });