content.spec.tsx 15 KB

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