content.spec.tsx 15 KB

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