content.spec.jsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import {browserHistory} from 'react-router';
  2. import {enforceActOnUseLegacyStoreHook, mountWithTheme} from 'sentry-test/enzyme';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {act} 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 {MEPSettingProvider} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  10. import {OrganizationContext} from 'sentry/views/organizationContext';
  11. import PerformanceContent from 'sentry/views/performance/content';
  12. import {DEFAULT_MAX_DURATION} from 'sentry/views/performance/trends/utils';
  13. import {RouteContext} from 'sentry/views/routeContext';
  14. const FEATURES = ['performance-view'];
  15. function WrappedComponent({organization, isMEPEnabled = false, router}) {
  16. return (
  17. <RouteContext.Provider
  18. value={{
  19. location,
  20. params: {},
  21. router,
  22. routes: [],
  23. }}
  24. >
  25. <OrganizationContext.Provider value={organization}>
  26. <MEPSettingProvider _isMEPEnabled={isMEPEnabled}>
  27. <PerformanceContent organization={organization} location={router.location} />
  28. </MEPSettingProvider>
  29. </OrganizationContext.Provider>
  30. </RouteContext.Provider>
  31. );
  32. }
  33. function initializeData(projects, query, features = FEATURES) {
  34. const organization = TestStubs.Organization({
  35. features,
  36. projects,
  37. });
  38. const initialData = initializeOrg({
  39. organization,
  40. router: {
  41. location: {
  42. pathname: '/test',
  43. query: query || {},
  44. },
  45. },
  46. });
  47. act(() => void OrganizationStore.onUpdate(initialData.organization, {replace: true}));
  48. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  49. return initialData;
  50. }
  51. function initializeTrendsData(query, addDefaultQuery = true) {
  52. const projects = [
  53. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  54. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  55. ];
  56. const organization = TestStubs.Organization({
  57. FEATURES,
  58. projects,
  59. });
  60. const otherTrendsQuery = addDefaultQuery
  61. ? {
  62. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  63. }
  64. : {};
  65. const initialData = initializeOrg({
  66. organization,
  67. router: {
  68. location: {
  69. pathname: '/test',
  70. query: {
  71. ...otherTrendsQuery,
  72. ...query,
  73. },
  74. },
  75. },
  76. });
  77. act(() => ProjectsStore.loadInitialData(initialData.organization.projects));
  78. return initialData;
  79. }
  80. describe('Performance > Content', function () {
  81. enforceActOnUseLegacyStoreHook();
  82. beforeEach(function () {
  83. act(() => void TeamStore.loadInitialData([], false, null));
  84. browserHistory.push = jest.fn();
  85. jest.spyOn(pageFilters, 'updateDateTime');
  86. MockApiClient.addMockResponse({
  87. url: '/organizations/org-slug/projects/',
  88. body: [],
  89. });
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/tags/',
  92. body: [],
  93. });
  94. MockApiClient.addMockResponse({
  95. url: '/organizations/org-slug/events-stats/',
  96. body: {data: [[123, []]]},
  97. });
  98. MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/events-histogram/',
  100. body: {'transaction.duration': [{bin: 0, count: 1000}]},
  101. });
  102. MockApiClient.addMockResponse({
  103. url: '/organizations/org-slug/users/',
  104. body: [],
  105. });
  106. MockApiClient.addMockResponse({
  107. url: '/organizations/org-slug/recent-searches/',
  108. body: [],
  109. });
  110. MockApiClient.addMockResponse({
  111. url: '/organizations/org-slug/recent-searches/',
  112. method: 'POST',
  113. body: [],
  114. });
  115. MockApiClient.addMockResponse({
  116. url: '/organizations/org-slug/sdk-updates/',
  117. body: [],
  118. });
  119. MockApiClient.addMockResponse({
  120. url: '/prompts-activity/',
  121. body: {},
  122. });
  123. MockApiClient.addMockResponse({
  124. url: '/organizations/org-slug/eventsv2/',
  125. body: {
  126. meta: {
  127. user: 'string',
  128. transaction: 'string',
  129. 'project.id': 'integer',
  130. tpm: 'number',
  131. p50: 'number',
  132. p95: 'number',
  133. failure_rate: 'number',
  134. apdex_300: 'number',
  135. count_unique_user: 'number',
  136. count_miserable_user_300: 'number',
  137. user_misery_300: 'number',
  138. },
  139. data: [
  140. {
  141. transaction: '/apple/cart',
  142. 'project.id': 1,
  143. user: 'uhoh@example.com',
  144. tpm: 30,
  145. p50: 100,
  146. p95: 500,
  147. failure_rate: 0.1,
  148. apdex_300: 0.6,
  149. count_unique_user: 1000,
  150. count_miserable_user_300: 122,
  151. user_misery_300: 0.114,
  152. },
  153. ],
  154. },
  155. match: [
  156. (_, options) => {
  157. if (!options.hasOwnProperty('query')) {
  158. return false;
  159. }
  160. if (!options.query.hasOwnProperty('field')) {
  161. return false;
  162. }
  163. return !options.query.field.includes('team_key_transaction');
  164. },
  165. ],
  166. });
  167. MockApiClient.addMockResponse({
  168. url: '/organizations/org-slug/eventsv2/',
  169. body: {
  170. meta: {
  171. user: 'string',
  172. transaction: 'string',
  173. 'project.id': 'integer',
  174. tpm: 'number',
  175. p50: 'number',
  176. p95: 'number',
  177. failure_rate: 'number',
  178. apdex_300: 'number',
  179. count_unique_user: 'number',
  180. count_miserable_user_300: 'number',
  181. user_misery_300: 'number',
  182. },
  183. data: [
  184. {
  185. team_key_transaction: 1,
  186. transaction: '/apple/cart',
  187. 'project.id': 1,
  188. user: 'uhoh@example.com',
  189. tpm: 30,
  190. p50: 100,
  191. p95: 500,
  192. failure_rate: 0.1,
  193. apdex_300: 0.6,
  194. count_unique_user: 1000,
  195. count_miserable_user_300: 122,
  196. user_misery_300: 0.114,
  197. },
  198. {
  199. team_key_transaction: 0,
  200. transaction: '/apple/checkout',
  201. 'project.id': 1,
  202. user: 'uhoh@example.com',
  203. tpm: 30,
  204. p50: 100,
  205. p95: 500,
  206. failure_rate: 0.1,
  207. apdex_300: 0.6,
  208. count_unique_user: 1000,
  209. count_miserable_user_300: 122,
  210. user_misery_300: 0.114,
  211. },
  212. ],
  213. },
  214. match: [
  215. (_, options) => {
  216. if (!options.hasOwnProperty('query')) {
  217. return false;
  218. }
  219. if (!options.query.hasOwnProperty('field')) {
  220. return false;
  221. }
  222. return options.query.field.includes('team_key_transaction');
  223. },
  224. ],
  225. });
  226. MockApiClient.addMockResponse({
  227. url: '/organizations/org-slug/events-meta/',
  228. body: {
  229. count: 2,
  230. },
  231. });
  232. MockApiClient.addMockResponse({
  233. url: '/organizations/org-slug/events-trends/',
  234. body: {
  235. stats: {},
  236. events: {meta: {}, data: []},
  237. },
  238. });
  239. MockApiClient.addMockResponse({
  240. url: '/organizations/org-slug/events-trends-stats/',
  241. body: {
  242. stats: {},
  243. events: {meta: {}, data: []},
  244. },
  245. });
  246. MockApiClient.addMockResponse({
  247. url: '/organizations/org-slug/events-vitals/',
  248. body: {
  249. 'measurements.lcp': {
  250. poor: 1,
  251. meh: 2,
  252. good: 3,
  253. total: 6,
  254. p75: 4500,
  255. },
  256. },
  257. });
  258. MockApiClient.addMockResponse({
  259. method: 'GET',
  260. url: `/organizations/org-slug/key-transactions-list/`,
  261. body: [],
  262. });
  263. MockApiClient.addMockResponse({
  264. method: 'GET',
  265. url: `/organizations/org-slug/events/`,
  266. body: {
  267. data: [{}],
  268. meta: {},
  269. },
  270. });
  271. });
  272. afterEach(function () {
  273. MockApiClient.clearMockResponses();
  274. act(() => ProjectsStore.reset());
  275. pageFilters.updateDateTime.mockRestore();
  276. });
  277. it('renders basic UI elements', async function () {
  278. const projects = [TestStubs.Project({firstTransactionEvent: true})];
  279. const data = initializeData(projects, {});
  280. const wrapper = mountWithTheme(
  281. <WrappedComponent organization={data.organization} router={data.router} />,
  282. data.routerContext
  283. );
  284. await act(async () => {
  285. await tick();
  286. wrapper.update();
  287. });
  288. // performance landing container
  289. expect(wrapper.find('div[data-test-id="performance-landing-v3"]').exists()).toBe(
  290. true
  291. );
  292. // No onboarding should show.
  293. expect(wrapper.find('Onboarding')).toHaveLength(0);
  294. // Table should render.
  295. expect(wrapper.find('Table')).toHaveLength(1);
  296. wrapper.unmount();
  297. });
  298. it('renders onboarding state when the selected project has no events', async function () {
  299. const projects = [
  300. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  301. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  302. ];
  303. const data = initializeData(projects, {project: [1]});
  304. const wrapper = mountWithTheme(
  305. <WrappedComponent organization={data.organization} router={data.router} />,
  306. data.routerContext
  307. );
  308. await act(async () => {
  309. await tick();
  310. wrapper.update();
  311. });
  312. // onboarding should show.
  313. expect(wrapper.find('Onboarding')).toHaveLength(1);
  314. // Table should not show.
  315. expect(wrapper.find('Table')).toHaveLength(0);
  316. wrapper.unmount();
  317. });
  318. it('does not render onboarding for "my projects"', async function () {
  319. const projects = [
  320. TestStubs.Project({id: '1', firstTransactionEvent: false}),
  321. TestStubs.Project({id: '2', firstTransactionEvent: true}),
  322. ];
  323. const data = initializeData(projects, {project: ['-1']});
  324. const wrapper = mountWithTheme(
  325. <WrappedComponent organization={data.organization} router={data.router} />,
  326. data.routerContext
  327. );
  328. await act(async () => {
  329. await tick();
  330. wrapper.update();
  331. });
  332. expect(wrapper.find('Onboarding')).toHaveLength(0);
  333. wrapper.unmount();
  334. });
  335. it('forwards conditions to transaction summary', async function () {
  336. const projects = [TestStubs.Project({id: '1', firstTransactionEvent: true})];
  337. const data = initializeData(projects, {project: ['1'], query: 'sentry:yes'});
  338. const wrapper = mountWithTheme(
  339. <WrappedComponent organization={data.organization} router={data.router} />,
  340. data.routerContext
  341. );
  342. await act(async () => {
  343. await tick();
  344. wrapper.update();
  345. });
  346. const link = wrapper.find('[data-test-id="grid-editable"] GridBody Link').at(0);
  347. link.simulate('click', {button: 0});
  348. expect(data.router.push).toHaveBeenCalledWith(
  349. expect.objectContaining({
  350. query: expect.objectContaining({
  351. transaction: '/apple/cart',
  352. query: 'sentry:yes',
  353. }),
  354. })
  355. );
  356. wrapper.unmount();
  357. });
  358. it('Default period for trends does not call updateDateTime', async function () {
  359. const data = initializeTrendsData({query: 'tag:value'}, false);
  360. const wrapper = mountWithTheme(
  361. <WrappedComponent organization={data.organization} router={data.router} />,
  362. data.routerContext
  363. );
  364. await act(async () => {
  365. await tick();
  366. wrapper.update();
  367. });
  368. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  369. wrapper.unmount();
  370. });
  371. it('Navigating to trends does not modify statsPeriod when already set', async function () {
  372. const data = initializeTrendsData({
  373. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION} api`,
  374. statsPeriod: '24h',
  375. });
  376. const wrapper = mountWithTheme(
  377. <WrappedComponent organization={data.organization} router={data.router} />,
  378. data.routerContext
  379. );
  380. await act(async () => {
  381. await tick();
  382. wrapper.update();
  383. });
  384. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  385. trendsLink.simulate('click');
  386. expect(pageFilters.updateDateTime).toHaveBeenCalledTimes(0);
  387. expect(browserHistory.push).toHaveBeenCalledWith(
  388. expect.objectContaining({
  389. pathname: '/organizations/org-slug/performance/trends/',
  390. query: {
  391. query: `tpm():>0.005 transaction.duration:>10 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  392. statsPeriod: '24h',
  393. },
  394. })
  395. );
  396. wrapper.unmount();
  397. });
  398. it('Default page (transactions) without trends feature will not update filters if none are set', async function () {
  399. const projects = [
  400. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  401. TestStubs.Project({id: 2, firstTransactionEvent: true}),
  402. ];
  403. const data = initializeData(projects, {view: undefined});
  404. const wrapper = mountWithTheme(
  405. <WrappedComponent organization={data.organization} router={data.router} />,
  406. data.routerContext
  407. );
  408. await act(async () => {
  409. await tick();
  410. wrapper.update();
  411. });
  412. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  413. wrapper.unmount();
  414. });
  415. it('Default page (transactions) with trends feature will not update filters if none are set', async function () {
  416. const data = initializeTrendsData({view: undefined}, false);
  417. const wrapper = mountWithTheme(
  418. <WrappedComponent organization={data.organization} router={data.router} />,
  419. data.routerContext
  420. );
  421. await act(async () => {
  422. await tick();
  423. wrapper.update();
  424. });
  425. expect(browserHistory.push).toHaveBeenCalledTimes(0);
  426. wrapper.unmount();
  427. });
  428. it('Tags are replaced with trends default query if navigating to trends', async function () {
  429. const data = initializeTrendsData({query: 'device.family:Mac'}, false);
  430. const wrapper = mountWithTheme(
  431. <WrappedComponent organization={data.organization} router={data.router} />,
  432. data.routerContext
  433. );
  434. await act(async () => {
  435. await tick();
  436. wrapper.update();
  437. });
  438. const trendsLink = wrapper.find('[data-test-id="landing-header-trends"]').at(0);
  439. trendsLink.simulate('click');
  440. expect(browserHistory.push).toHaveBeenCalledWith(
  441. expect.objectContaining({
  442. pathname: '/organizations/org-slug/performance/trends/',
  443. query: {
  444. query: `tpm():>0.01 transaction.duration:>0 transaction.duration:<${DEFAULT_MAX_DURATION}`,
  445. },
  446. })
  447. );
  448. wrapper.unmount();
  449. });
  450. it('Display Create Sample Transaction Button', async function () {
  451. const projects = [
  452. TestStubs.Project({id: 1, firstTransactionEvent: false}),
  453. TestStubs.Project({id: 2, firstTransactionEvent: false}),
  454. ];
  455. const data = initializeData(projects, {view: undefined});
  456. const wrapper = mountWithTheme(
  457. <WrappedComponent organization={data.organization} router={data.router} />,
  458. data.routerContext
  459. );
  460. await act(async () => {
  461. await tick();
  462. wrapper.update();
  463. });
  464. expect(
  465. wrapper.find('Button[data-test-id="create-sample-transaction-btn"]').exists()
  466. ).toBe(true);
  467. wrapper.unmount();
  468. });
  469. });