content.spec.jsx 14 KB

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