index.spec.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import {browserHistory} from 'react-router';
  2. import {addMetricsDataMock} from 'sentry-test/performance/addMetricsDataMock';
  3. import {initializeData} from 'sentry-test/performance/initializePerformanceData';
  4. import {
  5. act,
  6. render,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. waitForElementToBeRemoved,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import TeamStore from 'sentry/stores/teamStore';
  13. import {MetricsCardinalityProvider} from 'sentry/utils/performance/contexts/metricsCardinality';
  14. import {QueryClient, QueryClientProvider} from 'sentry/utils/queryClient';
  15. import {OrganizationContext} from 'sentry/views/organizationContext';
  16. import {generatePerformanceEventView} from 'sentry/views/performance/data';
  17. import {PerformanceLanding} from 'sentry/views/performance/landing';
  18. import {REACT_NATIVE_COLUMN_TITLES} from 'sentry/views/performance/landing/data';
  19. import {LandingDisplayField} from 'sentry/views/performance/landing/utils';
  20. const searchHandlerMock = jest.fn();
  21. function WrappedComponent({data, withStaticFilters = false}) {
  22. const eventView = generatePerformanceEventView(
  23. data.router.location,
  24. data.projects,
  25. {
  26. withStaticFilters,
  27. },
  28. data.organization
  29. );
  30. const client = new QueryClient();
  31. return (
  32. <QueryClientProvider client={client}>
  33. <OrganizationContext.Provider value={data.organization}>
  34. <MetricsCardinalityProvider
  35. location={data.router.location}
  36. organization={data.organization}
  37. >
  38. <PerformanceLanding
  39. router={data.router}
  40. organization={data.organization}
  41. location={data.router.location}
  42. eventView={eventView}
  43. projects={data.projects}
  44. selection={eventView.getPageFilters()}
  45. onboardingProject={undefined}
  46. handleSearch={searchHandlerMock}
  47. handleTrendsClick={() => {}}
  48. setError={() => {}}
  49. withStaticFilters={withStaticFilters}
  50. />
  51. </MetricsCardinalityProvider>
  52. </OrganizationContext.Provider>
  53. </QueryClientProvider>
  54. );
  55. }
  56. describe('Performance > Landing > Index', function () {
  57. let eventStatsMock: jest.Mock;
  58. let eventsMock: jest.Mock;
  59. let wrapper: any;
  60. act(() => void TeamStore.loadInitialData([], false, null));
  61. beforeEach(function () {
  62. // @ts-ignore no-console
  63. // eslint-disable-next-line no-console
  64. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  65. MockApiClient.addMockResponse({
  66. url: '/organizations/org-slug/sdk-updates/',
  67. body: [],
  68. });
  69. MockApiClient.addMockResponse({
  70. url: '/prompts-activity/',
  71. body: {},
  72. });
  73. MockApiClient.addMockResponse({
  74. url: '/organizations/org-slug/projects/',
  75. body: [],
  76. });
  77. MockApiClient.addMockResponse({
  78. method: 'GET',
  79. url: `/organizations/org-slug/key-transactions-list/`,
  80. body: [],
  81. });
  82. MockApiClient.addMockResponse({
  83. method: 'GET',
  84. url: `/organizations/org-slug/legacy-key-transactions-count/`,
  85. body: [],
  86. });
  87. eventStatsMock = MockApiClient.addMockResponse({
  88. method: 'GET',
  89. url: `/organizations/org-slug/events-stats/`,
  90. body: [],
  91. });
  92. MockApiClient.addMockResponse({
  93. method: 'GET',
  94. url: `/organizations/org-slug/events-trends-stats/`,
  95. body: [],
  96. });
  97. MockApiClient.addMockResponse({
  98. method: 'GET',
  99. url: `/organizations/org-slug/events-histogram/`,
  100. body: [],
  101. });
  102. eventsMock = MockApiClient.addMockResponse({
  103. method: 'GET',
  104. url: `/organizations/org-slug/events/`,
  105. body: {
  106. meta: {
  107. fields: {
  108. id: 'string',
  109. },
  110. },
  111. data: [
  112. {
  113. id: '1234',
  114. },
  115. ],
  116. },
  117. });
  118. MockApiClient.addMockResponse({
  119. method: 'GET',
  120. url: `/organizations/org-slug/metrics-compatibility/`,
  121. body: [],
  122. });
  123. MockApiClient.addMockResponse({
  124. method: 'GET',
  125. url: `/organizations/org-slug/metrics-compatibility-sums/`,
  126. body: [],
  127. });
  128. });
  129. afterEach(function () {
  130. MockApiClient.clearMockResponses();
  131. jest.clearAllMocks();
  132. jest.restoreAllMocks();
  133. if (wrapper) {
  134. wrapper.unmount();
  135. wrapper = undefined;
  136. }
  137. });
  138. it('renders basic UI elements', function () {
  139. const data = initializeData();
  140. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  141. expect(screen.getByTestId('performance-landing-v3')).toBeInTheDocument();
  142. });
  143. it('renders frontend pageload view', function () {
  144. const data = initializeData({
  145. query: {landingDisplay: LandingDisplayField.FRONTEND_PAGELOAD},
  146. });
  147. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  148. expect(screen.getByTestId('frontend-pageload-view')).toBeInTheDocument();
  149. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  150. const titles = screen.getAllByTestId('performance-widget-title');
  151. expect(titles).toHaveLength(5);
  152. expect(titles[0]).toHaveTextContent('Worst LCP Web Vitals');
  153. expect(titles[1]).toHaveTextContent('Worst FCP Web Vitals');
  154. expect(titles[2]).toHaveTextContent('p75 LCP');
  155. expect(titles[3]).toHaveTextContent('LCP Distribution');
  156. expect(titles[4]).toHaveTextContent('FCP Distribution');
  157. });
  158. it('renders frontend other view', function () {
  159. const data = initializeData({
  160. query: {landingDisplay: LandingDisplayField.FRONTEND_OTHER},
  161. });
  162. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  163. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  164. });
  165. it('renders backend view', function () {
  166. const data = initializeData({
  167. query: {landingDisplay: LandingDisplayField.BACKEND},
  168. });
  169. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  170. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  171. });
  172. it('renders mobile view', function () {
  173. const data = initializeData({
  174. query: {landingDisplay: LandingDisplayField.MOBILE},
  175. });
  176. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  177. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  178. });
  179. it('renders react-native table headers in mobile view', async function () {
  180. const project = TestStubs.Project({platform: 'react-native'});
  181. const projects = [project];
  182. const data = initializeData({
  183. query: {landingDisplay: LandingDisplayField.MOBILE},
  184. selectedProject: project.id,
  185. projects,
  186. });
  187. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  188. expect(await screen.findByTestId('performance-table')).toBeInTheDocument();
  189. expect(screen.getByTestId('grid-editable')).toBeInTheDocument();
  190. const columnHeaders = await screen.findAllByTestId('grid-head-cell');
  191. expect(columnHeaders).toHaveLength(REACT_NATIVE_COLUMN_TITLES.length);
  192. for (const [index, title] of columnHeaders.entries()) {
  193. expect(title).toHaveTextContent(REACT_NATIVE_COLUMN_TITLES[index]);
  194. }
  195. });
  196. it('renders all transactions view', async function () {
  197. const data = initializeData({
  198. query: {landingDisplay: LandingDisplayField.ALL},
  199. });
  200. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  201. expect(await screen.findByTestId('performance-table')).toBeInTheDocument();
  202. await waitFor(() => expect(eventStatsMock).toHaveBeenCalledTimes(1)); // Only one request is made since the query batcher is working.
  203. expect(eventStatsMock).toHaveBeenNthCalledWith(
  204. 1,
  205. expect.anything(),
  206. expect.objectContaining({
  207. query: expect.objectContaining({
  208. environment: [],
  209. interval: '1h',
  210. partial: '1',
  211. project: [],
  212. query: 'event.type:transaction',
  213. referrer: 'api.performance.generic-widget-chart.user-misery-area',
  214. statsPeriod: '14d',
  215. yAxis: ['user_misery()', 'tpm()', 'failure_rate()'],
  216. }),
  217. })
  218. );
  219. expect(eventsMock).toHaveBeenCalledTimes(3);
  220. const titles = await screen.findAllByTestId('performance-widget-title');
  221. expect(titles).toHaveLength(5);
  222. expect(titles.at(0)).toHaveTextContent('Most Regressed');
  223. expect(titles.at(1)).toHaveTextContent('Most Related Issues');
  224. expect(titles.at(2)).toHaveTextContent('User Misery');
  225. expect(titles.at(3)).toHaveTextContent('Transactions Per Minute');
  226. expect(titles.at(4)).toHaveTextContent('Failure Rate');
  227. });
  228. it('Can switch between landing displays', async function () {
  229. const data = initializeData({
  230. query: {landingDisplay: LandingDisplayField.FRONTEND_PAGELOAD, abc: '123'},
  231. });
  232. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  233. expect(screen.getByTestId('frontend-pageload-view')).toBeInTheDocument();
  234. await userEvent.click(screen.getByRole('tab', {name: 'All Transactions'}));
  235. expect(browserHistory.push).toHaveBeenNthCalledWith(
  236. 1,
  237. expect.objectContaining({
  238. pathname: data.location.pathname,
  239. query: {query: '', abc: '123'},
  240. })
  241. );
  242. });
  243. it('Updating projects switches performance view', function () {
  244. const data = initializeData({
  245. query: {landingDisplay: LandingDisplayField.FRONTEND_PAGELOAD},
  246. });
  247. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  248. expect(screen.getByTestId('frontend-pageload-view')).toBeInTheDocument();
  249. const updatedData = initializeData({
  250. projects: [TestStubs.Project({id: 123, platform: 'unknown'})],
  251. selectedProject: 123,
  252. });
  253. wrapper.rerender(<WrappedComponent data={updatedData} />, data.routerContext);
  254. expect(screen.getByTestId('all-transactions-view')).toBeInTheDocument();
  255. });
  256. it('View correctly defaults based on project without url param', function () {
  257. const data = initializeData({
  258. projects: [TestStubs.Project({id: 99, platform: 'javascript-react'})],
  259. selectedProject: 99,
  260. });
  261. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  262. expect(screen.getByTestId('frontend-pageload-view')).toBeInTheDocument();
  263. });
  264. describe('With transaction search feature', function () {
  265. it('does not search for empty string transaction', async function () {
  266. const data = initializeData();
  267. render(<WrappedComponent data={data} withStaticFilters />, data.routerContext);
  268. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  269. await userEvent.type(screen.getByPlaceholderText('Search Transactions'), '{enter}');
  270. expect(searchHandlerMock).toHaveBeenCalledWith('', 'transactionsOnly');
  271. });
  272. it('renders the search bar', async function () {
  273. addMetricsDataMock();
  274. const data = initializeData({
  275. query: {
  276. field: 'test',
  277. },
  278. });
  279. wrapper = render(
  280. <WrappedComponent data={data} withStaticFilters />,
  281. data.routerContext
  282. );
  283. expect(await screen.findByTestId('transaction-search-bar')).toBeInTheDocument();
  284. });
  285. it('extracts free text from the query', async function () {
  286. const data = initializeData();
  287. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  288. await waitForElementToBeRemoved(() => screen.queryByTestId('loading-indicator'));
  289. expect(await screen.findByPlaceholderText('Search Transactions')).toHaveValue('');
  290. });
  291. });
  292. describe('With span operations widget feature flag', function () {
  293. it('Displays the span operations widget', async function () {
  294. addMetricsDataMock();
  295. const data = initializeData({
  296. features: [
  297. 'performance-transaction-name-only-search',
  298. 'performance-new-widget-designs',
  299. ],
  300. });
  301. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  302. const titles = await screen.findAllByTestId('performance-widget-title');
  303. expect(titles.at(0)).toHaveTextContent('Span Operations');
  304. });
  305. });
  306. });