content.spec.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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('div[data-test-id="performance-landing-v3"]').exists()).toBe(
  294. true
  295. );
  296. // No onboarding should show.
  297. expect(wrapper.find('Onboarding')).toHaveLength(0);
  298. // Table should render.
  299. expect(wrapper.find('Table')).toHaveLength(1);
  300. wrapper.unmount();
  301. });
  302. it('renders onboarding state when the selected project has no events', async function () {
  303. const projects = [
  304. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  305. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  306. ];
  307. const data = initializeData(projects, {project: [1]});
  308. const wrapper = mountWithTheme(
  309. <WrappedComponent organization={data.organization} router={data.router} />,
  310. data.routerContext
  311. );
  312. await act(async () => {
  313. await tick();
  314. wrapper.update();
  315. });
  316. // onboarding should show.
  317. expect(wrapper.find('Onboarding')).toHaveLength(1);
  318. // Table should not show.
  319. expect(wrapper.find('Table')).toHaveLength(0);
  320. wrapper.unmount();
  321. });
  322. it('does not render onboarding for "my projects"', async function () {
  323. const projects = [
  324. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  325. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  326. ];
  327. const data = initializeData(projects, {project: ['-1']});
  328. const wrapper = mountWithTheme(
  329. <WrappedComponent organization={data.organization} router={data.router} />,
  330. data.routerContext
  331. );
  332. await act(async () => {
  333. await tick();
  334. wrapper.update();
  335. });
  336. expect(wrapper.find('Onboarding')).toHaveLength(0);
  337. wrapper.unmount();
  338. });
  339. it('forwards conditions to transaction summary', async function () {
  340. const projects = [TestStubs.Project({id: '1', firstTransactionEvent: true})];
  341. const data = initializeData(projects, {project: ['1'], query: 'sentry:yes'});
  342. const wrapper = mountWithTheme(
  343. <WrappedComponent organization={data.organization} router={data.router} />,
  344. data.routerContext
  345. );
  346. await act(async () => {
  347. await tick();
  348. wrapper.update();
  349. });
  350. const link = wrapper.find('[data-test-id="grid-editable"] GridBody Link').at(0);
  351. link.simulate('click', {button: 0});
  352. expect(data.router.push).toHaveBeenCalledWith(
  353. expect.objectContaining({
  354. query: expect.objectContaining({
  355. transaction: '/apple/cart',
  356. query: 'sentry:yes',
  357. }),
  358. })
  359. );
  360. wrapper.unmount();
  361. });
  362. it('Default period for trends does not call updateDateTime', async function () {
  363. const data = initializeTrendsData({query: 'tag:value'}, false);
  364. const wrapper = mountWithTheme(
  365. <WrappedComponent organization={data.organization} router={data.router} />,
  366. data.routerContext
  367. );
  368. await act(async () => {
  369. await tick();
  370. wrapper.update();
  371. });
  372. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  373. wrapper.unmount();
  374. });
  375. it('Navigating to trends does not modify statsPeriod when already set', async function () {
  376. const data = initializeTrendsData({
  377. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION} api`,
  378. statsPeriod: '24h',
  379. });
  380. const wrapper = mountWithTheme(
  381. <WrappedComponent organization={data.organization} router={data.router} />,
  382. data.routerContext
  383. );
  384. await act(async () => {
  385. await tick();
  386. wrapper.update();
  387. });
  388. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  389. trendsLink.simulate('click');
  390. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  391. expect(browserHistory.push).toHaveBeenCalledWith(
  392. expect.objectContaining({
  393. pathname: '/organizations/org-slug/performance/trends/',
  394. query: {
  395. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  396. statsPeriod: '24h',
  397. },
  398. })
  399. );
  400. wrapper.unmount();
  401. });
  402. it('Default page (transactions) without trends feature will not update filters if none are set', async function () {
  403. const projects = [
  404. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  405. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  406. ];
  407. const data = initializeData(projects, {view: undefined});
  408. const wrapper = mountWithTheme(
  409. <WrappedComponent organization={data.organization} router={data.router} />,
  410. data.routerContext
  411. );
  412. await act(async () => {
  413. await tick();
  414. wrapper.update();
  415. });
  416. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  417. wrapper.unmount();
  418. });
  419. it('Default page (transactions) with trends feature will not update filters if none are set', async function () {
  420. const data = initializeTrendsData({view: undefined}, false);
  421. const wrapper = mountWithTheme(
  422. <WrappedComponent organization={data.organization} router={data.router} />,
  423. data.routerContext
  424. );
  425. await act(async () => {
  426. await tick();
  427. wrapper.update();
  428. });
  429. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  430. wrapper.unmount();
  431. });
  432. it('Tags are replaced with trends default query if navigating to trends', async function () {
  433. const data = initializeTrendsData({query: 'device.family:Mac'}, false);
  434. const wrapper = mountWithTheme(
  435. <WrappedComponent organization={data.organization} router={data.router} />,
  436. data.routerContext
  437. );
  438. await act(async () => {
  439. await tick();
  440. wrapper.update();
  441. });
  442. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  443. trendsLink.simulate('click');
  444. expect(browserHistory.push).toHaveBeenCalledWith(
  445. expect.objectContaining({
  446. pathname: '/organizations/org-slug/performance/trends/',
  447. query: {
  448. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  449. },
  450. })
  451. );
  452. wrapper.unmount();
  453. });
  454. it('Display Create Sample Transaction Button', async function () {
  455. const projects = [
  456. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  457. TestStubs.Project({id: 2, firstTransactionEvent: false}),
  458. ];
  459. const data = initializeData(projects, {view: undefined});
  460. const wrapper = mountWithTheme(
  461. <WrappedComponent organization={data.organization} router={data.router} />,
  462. data.routerContext
  463. );
  464. await act(async () => {
  465. await tick();
  466. wrapper.update();
  467. });
  468. expect(
  469. wrapper.find('Button[data-test-id="create-sample-transaction-btn"]').exists()
  470. ).toBe(true);
  471. wrapper.unmount();
  472. });
  473. });