index.spec.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. jest.restoreAllMocks();
  141. });
  142. it('render no access without feature', function () {
  143. const {organization, router, routerContext} = initialize({
  144. features: [],
  145. });
  146. render(<WrappedComponent organization={organization} location={router.location} />, {
  147. context: routerContext,
  148. });
  149. expect(screen.getByText("You don't have access to this feature")).toBeInTheDocument();
  150. });
  151. it('renders the basic UI components', function () {
  152. const {organization, router, routerContext} = initialize({
  153. transaction: '/organizations/:orgId/',
  154. });
  155. render(
  156. <WrappedComponent
  157. organization={organization}
  158. location={router.location}
  159. router={router}
  160. />,
  161. {context: routerContext}
  162. );
  163. expect(
  164. screen.getByRole('heading', {name: '/organizations/:orgId/'})
  165. ).toBeInTheDocument();
  166. ['navigation', 'main'].forEach(role => {
  167. expect(screen.getByRole(role)).toBeInTheDocument();
  168. });
  169. });
  170. it('renders the correct bread crumbs', function () {
  171. const {organization, router, routerContext} = initialize();
  172. render(
  173. <WrappedComponent
  174. organization={organization}
  175. location={router.location}
  176. router={router}
  177. />,
  178. {context: routerContext}
  179. );
  180. expect(screen.getByRole('navigation')).toHaveTextContent('PerformanceWeb Vitals');
  181. });
  182. describe('renders all vitals cards correctly', function () {
  183. const {organization, router, routerContext} = initialize();
  184. beforeEach(() => {
  185. render(
  186. <WrappedComponent
  187. organization={organization}
  188. location={router.location}
  189. router={router}
  190. />,
  191. {context: routerContext}
  192. );
  193. });
  194. it.each(vitals)('Renders %s', function (vital) {
  195. expect(screen.getByText(vital.heading)).toBeInTheDocument();
  196. expect(screen.getByText(vital.baseline)).toBeInTheDocument();
  197. });
  198. });
  199. describe('reset view', function () {
  200. it('disables button on default view', function () {
  201. const {organization, router, routerContext} = initialize();
  202. render(
  203. <WrappedComponent
  204. organization={organization}
  205. location={router.location}
  206. router={router}
  207. />,
  208. {context: routerContext}
  209. );
  210. expect(screen.getByRole('button', {name: 'Reset View'})).toBeDisabled();
  211. });
  212. it('enables button on left zoom', function () {
  213. const {organization, router, routerContext} = initialize({
  214. query: {
  215. lcpStart: '20',
  216. },
  217. });
  218. render(
  219. <WrappedComponent
  220. organization={organization}
  221. location={router.location}
  222. router={router}
  223. />,
  224. {context: routerContext}
  225. );
  226. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  227. });
  228. it('enables button on right zoom', function () {
  229. const {organization, router, routerContext} = initialize({
  230. query: {
  231. fpEnd: '20',
  232. },
  233. });
  234. render(
  235. <WrappedComponent
  236. organization={organization}
  237. location={router.location}
  238. router={router}
  239. />,
  240. {context: routerContext}
  241. );
  242. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  243. });
  244. it('enables button on left and right zoom', function () {
  245. const {organization, router, routerContext} = initialize({
  246. query: {
  247. fcpStart: '20',
  248. fcpEnd: '20',
  249. },
  250. });
  251. render(
  252. <WrappedComponent
  253. organization={organization}
  254. location={router.location}
  255. router={router}
  256. />,
  257. {context: routerContext}
  258. );
  259. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  260. });
  261. it('resets view properly', function () {
  262. const {organization, router, routerContext} = initialize({
  263. query: {
  264. fidStart: '20',
  265. lcpEnd: '20',
  266. },
  267. });
  268. render(
  269. <WrappedComponent
  270. organization={organization}
  271. location={router.location}
  272. router={router}
  273. />,
  274. {context: routerContext}
  275. );
  276. userEvent.click(screen.getByRole('button', {name: 'Reset View'}));
  277. expect(browserHistory.push).toHaveBeenCalledWith({
  278. query: expect.not.objectContaining(
  279. ZOOM_KEYS.reduce((obj, key) => {
  280. obj[key] = expect.anything();
  281. return obj;
  282. }, {})
  283. ),
  284. });
  285. });
  286. it('renders an info alert when missing web vitals data', async function () {
  287. MockApiClient.addMockResponse({
  288. url: '/organizations/org-slug/events-vitals/',
  289. body: {
  290. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  291. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  292. },
  293. });
  294. const {organization, router, routerContext} = initialize({
  295. query: {
  296. lcpStart: '20',
  297. },
  298. });
  299. render(
  300. <WrappedComponent
  301. organization={organization}
  302. location={router.location}
  303. router={router}
  304. />,
  305. {context: routerContext}
  306. );
  307. await waitForElementToBeRemoved(() =>
  308. screen.queryAllByTestId('loading-placeholder')
  309. );
  310. expect(
  311. screen.getByText(
  312. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  313. )
  314. ).toBeInTheDocument();
  315. });
  316. it('does not render an info alert when data from all web vitals is present', async function () {
  317. const {organization, router, routerContext} = initialize({
  318. query: {
  319. lcpStart: '20',
  320. },
  321. });
  322. render(
  323. <WrappedComponent
  324. organization={organization}
  325. location={router.location}
  326. router={router}
  327. />,
  328. {context: routerContext}
  329. );
  330. await waitForElementToBeRemoved(() =>
  331. screen.queryAllByTestId('loading-placeholder')
  332. );
  333. expect(
  334. screen.queryByText(
  335. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  336. )
  337. ).not.toBeInTheDocument();
  338. });
  339. });
  340. it('renders an info alert when some web vitals measurements has no data available', async function () {
  341. MockApiClient.addMockResponse({
  342. url: '/organizations/org-slug/events-vitals/',
  343. body: {
  344. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  345. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  346. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  347. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  348. 'measurements.lcp': {poor: 0, meh: 0, good: 0, total: 0, p75: null},
  349. },
  350. });
  351. const {organization, router, routerContext} = initialize({
  352. query: {
  353. lcpStart: '20',
  354. },
  355. });
  356. render(
  357. <WrappedComponent
  358. organization={organization}
  359. location={router.location}
  360. router={router}
  361. />,
  362. {context: routerContext}
  363. );
  364. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-placeholder'));
  365. expect(
  366. screen.getByText(
  367. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  368. )
  369. ).toBeInTheDocument();
  370. });
  371. });