content.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. import {browserHistory} from 'react-router';
  2. import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  5. import * as pageFilters from 'sentry/actionCreators/pageFilters';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  10. import {OrganizationContext} from 'sentry/views/organizationContext';
  11. import PerformanceContent from 'sentry/views/performance/content';
  12. import {DEFAULT_MAX_DURATION} from 'sentry/views/performance/trends/utils';
  13. import {RouteContext} from 'sentry/views/routeContext';
  14. const FEATURES = ['performance-view'];
  15. function WrappedComponent({organization, router}) {
  16. const client = new QueryClient();
  17. return (
  18. <QueryClientProvider client={client}>
  19. <RouteContext.Provider
  20. value={{
  21. location: router.location,
  22. params: {},
  23. router,
  24. routes: [],
  25. }}
  26. >
  27. <OrganizationContext.Provider value={organization}>
  28. <MEPSettingProvider>
  29. <PerformanceContent router={router} location={router.location} />
  30. </MEPSettingProvider>
  31. </OrganizationContext.Provider>
  32. </RouteContext.Provider>
  33. </QueryClientProvider>
  34. );
  35. }
  36. function initializeData(projects, query, features = FEATURES) {
  37. const organization = TestStubs.Organization({
  38. features,
  39. projects,
  40. });
  41. const initialData = initializeOrg({
  42. ...initializeOrg(),
  43. organization,
  44. router: {
  45. location: {
  46. pathname: '/test',
  47. query: query || {},
  48. },
  49. },
  50. });
  51. act(() => void OrganizationStore.onUpdate(initialData.organization, {replace: true}));
  52. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  53. return initialData;
  54. }
  55. function initializeTrendsData(query, addDefaultQuery = true) {
  56. const projects = [
  57. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  58. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  59. ];
  60. const organization = TestStubs.Organization({
  61. FEATURES,
  62. projects,
  63. });
  64. const otherTrendsQuery = addDefaultQuery
  65. ? {
  66. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  67. }
  68. : {};
  69. const initialData = initializeOrg({
  70. ...initializeOrg(),
  71. organization,
  72. router: {
  73. location: {
  74. pathname: '/test',
  75. query: {
  76. ...otherTrendsQuery,
  77. ...query,
  78. },
  79. },
  80. },
  81. });
  82. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  83. return initialData;
  84. }
  85. describe('Performance > Content', function () {
  86. beforeEach(function () {
  87. act(() => void TeamStore.loadInitialData([], false, null));
  88. browserHistory.push = jest.fn();
  89. jest.spyOn(pageFilters, 'updateDateTime');
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/projects/',
  92. body: [],
  93. });
  94. MockApiClient.addMockResponse({
  95. url: '/organizations/org-slug/tags/',
  96. body: [],
  97. });
  98. MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/events-stats/',
  100. body: {data: [[123, []]]},
  101. });
  102. MockApiClient.addMockResponse({
  103. url: '/organizations/org-slug/events-histogram/',
  104. body: {'transaction.duration': [{bin: 0, count: 1000}]},
  105. });
  106. MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/users/',
  108. body: [],
  109. });
  110. MockApiClient.addMockResponse({
  111. url: '/organizations/org-slug/recent-searches/',
  112. body: [],
  113. });
  114. MockApiClient.addMockResponse({
  115. url: '/organizations/org-slug/recent-searches/',
  116. method: 'POST',
  117. body: [],
  118. });
  119. MockApiClient.addMockResponse({
  120. url: '/organizations/org-slug/sdk-updates/',
  121. body: [],
  122. });
  123. MockApiClient.addMockResponse({
  124. url: '/prompts-activity/',
  125. body: {},
  126. });
  127. MockApiClient.addMockResponse({
  128. url: '/organizations/org-slug/eventsv2/',
  129. body: {
  130. meta: {
  131. user: 'string',
  132. transaction: 'string',
  133. 'project.id': 'integer',
  134. tpm: 'number',
  135. p50: 'number',
  136. p95: 'number',
  137. failure_rate: 'number',
  138. apdex_300: 'number',
  139. count_unique_user: 'number',
  140. count_miserable_user_300: 'number',
  141. user_misery_300: 'number',
  142. },
  143. data: [
  144. {
  145. transaction: '/apple/cart',
  146. 'project.id': 1,
  147. user: 'uhoh@example.com',
  148. tpm: 30,
  149. p50: 100,
  150. p95: 500,
  151. failure_rate: 0.1,
  152. apdex_300: 0.6,
  153. count_unique_user: 1000,
  154. count_miserable_user_300: 122,
  155. user_misery_300: 0.114,
  156. },
  157. ],
  158. },
  159. match: [
  160. (_, options) => {
  161. if (!options.hasOwnProperty('query')) {
  162. return false;
  163. }
  164. if (!options.query?.hasOwnProperty('field')) {
  165. return false;
  166. }
  167. return !options.query?.field.includes('team_key_transaction');
  168. },
  169. ],
  170. });
  171. MockApiClient.addMockResponse({
  172. url: '/organizations/org-slug/eventsv2/',
  173. body: {
  174. meta: {
  175. user: 'string',
  176. transaction: 'string',
  177. 'project.id': 'integer',
  178. tpm: 'number',
  179. p50: 'number',
  180. p95: 'number',
  181. failure_rate: 'number',
  182. apdex_300: 'number',
  183. count_unique_user: 'number',
  184. count_miserable_user_300: 'number',
  185. user_misery_300: 'number',
  186. },
  187. data: [
  188. {
  189. team_key_transaction: 1,
  190. transaction: '/apple/cart',
  191. 'project.id': 1,
  192. user: 'uhoh@example.com',
  193. tpm: 30,
  194. p50: 100,
  195. p95: 500,
  196. failure_rate: 0.1,
  197. apdex_300: 0.6,
  198. count_unique_user: 1000,
  199. count_miserable_user_300: 122,
  200. user_misery_300: 0.114,
  201. },
  202. {
  203. team_key_transaction: 0,
  204. transaction: '/apple/checkout',
  205. 'project.id': 1,
  206. user: 'uhoh@example.com',
  207. tpm: 30,
  208. p50: 100,
  209. p95: 500,
  210. failure_rate: 0.1,
  211. apdex_300: 0.6,
  212. count_unique_user: 1000,
  213. count_miserable_user_300: 122,
  214. user_misery_300: 0.114,
  215. },
  216. ],
  217. },
  218. match: [
  219. (_, options) => {
  220. if (!options.hasOwnProperty('query')) {
  221. return false;
  222. }
  223. if (!options.query?.hasOwnProperty('field')) {
  224. return false;
  225. }
  226. return options.query?.field.includes('team_key_transaction');
  227. },
  228. ],
  229. });
  230. MockApiClient.addMockResponse({
  231. url: '/organizations/org-slug/events-meta/',
  232. body: {
  233. count: 2,
  234. },
  235. });
  236. MockApiClient.addMockResponse({
  237. url: '/organizations/org-slug/events-trends/',
  238. body: {
  239. stats: {},
  240. events: {meta: {}, data: []},
  241. },
  242. });
  243. MockApiClient.addMockResponse({
  244. url: '/organizations/org-slug/events-trends-stats/',
  245. body: {
  246. stats: {},
  247. events: {meta: {}, data: []},
  248. },
  249. });
  250. MockApiClient.addMockResponse({
  251. url: '/organizations/org-slug/events-vitals/',
  252. body: {
  253. 'measurements.lcp': {
  254. poor: 1,
  255. meh: 2,
  256. good: 3,
  257. total: 6,
  258. p75: 4500,
  259. },
  260. },
  261. });
  262. MockApiClient.addMockResponse({
  263. method: 'GET',
  264. url: `/organizations/org-slug/key-transactions-list/`,
  265. body: [],
  266. });
  267. MockApiClient.addMockResponse({
  268. method: 'GET',
  269. url: `/organizations/org-slug/events/`,
  270. body: {
  271. data: [{}],
  272. meta: {},
  273. },
  274. });
  275. });
  276. afterEach(function () {
  277. MockApiClient.clearMockResponses();
  278. act(() => ProjectsStore.reset());
  279. // TODO: This was likely a defensive check added due to a previous isolation issue, it can possibly be removed.
  280. // @ts-ignore
  281. pageFilters.updateDateTime.mockRestore();
  282. });
  283. it('renders basic UI elements', async function () {
  284. const projects = [TestStubs.Project({firstTransactionEvent: true})];
  285. const data = initializeData(projects, {});
  286. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  287. context: data.routerContext,
  288. });
  289. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  290. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  291. expect(screen.queryByText('Pinpoint problems')).not.toBeInTheDocument();
  292. });
  293. it('renders onboarding state when the selected project has no events', async function () {
  294. const projects = [
  295. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  296. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  297. ];
  298. const data = initializeData(projects, {project: [1]});
  299. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  300. context: data.routerContext,
  301. });
  302. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  303. expect(screen.queryByText('Pinpoint problems')).toBeInTheDocument();
  304. expect(screen.queryByTestId('performance-table')).not.toBeInTheDocument();
  305. });
  306. it('does not render onboarding for "my projects"', async function () {
  307. const projects = [
  308. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  309. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  310. ];
  311. const data = initializeData(projects, {project: ['-1']});
  312. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  313. context: data.routerContext,
  314. });
  315. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  316. expect(screen.queryByText('Pinpoint problems')).not.toBeInTheDocument();
  317. });
  318. it('forwards conditions to transaction summary', async function () {
  319. const projects = [TestStubs.Project({id: '1', firstTransactionEvent: true})];
  320. const data = initializeData(projects, {project: ['1'], query: 'sentry:yes'});
  321. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  322. context: data.routerContext,
  323. });
  324. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  325. const link = screen.getByRole('link', {name: '/apple/cart'});
  326. userEvent.click(link);
  327. expect(data.router.push).toHaveBeenCalledWith(
  328. expect.objectContaining({
  329. query: expect.objectContaining({
  330. transaction: '/apple/cart',
  331. query: 'sentry:yes',
  332. }),
  333. })
  334. );
  335. });
  336. it('Default period for trends does not call updateDateTime', async function () {
  337. const data = initializeTrendsData({query: 'tag:value'}, false);
  338. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  339. context: data.routerContext,
  340. });
  341. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  342. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  343. });
  344. it('Navigating to trends does not modify statsPeriod when already set', async function () {
  345. const data = initializeTrendsData({
  346. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION} api`,
  347. statsPeriod: '24h',
  348. });
  349. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  350. context: data.routerContext,
  351. });
  352. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  353. const link = screen.getByRole('button', {name: 'View Trends'});
  354. userEvent.click(link);
  355. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  356. expect(browserHistory.push).toHaveBeenCalledWith(
  357. expect.objectContaining({
  358. pathname: '/organizations/org-slug/performance/trends/',
  359. query: {
  360. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  361. statsPeriod: '24h',
  362. },
  363. })
  364. );
  365. });
  366. it('Default page (transactions) without trends feature will not update filters if none are set', async function () {
  367. const projects = [
  368. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  369. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  370. ];
  371. const data = initializeData(projects, {view: undefined});
  372. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  373. context: data.routerContext,
  374. });
  375. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  376. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  377. });
  378. it('Default page (transactions) with trends feature will not update filters if none are set', async function () {
  379. const data = initializeTrendsData({view: undefined}, false);
  380. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  381. context: data.routerContext,
  382. });
  383. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  384. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  385. });
  386. it('Tags are replaced with trends default query if navigating to trends', async function () {
  387. const data = initializeTrendsData({query: 'device.family:Mac'}, false);
  388. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  389. context: data.routerContext,
  390. });
  391. const trendsLinks = await screen.findAllByTestId('landing-header-trends');
  392. userEvent.click(trendsLinks[0]);
  393. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  394. expect(browserHistory.push).toHaveBeenCalledWith(
  395. expect.objectContaining({
  396. pathname: '/organizations/org-slug/performance/trends/',
  397. query: {
  398. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  399. },
  400. })
  401. );
  402. });
  403. it('Display Create Sample Transaction Button', async function () {
  404. const projects = [
  405. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  406. TestStubs.Project({id: 2, firstTransactionEvent: false}),
  407. ];
  408. const data = initializeData(projects, {view: undefined});
  409. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  410. context: data.routerContext,
  411. });
  412. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  413. expect(screen.queryByTestId('create-sample-transaction-btn')).toBeInTheDocument();
  414. });
  415. });