content.spec.tsx 14 KB

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