homepage.spec.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import {browserHistory} from 'react-router';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {mountGlobalModal} from 'sentry-test/modal';
  4. import {act, render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  5. import Homepage from './homepage';
  6. describe('Discover > Homepage', () => {
  7. const features = [
  8. 'global-views',
  9. 'discover-query',
  10. 'discover-query-builder-as-landing-page',
  11. ];
  12. let initialData, organization, mockHomepage;
  13. beforeEach(() => {
  14. organization = TestStubs.Organization({
  15. features,
  16. });
  17. initialData = initializeOrg({
  18. ...initializeOrg(),
  19. organization,
  20. router: {
  21. location: TestStubs.location(),
  22. },
  23. });
  24. MockApiClient.addMockResponse({
  25. url: '/organizations/org-slug/eventsv2/',
  26. body: [],
  27. });
  28. MockApiClient.addMockResponse({
  29. url: '/organizations/org-slug/events-meta/',
  30. body: {
  31. count: 2,
  32. },
  33. });
  34. MockApiClient.addMockResponse({
  35. url: '/organizations/org-slug/events-stats/',
  36. body: {data: [[123, []]]},
  37. });
  38. MockApiClient.addMockResponse({
  39. url: '/organizations/org-slug/tags/',
  40. body: [],
  41. });
  42. MockApiClient.addMockResponse({
  43. url: '/organizations/org-slug/releases/stats/',
  44. body: [],
  45. });
  46. mockHomepage = MockApiClient.addMockResponse({
  47. url: '/organizations/org-slug/discover/homepage/',
  48. method: 'GET',
  49. statusCode: 200,
  50. body: {
  51. id: '2',
  52. name: 'homepage query',
  53. projects: [],
  54. version: 2,
  55. expired: false,
  56. dateCreated: '2021-04-08T17:53:25.195782Z',
  57. dateUpdated: '2021-04-09T12:13:18.567264Z',
  58. createdBy: {
  59. id: '2',
  60. },
  61. environment: ['alpha'],
  62. fields: ['environment'],
  63. widths: ['-1'],
  64. range: '24h',
  65. orderby: '-environment',
  66. display: 'previous',
  67. query: 'event.type:error',
  68. },
  69. });
  70. });
  71. it('fetches from the homepage URL and renders fields, page filters, and chart information', async () => {
  72. render(
  73. <Homepage
  74. organization={organization}
  75. location={initialData.router.location}
  76. router={initialData.router}
  77. setSavedQuery={jest.fn()}
  78. loading={false}
  79. />,
  80. {context: initialData.routerContext, organization: initialData.organization}
  81. );
  82. expect(mockHomepage).toHaveBeenCalled();
  83. await screen.findByText('environment');
  84. // Only the environment field
  85. expect(screen.getAllByTestId('grid-head-cell').length).toEqual(1);
  86. screen.getByText('Previous Period');
  87. screen.getByText('alpha');
  88. screen.getByText('event.type:error');
  89. });
  90. it('applies URL changes with the homepage pathname', async () => {
  91. render(
  92. <Homepage
  93. organization={organization}
  94. location={initialData.router.location}
  95. router={initialData.router}
  96. setSavedQuery={jest.fn()}
  97. loading={false}
  98. />,
  99. {context: initialData.routerContext, organization: initialData.organization}
  100. );
  101. userEvent.click(screen.getByText('Columns'));
  102. await act(async () => {
  103. await mountGlobalModal();
  104. });
  105. userEvent.click(screen.getByTestId('label'));
  106. userEvent.click(screen.getByText('event.type'));
  107. userEvent.click(screen.getByText('Apply'));
  108. expect(browserHistory.push).toHaveBeenCalledWith(
  109. expect.objectContaining({
  110. pathname: '/organizations/org-slug/discover/homepage/',
  111. query: expect.objectContaining({
  112. field: ['event.type'],
  113. }),
  114. })
  115. );
  116. });
  117. it('does not show an editable header or author information', () => {
  118. render(
  119. <Homepage
  120. organization={organization}
  121. location={initialData.router.location}
  122. router={initialData.router}
  123. setSavedQuery={jest.fn()}
  124. loading={false}
  125. />,
  126. {context: initialData.routerContext, organization: initialData.organization}
  127. );
  128. userEvent.click(screen.getByTestId('editable-text-label'));
  129. // Check that clicking the label didn't render a textbox for editing
  130. expect(
  131. within(screen.getByTestId('editable-text-label')).queryByRole('textbox')
  132. ).not.toBeInTheDocument();
  133. expect(screen.queryByText(/Created by:/)).not.toBeInTheDocument();
  134. expect(screen.queryByText(/Last edited:/)).not.toBeInTheDocument();
  135. });
  136. it('shows the Reset Discover Home button on initial load', async () => {
  137. MockApiClient.addMockResponse({
  138. url: '/organizations/org-slug/discover/homepage/',
  139. method: 'GET',
  140. statusCode: 200,
  141. body: {
  142. id: '2',
  143. name: 'homepage query',
  144. projects: [],
  145. version: 2,
  146. expired: false,
  147. dateCreated: '2021-04-08T17:53:25.195782Z',
  148. dateUpdated: '2021-04-09T12:13:18.567264Z',
  149. createdBy: {
  150. id: '2',
  151. },
  152. environment: [],
  153. fields: ['environment'],
  154. widths: ['-1'],
  155. range: '14d',
  156. orderby: '-environment',
  157. display: 'previous',
  158. query: 'event.type:error',
  159. topEvents: '5',
  160. },
  161. });
  162. render(
  163. <Homepage
  164. organization={organization}
  165. location={initialData.router.location}
  166. router={initialData.router}
  167. setSavedQuery={jest.fn()}
  168. loading={false}
  169. />,
  170. {context: initialData.routerContext, organization: initialData.organization}
  171. );
  172. expect(await screen.findByText('Reset Discover Home')).toBeInTheDocument();
  173. expect(screen.queryByText('Use as Discover Home')).not.toBeInTheDocument();
  174. });
  175. it('Disables the Use as Discover Home button when no saved homepage', () => {
  176. mockHomepage = MockApiClient.addMockResponse({
  177. url: '/organizations/org-slug/discover/homepage/',
  178. method: 'GET',
  179. statusCode: 200,
  180. });
  181. render(
  182. <Homepage
  183. organization={organization}
  184. location={initialData.router.location}
  185. router={initialData.router}
  186. setSavedQuery={jest.fn()}
  187. loading={false}
  188. />,
  189. {context: initialData.routerContext, organization: initialData.organization}
  190. );
  191. expect(mockHomepage).toHaveBeenCalled();
  192. expect(screen.getByRole('button', {name: /use as discover home/i})).toBeDisabled();
  193. });
  194. it('follows absolute date selection', async () => {
  195. MockApiClient.addMockResponse({
  196. url: '/organizations/org-slug/discover/homepage/',
  197. method: 'GET',
  198. statusCode: 200,
  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. userEvent.click(await screen.findByText('14D'));
  211. userEvent.click(await screen.findByText('Absolute date'));
  212. userEvent.click(screen.getByText('Apply'));
  213. expect(screen.queryByText('14D')).not.toBeInTheDocument();
  214. });
  215. });