index.spec.tsx 12 KB

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