content.spec.tsx 14 KB

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