index.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 other view', function () {
  143. const data = initializeData({
  144. query: {landingDisplay: LandingDisplayField.FRONTEND_OTHER},
  145. });
  146. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  147. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  148. });
  149. it('renders backend view', function () {
  150. const data = initializeData({
  151. query: {landingDisplay: LandingDisplayField.BACKEND},
  152. });
  153. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  154. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  155. });
  156. it('renders mobile view', function () {
  157. const data = initializeData({
  158. query: {landingDisplay: LandingDisplayField.MOBILE},
  159. });
  160. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  161. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  162. });
  163. it('renders react-native table headers in mobile view', async function () {
  164. const project = TestStubs.Project({platform: 'react-native'});
  165. const projects = [project];
  166. const data = initializeData({
  167. query: {landingDisplay: LandingDisplayField.MOBILE},
  168. selectedProject: project.id,
  169. projects,
  170. });
  171. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  172. expect(await screen.findByTestId('performance-table')).toBeInTheDocument();
  173. expect(screen.getByTestId('grid-editable')).toBeInTheDocument();
  174. const columnHeaders = await screen.findAllByTestId('grid-head-cell');
  175. expect(columnHeaders).toHaveLength(REACT_NATIVE_COLUMN_TITLES.length);
  176. for (const [index, title] of columnHeaders.entries()) {
  177. expect(title).toHaveTextContent(REACT_NATIVE_COLUMN_TITLES[index]);
  178. }
  179. });
  180. it('renders all transactions view', async function () {
  181. const data = initializeData({
  182. query: {landingDisplay: LandingDisplayField.ALL},
  183. });
  184. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  185. expect(await screen.findByTestId('performance-table')).toBeInTheDocument();
  186. await waitFor(() => expect(eventStatsMock).toHaveBeenCalledTimes(1)); // Only one request is made since the query batcher is working.
  187. expect(eventStatsMock).toHaveBeenNthCalledWith(
  188. 1,
  189. expect.anything(),
  190. expect.objectContaining({
  191. query: expect.objectContaining({
  192. environment: [],
  193. interval: '1h',
  194. partial: '1',
  195. project: [],
  196. query: 'event.type:transaction',
  197. referrer: 'api.performance.generic-widget-chart.user-misery-area',
  198. statsPeriod: '14d',
  199. yAxis: ['user_misery()', 'tpm()', 'failure_rate()'],
  200. }),
  201. })
  202. );
  203. expect(eventsMock).toHaveBeenCalledTimes(2);
  204. const titles = await screen.findAllByTestId('performance-widget-title');
  205. expect(titles).toHaveLength(5);
  206. expect(titles.at(0)).toHaveTextContent('Most Regressed');
  207. expect(titles.at(1)).toHaveTextContent('Most Improved');
  208. expect(titles.at(2)).toHaveTextContent('User Misery');
  209. expect(titles.at(3)).toHaveTextContent('Transactions Per Minute');
  210. expect(titles.at(4)).toHaveTextContent('Failure Rate');
  211. });
  212. it('Can switch between landing displays', async function () {
  213. const data = initializeData({
  214. query: {landingDisplay: LandingDisplayField.FRONTEND_OTHER, abc: '123'},
  215. });
  216. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  217. expect(screen.getByTestId('frontend-other-view')).toBeInTheDocument();
  218. await userEvent.click(screen.getByRole('tab', {name: 'All Transactions'}));
  219. expect(browserHistory.push).toHaveBeenNthCalledWith(
  220. 1,
  221. expect.objectContaining({
  222. pathname: data.location.pathname,
  223. query: {query: '', abc: '123'},
  224. })
  225. );
  226. });
  227. it('Updating projects switches performance view', function () {
  228. const data = initializeData({
  229. query: {landingDisplay: LandingDisplayField.FRONTEND_OTHER},
  230. });
  231. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  232. expect(screen.getByTestId('frontend-other-view')).toBeInTheDocument();
  233. const updatedData = initializeData({
  234. projects: [TestStubs.Project({id: 123, platform: 'unknown'})],
  235. selectedProject: 123,
  236. });
  237. wrapper.rerender(<WrappedComponent data={updatedData} />, data.routerContext);
  238. expect(screen.getByTestId('all-transactions-view')).toBeInTheDocument();
  239. });
  240. it('View correctly defaults based on project without url param', function () {
  241. const data = initializeData({
  242. projects: [TestStubs.Project({id: 99, platform: 'javascript-react'})],
  243. selectedProject: 99,
  244. });
  245. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  246. expect(screen.getByTestId('frontend-other-view')).toBeInTheDocument();
  247. });
  248. describe('With transaction search feature', function () {
  249. it('does not search for empty string transaction', async function () {
  250. const data = initializeData();
  251. render(<WrappedComponent data={data} withStaticFilters />, data.routerContext);
  252. await waitForElementToBeRemoved(() => screen.getAllByTestId('loading-indicator'));
  253. await userEvent.type(screen.getByPlaceholderText('Search Transactions'), '{enter}');
  254. expect(searchHandlerMock).toHaveBeenCalledWith('', 'transactionsOnly');
  255. });
  256. it('renders the search bar', async function () {
  257. addMetricsDataMock();
  258. const data = initializeData({
  259. query: {
  260. field: 'test',
  261. },
  262. });
  263. wrapper = render(
  264. <WrappedComponent data={data} withStaticFilters />,
  265. data.routerContext
  266. );
  267. expect(await screen.findByTestId('transaction-search-bar')).toBeInTheDocument();
  268. });
  269. it('extracts free text from the query', async function () {
  270. const data = initializeData();
  271. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  272. await waitForElementToBeRemoved(() => screen.getAllByTestId('loading-indicator'));
  273. expect(await screen.findByPlaceholderText('Search Transactions')).toHaveValue('');
  274. });
  275. });
  276. describe('With span operations widget feature flag', function () {
  277. it('Displays the span operations widget', async function () {
  278. addMetricsDataMock();
  279. const data = initializeData({
  280. features: [
  281. 'performance-transaction-name-only-search',
  282. 'performance-new-widget-designs',
  283. ],
  284. });
  285. wrapper = render(<WrappedComponent data={data} />, data.routerContext);
  286. const titles = await screen.findAllByTestId('performance-widget-title');
  287. expect(titles.at(0)).toHaveTextContent('Most Regressed');
  288. });
  289. });
  290. });