content.spec.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. import {browserHistory} from 'react-router';
  2. import {QueryClient, QueryClientProvider} from '@tanstack/react-query';
  3. import {enforceActOnUseLegacyStoreHook, mountWithTheme} from 'sentry-test/enzyme';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {act} 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 {OrganizationContext} from 'sentry/views/organizationContext';
  12. import PerformanceContent from 'sentry/views/performance/content';
  13. import {DEFAULT_MAX_DURATION} from 'sentry/views/performance/trends/utils';
  14. import {RouteContext} from 'sentry/views/routeContext';
  15. const FEATURES = ['performance-view'];
  16. function WrappedComponent({organization, isMEPEnabled = false, router}) {
  17. const client = new QueryClient();
  18. return (
  19. <QueryClientProvider client={client}>
  20. <RouteContext.Provider
  21. value={{
  22. location,
  23. params: {},
  24. router,
  25. routes: [],
  26. }}
  27. >
  28. <OrganizationContext.Provider value={organization}>
  29. <MEPSettingProvider _isMEPEnabled={isMEPEnabled}>
  30. <PerformanceContent organization={organization} 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 = TestStubs.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 = 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. 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. enforceActOnUseLegacyStoreHook();
  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/eventsv2/',
  129. body: {
  130. meta: {
  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. 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/eventsv2/',
  173. body: {
  174. meta: {
  175. user: 'string',
  176. transaction: 'string',
  177. 'project.id': 'integer',
  178. tpm: 'number',
  179. p50: 'number',
  180. p95: 'number',
  181. failure_rate: 'number',
  182. apdex_300: 'number',
  183. count_unique_user: 'number',
  184. count_miserable_user_300: 'number',
  185. user_misery_300: 'number',
  186. },
  187. data: [
  188. {
  189. team_key_transaction: 1,
  190. transaction: '/apple/cart',
  191. 'project.id': 1,
  192. user: 'uhoh@example.com',
  193. tpm: 30,
  194. p50: 100,
  195. p95: 500,
  196. failure_rate: 0.1,
  197. apdex_300: 0.6,
  198. count_unique_user: 1000,
  199. count_miserable_user_300: 122,
  200. user_misery_300: 0.114,
  201. },
  202. {
  203. team_key_transaction: 0,
  204. transaction: '/apple/checkout',
  205. 'project.id': 1,
  206. user: 'uhoh@example.com',
  207. tpm: 30,
  208. p50: 100,
  209. p95: 500,
  210. failure_rate: 0.1,
  211. apdex_300: 0.6,
  212. count_unique_user: 1000,
  213. count_miserable_user_300: 122,
  214. user_misery_300: 0.114,
  215. },
  216. ],
  217. },
  218. match: [
  219. (_, options) => {
  220. if (!options.hasOwnProperty('query')) {
  221. return false;
  222. }
  223. if (!options.query.hasOwnProperty('field')) {
  224. return false;
  225. }
  226. return options.query.field.includes('team_key_transaction');
  227. },
  228. ],
  229. });
  230. MockApiClient.addMockResponse({
  231. url: '/organizations/org-slug/events-meta/',
  232. body: {
  233. count: 2,
  234. },
  235. });
  236. MockApiClient.addMockResponse({
  237. url: '/organizations/org-slug/events-trends/',
  238. body: {
  239. stats: {},
  240. events: {meta: {}, data: []},
  241. },
  242. });
  243. MockApiClient.addMockResponse({
  244. url: '/organizations/org-slug/events-trends-stats/',
  245. body: {
  246. stats: {},
  247. events: {meta: {}, data: []},
  248. },
  249. });
  250. MockApiClient.addMockResponse({
  251. url: '/organizations/org-slug/events-vitals/',
  252. body: {
  253. 'measurements.lcp': {
  254. poor: 1,
  255. meh: 2,
  256. good: 3,
  257. total: 6,
  258. p75: 4500,
  259. },
  260. },
  261. });
  262. MockApiClient.addMockResponse({
  263. method: 'GET',
  264. url: `/organizations/org-slug/key-transactions-list/`,
  265. body: [],
  266. });
  267. MockApiClient.addMockResponse({
  268. method: 'GET',
  269. url: `/organizations/org-slug/events/`,
  270. body: {
  271. data: [{}],
  272. meta: {},
  273. },
  274. });
  275. });
  276. afterEach(function () {
  277. MockApiClient.clearMockResponses();
  278. act(() => ProjectsStore.reset());
  279. pageFilters.updateDateTime.mockRestore();
  280. });
  281. it('renders basic UI elements', async function () {
  282. const projects = [TestStubs.Project({firstTransactionEvent: true})];
  283. const data = initializeData(projects, {});
  284. const wrapper = mountWithTheme(
  285. <WrappedComponent organization={data.organization} router={data.router} />,
  286. data.routerContext
  287. );
  288. await act(async () => {
  289. await tick();
  290. wrapper.update();
  291. });
  292. // performance landing container
  293. expect(wrapper.find('[data-test-id="performance-landing-v3"]').exists()).toBe(true);
  294. // No onboarding should show.
  295. expect(wrapper.find('Onboarding')).toHaveLength(0);
  296. // Table should render.
  297. expect(wrapper.find('Table')).toHaveLength(1);
  298. wrapper.unmount();
  299. });
  300. it('renders onboarding state when the selected project has no events', 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. const wrapper = mountWithTheme(
  307. <WrappedComponent organization={data.organization} router={data.router} />,
  308. data.routerContext
  309. );
  310. await act(async () => {
  311. await tick();
  312. wrapper.update();
  313. });
  314. // onboarding should show.
  315. expect(wrapper.find('Onboarding')).toHaveLength(1);
  316. // Table should not show.
  317. expect(wrapper.find('Table')).toHaveLength(0);
  318. wrapper.unmount();
  319. });
  320. it('does not render onboarding for "my projects"', async function () {
  321. const projects = [
  322. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  323. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  324. ];
  325. const data = initializeData(projects, {project: ['-1']});
  326. const wrapper = mountWithTheme(
  327. <WrappedComponent organization={data.organization} router={data.router} />,
  328. data.routerContext
  329. );
  330. await act(async () => {
  331. await tick();
  332. wrapper.update();
  333. });
  334. expect(wrapper.find('Onboarding')).toHaveLength(0);
  335. wrapper.unmount();
  336. });
  337. it('forwards conditions to transaction summary', async function () {
  338. const projects = [TestStubs.Project({id: '1', firstTransactionEvent: true})];
  339. const data = initializeData(projects, {project: ['1'], query: 'sentry:yes'});
  340. const wrapper = mountWithTheme(
  341. <WrappedComponent organization={data.organization} router={data.router} />,
  342. data.routerContext
  343. );
  344. await act(async () => {
  345. await tick();
  346. wrapper.update();
  347. });
  348. const link = wrapper.find('[data-test-id="grid-editable"] GridBody Link').at(0);
  349. link.simulate('click', {button: 0});
  350. expect(data.router.push).toHaveBeenCalledWith(
  351. expect.objectContaining({
  352. query: expect.objectContaining({
  353. transaction: '/apple/cart',
  354. query: 'sentry:yes',
  355. }),
  356. })
  357. );
  358. wrapper.unmount();
  359. });
  360. it('Default period for trends does not call updateDateTime', async function () {
  361. const data = initializeTrendsData({query: 'tag:value'}, false);
  362. const wrapper = mountWithTheme(
  363. <WrappedComponent organization={data.organization} router={data.router} />,
  364. data.routerContext
  365. );
  366. await act(async () => {
  367. await tick();
  368. wrapper.update();
  369. });
  370. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  371. wrapper.unmount();
  372. });
  373. it('Navigating to trends does not modify statsPeriod when already set', async function () {
  374. const data = initializeTrendsData({
  375. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION} api`,
  376. statsPeriod: '24h',
  377. });
  378. const wrapper = mountWithTheme(
  379. <WrappedComponent organization={data.organization} router={data.router} />,
  380. data.routerContext
  381. );
  382. await act(async () => {
  383. await tick();
  384. wrapper.update();
  385. });
  386. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  387. trendsLink.simulate('click');
  388. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  389. expect(browserHistory.push).toHaveBeenCalledWith(
  390. expect.objectContaining({
  391. pathname: '/organizations/org-slug/performance/trends/',
  392. query: {
  393. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  394. statsPeriod: '24h',
  395. },
  396. })
  397. );
  398. wrapper.unmount();
  399. });
  400. it('Default page (transactions) without trends feature will not update filters if none are set', async function () {
  401. const projects = [
  402. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  403. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  404. ];
  405. const data = initializeData(projects, {view: undefined});
  406. const wrapper = mountWithTheme(
  407. <WrappedComponent organization={data.organization} router={data.router} />,
  408. data.routerContext
  409. );
  410. await act(async () => {
  411. await tick();
  412. wrapper.update();
  413. });
  414. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  415. wrapper.unmount();
  416. });
  417. it('Default page (transactions) with trends feature will not update filters if none are set', async function () {
  418. const data = initializeTrendsData({view: undefined}, false);
  419. const wrapper = mountWithTheme(
  420. <WrappedComponent organization={data.organization} router={data.router} />,
  421. data.routerContext
  422. );
  423. await act(async () => {
  424. await tick();
  425. wrapper.update();
  426. });
  427. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  428. wrapper.unmount();
  429. });
  430. it('Tags are replaced with trends default query if navigating to trends', async function () {
  431. const data = initializeTrendsData({query: 'device.family:Mac'}, false);
  432. const wrapper = mountWithTheme(
  433. <WrappedComponent organization={data.organization} router={data.router} />,
  434. data.routerContext
  435. );
  436. await act(async () => {
  437. await tick();
  438. wrapper.update();
  439. });
  440. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  441. trendsLink.simulate('click');
  442. expect(browserHistory.push).toHaveBeenCalledWith(
  443. expect.objectContaining({
  444. pathname: '/organizations/org-slug/performance/trends/',
  445. query: {
  446. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  447. },
  448. })
  449. );
  450. wrapper.unmount();
  451. });
  452. it('Display Create Sample Transaction Button', async function () {
  453. const projects = [
  454. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  455. TestStubs.Project({id: 2, firstTransactionEvent: false}),
  456. ];
  457. const data = initializeData(projects, {view: undefined});
  458. const wrapper = mountWithTheme(
  459. <WrappedComponent organization={data.organization} router={data.router} />,
  460. data.routerContext
  461. );
  462. await act(async () => {
  463. await tick();
  464. wrapper.update();
  465. });
  466. expect(
  467. wrapper.find('Button[data-test-id="create-sample-transaction-btn"]').exists()
  468. ).toBe(true);
  469. wrapper.unmount();
  470. });
  471. });