content.spec.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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/events/',
  129. body: {
  130. meta: {
  131. fields: {
  132. user: 'string',
  133. transaction: 'string',
  134. 'project.id': 'integer',
  135. 'tpm()': 'number',
  136. 'p50()': 'number',
  137. 'p95()': 'number',
  138. 'failure_rate()': 'number',
  139. 'apdex(300)': 'number',
  140. 'count_unique(user)': 'number',
  141. 'count_miserable(user,300)': 'number',
  142. 'user_misery(300)': 'number',
  143. },
  144. },
  145. data: [
  146. {
  147. transaction: '/apple/cart',
  148. 'project.id': 1,
  149. user: 'uhoh@example.com',
  150. 'tpm()': 30,
  151. 'p50()': 100,
  152. 'p95()': 500,
  153. 'failure_rate()': 0.1,
  154. 'apdex(300)': 0.6,
  155. 'count_unique(user)': 1000,
  156. 'count_miserable(user,300)': 122,
  157. 'user_misery(300)': 0.114,
  158. },
  159. ],
  160. },
  161. match: [
  162. (_, options) => {
  163. if (!options.hasOwnProperty('query')) {
  164. return false;
  165. }
  166. if (!options.query?.hasOwnProperty('field')) {
  167. return false;
  168. }
  169. return !options.query?.field.includes('team_key_transaction');
  170. },
  171. ],
  172. });
  173. MockApiClient.addMockResponse({
  174. url: '/organizations/org-slug/events/',
  175. body: {
  176. meta: {
  177. fields: {
  178. user: 'string',
  179. transaction: 'string',
  180. 'project.id': 'integer',
  181. 'tpm()': 'number',
  182. 'p50()': 'number',
  183. 'p95()': 'number',
  184. 'failure_rate()': 'number',
  185. 'apdex(300)': 'number',
  186. 'count_unique(user)': 'number',
  187. 'count_miserable(user,300)': 'number',
  188. 'user_misery(300)': 'number',
  189. },
  190. },
  191. data: [
  192. {
  193. team_key_transaction: 1,
  194. transaction: '/apple/cart',
  195. 'project.id': 1,
  196. user: 'uhoh@example.com',
  197. 'tpm()': 30,
  198. 'p50()': 100,
  199. 'p95()': 500,
  200. 'failure_rate()': 0.1,
  201. 'apdex(300)': 0.6,
  202. 'count_unique(user)': 1000,
  203. 'count_miserable(user,300)': 122,
  204. 'user_misery(300)': 0.114,
  205. },
  206. {
  207. team_key_transaction: 0,
  208. transaction: '/apple/checkout',
  209. 'project.id': 1,
  210. user: 'uhoh@example.com',
  211. 'tpm()': 30,
  212. 'p50()': 100,
  213. 'p95()': 500,
  214. 'failure_rate()': 0.1,
  215. 'apdex(300)': 0.6,
  216. 'count_unique(user)': 1000,
  217. 'count_miserable(user,300)': 122,
  218. 'user_misery(300)': 0.114,
  219. },
  220. ],
  221. },
  222. match: [
  223. (_, options) => {
  224. if (!options.hasOwnProperty('query')) {
  225. return false;
  226. }
  227. if (!options.query?.hasOwnProperty('field')) {
  228. return false;
  229. }
  230. return options.query?.field.includes('team_key_transaction');
  231. },
  232. ],
  233. });
  234. MockApiClient.addMockResponse({
  235. url: '/organizations/org-slug/events-meta/',
  236. body: {
  237. count: 2,
  238. },
  239. });
  240. MockApiClient.addMockResponse({
  241. url: '/organizations/org-slug/events-trends/',
  242. body: {
  243. stats: {},
  244. events: {meta: {}, data: []},
  245. },
  246. });
  247. MockApiClient.addMockResponse({
  248. url: '/organizations/org-slug/events-trends-stats/',
  249. body: {
  250. stats: {},
  251. events: {meta: {}, data: []},
  252. },
  253. });
  254. MockApiClient.addMockResponse({
  255. url: '/organizations/org-slug/events-vitals/',
  256. body: {
  257. 'measurements.lcp': {
  258. poor: 1,
  259. meh: 2,
  260. good: 3,
  261. total: 6,
  262. p75: 4500,
  263. },
  264. },
  265. });
  266. MockApiClient.addMockResponse({
  267. method: 'GET',
  268. url: `/organizations/org-slug/key-transactions-list/`,
  269. body: [],
  270. });
  271. });
  272. afterEach(function () {
  273. MockApiClient.clearMockResponses();
  274. act(() => ProjectsStore.reset());
  275. // TODO: This was likely a defensive check added due to a previous isolation issue, it can possibly be removed.
  276. // @ts-ignore
  277. pageFilters.updateDateTime.mockRestore();
  278. });
  279. it('renders basic UI elements', async function () {
  280. const projects = [TestStubs.Project({firstTransactionEvent: true})];
  281. const data = initializeData(projects, {});
  282. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  283. context: data.routerContext,
  284. });
  285. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  286. expect(screen.getByTestId('performance-table')).toBeInTheDocument();
  287. expect(screen.queryByText('Pinpoint problems')).not.toBeInTheDocument();
  288. });
  289. it('renders onboarding state when the selected project has no events', async function () {
  290. const projects = [
  291. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  292. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  293. ];
  294. const data = initializeData(projects, {project: [1]});
  295. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  296. context: data.routerContext,
  297. });
  298. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  299. expect(screen.queryByText('Pinpoint problems')).toBeInTheDocument();
  300. expect(screen.queryByTestId('performance-table')).not.toBeInTheDocument();
  301. });
  302. it('does not render onboarding for "my projects"', async function () {
  303. const projects = [
  304. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  305. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  306. ];
  307. const data = initializeData(projects, {project: ['-1']});
  308. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  309. context: data.routerContext,
  310. });
  311. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  312. expect(screen.queryByText('Pinpoint problems')).not.toBeInTheDocument();
  313. });
  314. it('forwards conditions to transaction summary', async function () {
  315. const projects = [TestStubs.Project({id: '1', firstTransactionEvent: true})];
  316. const data = initializeData(projects, {project: ['1'], query: 'sentry:yes'});
  317. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  318. context: data.routerContext,
  319. });
  320. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  321. const link = screen.getByRole('link', {name: '/apple/cart'});
  322. userEvent.click(link);
  323. expect(data.router.push).toHaveBeenCalledWith(
  324. expect.objectContaining({
  325. query: expect.objectContaining({
  326. transaction: '/apple/cart',
  327. query: 'sentry:yes',
  328. }),
  329. })
  330. );
  331. });
  332. it('Default period for trends does not call updateDateTime', async function () {
  333. const data = initializeTrendsData({query: 'tag:value'}, false);
  334. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  335. context: data.routerContext,
  336. });
  337. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  338. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  339. });
  340. it('Navigating to trends does not modify statsPeriod when already set', async function () {
  341. const data = initializeTrendsData({
  342. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION} api`,
  343. statsPeriod: '24h',
  344. });
  345. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  346. context: data.routerContext,
  347. });
  348. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  349. const link = screen.getByRole('button', {name: 'View Trends'});
  350. userEvent.click(link);
  351. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  352. expect(browserHistory.push).toHaveBeenCalledWith(
  353. expect.objectContaining({
  354. pathname: '/organizations/org-slug/performance/trends/',
  355. query: {
  356. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  357. statsPeriod: '24h',
  358. },
  359. })
  360. );
  361. });
  362. it('Default page (transactions) without trends feature will not update filters if none are set', async function () {
  363. const projects = [
  364. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  365. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  366. ];
  367. const data = initializeData(projects, {view: undefined});
  368. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  369. context: data.routerContext,
  370. });
  371. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  372. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  373. });
  374. it('Default page (transactions) with trends feature will not update filters if none are set', async function () {
  375. const data = initializeTrendsData({view: undefined}, false);
  376. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  377. context: data.routerContext,
  378. });
  379. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  380. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  381. });
  382. it('Tags are replaced with trends default query if navigating to trends', async function () {
  383. const data = initializeTrendsData({query: 'device.family:Mac'}, false);
  384. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  385. context: data.routerContext,
  386. });
  387. const trendsLinks = await screen.findAllByTestId('landing-header-trends');
  388. userEvent.click(trendsLinks[0]);
  389. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  390. expect(browserHistory.push).toHaveBeenCalledWith(
  391. expect.objectContaining({
  392. pathname: '/organizations/org-slug/performance/trends/',
  393. query: {
  394. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  395. },
  396. })
  397. );
  398. });
  399. it('Display Create Sample Transaction Button', async function () {
  400. const projects = [
  401. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  402. TestStubs.Project({id: 2, firstTransactionEvent: false}),
  403. ];
  404. const data = initializeData(projects, {view: undefined});
  405. render(<WrappedComponent organization={data.organization} router={data.router} />, {
  406. context: data.routerContext,
  407. });
  408. expect(await screen.findByTestId('performance-landing-v3')).toBeInTheDocument();
  409. expect(screen.queryByTestId('create-sample-transaction-btn')).toBeInTheDocument();
  410. });
  411. });