index.spec.jsx 11 KB

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