transactionVitals.spec.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 ProjectsStore from 'sentry/stores/projectsStore';
  6. import {OrganizationContext} from 'sentry/views/organizationContext';
  7. import TransactionVitals from 'sentry/views/performance/transactionSummary/transactionVitals';
  8. import {
  9. VITAL_GROUPS,
  10. ZOOM_KEYS,
  11. } from 'sentry/views/performance/transactionSummary/transactionVitals/constants';
  12. function initialize({project, features, transaction, query} = {}) {
  13. features = features || ['performance-view'];
  14. project = project || TestStubs.Project();
  15. query = query || {};
  16. const data = initializeOrg({
  17. organization: TestStubs.Organization({
  18. features,
  19. projects: [project],
  20. }),
  21. router: {
  22. location: {
  23. query: {
  24. transaction: transaction || '/',
  25. project: project.id,
  26. ...query,
  27. },
  28. },
  29. },
  30. });
  31. act(() => ProjectsStore.loadInitialData(data.organization.projects));
  32. return data;
  33. }
  34. const WrappedComponent = ({organization, ...props}) => {
  35. return (
  36. <OrganizationContext.Provider value={organization}>
  37. <TransactionVitals organization={organization} {...props} />
  38. </OrganizationContext.Provider>
  39. );
  40. };
  41. /**
  42. * These values are what we expect to see on the page based on the
  43. * mocked api responses below.
  44. */
  45. const vitals = [
  46. {
  47. slug: 'fp',
  48. heading: 'First Paint (FP)',
  49. baseline: '4.57s',
  50. },
  51. {
  52. slug: 'fcp',
  53. heading: 'First Contentful Paint (FCP)',
  54. baseline: '1.46s',
  55. },
  56. {
  57. slug: 'lcp',
  58. heading: 'Largest Contentful Paint (LCP)',
  59. baseline: '1.34s',
  60. },
  61. {
  62. slug: 'fid',
  63. heading: 'First Input Delay (FID)',
  64. baseline: '987.00ms',
  65. },
  66. {
  67. slug: 'cls',
  68. heading: 'Cumulative Layout Shift (CLS)',
  69. baseline: '0.02',
  70. },
  71. ];
  72. describe('Performance > Web Vitals', function () {
  73. enforceActOnUseLegacyStoreHook();
  74. beforeEach(function () {
  75. // @ts-ignore no-console
  76. // eslint-disable-next-line no-console
  77. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/projects/',
  80. body: [],
  81. });
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/project-transaction-threshold-override/',
  84. method: 'GET',
  85. body: {
  86. threshold: '800',
  87. metric: 'lcp',
  88. },
  89. });
  90. // Mock baseline measurements
  91. MockApiClient.addMockResponse({
  92. url: '/organizations/org-slug/events-vitals/',
  93. body: {
  94. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  95. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  96. 'measurements.lcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1342},
  97. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 987},
  98. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 0.02},
  99. },
  100. });
  101. const histogramData = {};
  102. const webVitals = VITAL_GROUPS.reduce((vs, group) => vs.concat(group.vitals), []);
  103. for (const measurement of webVitals) {
  104. const data = [];
  105. for (let i = 0; i < 100; i++) {
  106. data.push({
  107. histogram: i,
  108. count: i,
  109. });
  110. }
  111. histogramData[`measurements.${measurement}`] = data;
  112. }
  113. MockApiClient.addMockResponse({
  114. url: '/organizations/org-slug/events-histogram/',
  115. body: histogramData,
  116. });
  117. MockApiClient.addMockResponse({
  118. method: 'GET',
  119. url: `/organizations/org-slug/key-transactions-list/`,
  120. body: [],
  121. });
  122. MockApiClient.addMockResponse({
  123. url: '/prompts-activity/',
  124. body: {},
  125. });
  126. MockApiClient.addMockResponse({
  127. url: '/organizations/org-slug/sdk-updates/',
  128. body: [],
  129. });
  130. });
  131. afterEach(() => {
  132. // @ts-ignore no-console
  133. // eslint-disable-next-line no-console
  134. console.error.mockRestore();
  135. });
  136. it('render no access without feature', async function () {
  137. const {organization, router} = initialize({
  138. features: [],
  139. });
  140. const wrapper = mountWithTheme(
  141. <WrappedComponent organization={organization} location={router.location} />
  142. );
  143. await tick();
  144. wrapper.update();
  145. expect(wrapper.text()).toEqual("You don't have access to this feature");
  146. });
  147. it('renders the basic UI components', async function () {
  148. const {organization, router, routerContext} = initialize();
  149. const wrapper = mountWithTheme(
  150. <WrappedComponent
  151. organization={organization}
  152. location={router.location}
  153. router={router}
  154. />,
  155. routerContext
  156. );
  157. await tick();
  158. wrapper.update();
  159. expect(wrapper.find('TransactionHeader')).toHaveLength(1);
  160. expect(wrapper.find('SearchBar')).toHaveLength(1);
  161. expect(wrapper.find('TransactionVitals')).toHaveLength(1);
  162. });
  163. it('renders the correct bread crumbs', async function () {
  164. const {organization, router, routerContext} = initialize();
  165. const wrapper = mountWithTheme(
  166. <WrappedComponent
  167. organization={organization}
  168. location={router.location}
  169. router={router}
  170. />,
  171. routerContext
  172. );
  173. await tick();
  174. wrapper.update();
  175. expect(wrapper.find('Breadcrumb').text()).toEqual(
  176. expect.stringContaining('Web Vitals')
  177. );
  178. });
  179. it('renders all vitals cards correctly', async function () {
  180. const {organization, router, routerContext} = initialize();
  181. const wrapper = mountWithTheme(
  182. <WrappedComponent
  183. organization={organization}
  184. location={router.location}
  185. router={router}
  186. />,
  187. routerContext
  188. );
  189. await tick();
  190. wrapper.update();
  191. const vitalCards = wrapper.find('VitalCard');
  192. expect(vitalCards).toHaveLength(5);
  193. vitalCards.forEach((vitalCard, i) => {
  194. expect(vitalCard.find('CardSectionHeading').text()).toEqual(
  195. expect.stringContaining(vitals[i].heading)
  196. );
  197. expect(vitalCard.find('StatNumber').text()).toEqual(vitals[i].baseline);
  198. });
  199. expect(vitalCards.find('BarChart')).toHaveLength(5);
  200. });
  201. describe('reset view', function () {
  202. it('disables button on default view', async function () {
  203. const {organization, router, routerContext} = initialize();
  204. const wrapper = mountWithTheme(
  205. <WrappedComponent
  206. organization={organization}
  207. location={router.location}
  208. router={router}
  209. />,
  210. routerContext
  211. );
  212. await tick();
  213. wrapper.update();
  214. expect(
  215. wrapper.find('Button[data-test-id="reset-view"]').prop('disabled')
  216. ).toBeTruthy();
  217. });
  218. it('enables button on left zoom', async function () {
  219. const {organization, router, routerContext} = initialize({
  220. query: {
  221. lcpStart: '20',
  222. },
  223. });
  224. const wrapper = mountWithTheme(
  225. <WrappedComponent
  226. organization={organization}
  227. location={router.location}
  228. router={router}
  229. />,
  230. routerContext
  231. );
  232. await tick();
  233. wrapper.update();
  234. expect(
  235. wrapper.find('Button[data-test-id="reset-view"]').prop('disabled')
  236. ).toBeFalsy();
  237. });
  238. it('enables button on right zoom', async function () {
  239. const {organization, router, routerContext} = initialize({
  240. query: {
  241. fpEnd: '20',
  242. },
  243. });
  244. const wrapper = mountWithTheme(
  245. <WrappedComponent
  246. organization={organization}
  247. location={router.location}
  248. router={router}
  249. />,
  250. routerContext
  251. );
  252. await tick();
  253. wrapper.update();
  254. expect(
  255. wrapper.find('Button[data-test-id="reset-view"]').prop('disabled')
  256. ).toBeFalsy();
  257. });
  258. it('enables button on left and right zoom', async function () {
  259. const {organization, router, routerContext} = initialize({
  260. query: {
  261. fcpStart: '20',
  262. fcpEnd: '20',
  263. },
  264. });
  265. const wrapper = mountWithTheme(
  266. <WrappedComponent
  267. organization={organization}
  268. location={router.location}
  269. router={router}
  270. />,
  271. routerContext
  272. );
  273. await tick();
  274. wrapper.update();
  275. expect(
  276. wrapper.find('Button[data-test-id="reset-view"]').prop('disabled')
  277. ).toBeFalsy();
  278. });
  279. it('resets view properly', async function () {
  280. const {organization, router, routerContext} = initialize({
  281. query: {
  282. fidStart: '20',
  283. lcpEnd: '20',
  284. },
  285. });
  286. const wrapper = mountWithTheme(
  287. <WrappedComponent
  288. organization={organization}
  289. location={router.location}
  290. router={router}
  291. />,
  292. routerContext
  293. );
  294. await tick();
  295. wrapper.update();
  296. wrapper.find('Button[data-test-id="reset-view"]').simulate('click');
  297. expect(browserHistory.push).toHaveBeenCalledWith({
  298. query: expect.not.objectContaining(
  299. ZOOM_KEYS.reduce((obj, key) => {
  300. obj[key] = expect.anything();
  301. return obj;
  302. }, {})
  303. ),
  304. });
  305. });
  306. it('renders an info alert when missing web vitals data', async function () {
  307. MockApiClient.addMockResponse({
  308. url: '/organizations/org-slug/events-vitals/',
  309. body: {
  310. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  311. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  312. },
  313. });
  314. const {organization, router, routerContext} = initialize({
  315. query: {
  316. lcpStart: '20',
  317. },
  318. });
  319. const wrapper = mountWithTheme(
  320. <WrappedComponent
  321. organization={organization}
  322. location={router.location}
  323. router={router}
  324. />,
  325. routerContext
  326. );
  327. await tick();
  328. wrapper.update();
  329. expect(wrapper.find('Alert')).toHaveLength(1);
  330. });
  331. it('does not render an info alert when data from all web vitals is present', async function () {
  332. const {organization, router, routerContext} = initialize({
  333. query: {
  334. lcpStart: '20',
  335. },
  336. });
  337. const wrapper = mountWithTheme(
  338. <WrappedComponent
  339. organization={organization}
  340. location={router.location}
  341. router={router}
  342. />,
  343. routerContext
  344. );
  345. await tick();
  346. wrapper.update();
  347. expect(wrapper.find('Alert')).toHaveLength(0);
  348. });
  349. });
  350. it('renders an info alert when some web vitals measurements has no data available', async function () {
  351. MockApiClient.addMockResponse({
  352. url: '/organizations/org-slug/events-vitals/',
  353. body: {
  354. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  355. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  356. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  357. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  358. 'measurements.lcp': {poor: 0, meh: 0, good: 0, total: 0, p75: null},
  359. },
  360. });
  361. const {organization, router, routerContext} = initialize({
  362. query: {
  363. lcpStart: '20',
  364. },
  365. });
  366. const wrapper = mountWithTheme(
  367. <WrappedComponent
  368. organization={organization}
  369. location={router.location}
  370. router={router}
  371. />,
  372. routerContext
  373. );
  374. await tick();
  375. wrapper.update();
  376. expect(wrapper.find('Alert')).toHaveLength(1);
  377. });
  378. });