content.spec.jsx 15 KB

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