index.spec.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. function 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. // eslint-disable-next-line no-console
  80. jest.spyOn(console, 'error').mockImplementation(jest.fn());
  81. MockApiClient.addMockResponse({
  82. url: '/organizations/org-slug/projects/',
  83. body: [],
  84. });
  85. MockApiClient.addMockResponse({
  86. url: '/organizations/org-slug/events-has-measurements/',
  87. body: {measurements: true},
  88. });
  89. MockApiClient.addMockResponse({
  90. url: '/organizations/org-slug/project-transaction-threshold-override/',
  91. method: 'GET',
  92. body: {
  93. threshold: '800',
  94. metric: 'lcp',
  95. },
  96. });
  97. // Mock baseline measurements
  98. MockApiClient.addMockResponse({
  99. url: '/organizations/org-slug/events-vitals/',
  100. body: {
  101. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  102. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  103. 'measurements.lcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1342},
  104. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 987},
  105. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 0.02},
  106. },
  107. });
  108. const histogramData = {};
  109. const webVitals = VITAL_GROUPS.reduce((vs, group) => vs.concat(group.vitals), []);
  110. for (const measurement of webVitals) {
  111. const data = [];
  112. for (let i = 0; i < 100; i++) {
  113. data.push({
  114. histogram: i,
  115. count: i,
  116. });
  117. }
  118. histogramData[`measurements.${measurement}`] = data;
  119. }
  120. MockApiClient.addMockResponse({
  121. url: '/organizations/org-slug/events-histogram/',
  122. body: histogramData,
  123. });
  124. MockApiClient.addMockResponse({
  125. method: 'GET',
  126. url: `/organizations/org-slug/key-transactions-list/`,
  127. body: [],
  128. });
  129. MockApiClient.addMockResponse({
  130. url: '/prompts-activity/',
  131. body: {},
  132. });
  133. MockApiClient.addMockResponse({
  134. url: '/organizations/org-slug/sdk-updates/',
  135. body: [],
  136. });
  137. });
  138. afterEach(() => {
  139. jest.restoreAllMocks();
  140. });
  141. it('render no access without feature', function () {
  142. const {organization, router, routerContext} = initialize({
  143. features: [],
  144. });
  145. render(<WrappedComponent organization={organization} location={router.location} />, {
  146. context: routerContext,
  147. });
  148. expect(screen.getByText("You don't have access to this feature")).toBeInTheDocument();
  149. });
  150. it('renders the basic UI components', function () {
  151. const {organization, router, routerContext} = initialize({
  152. transaction: '/organizations/:orgId/',
  153. });
  154. render(
  155. <WrappedComponent
  156. organization={organization}
  157. location={router.location}
  158. router={router}
  159. />,
  160. {context: routerContext}
  161. );
  162. expect(
  163. screen.getByRole('heading', {name: '/organizations/:orgId/'})
  164. ).toBeInTheDocument();
  165. ['navigation', 'main'].forEach(role => {
  166. expect(screen.getByRole(role)).toBeInTheDocument();
  167. });
  168. });
  169. it('renders the correct bread crumbs', function () {
  170. const {organization, router, routerContext} = initialize();
  171. render(
  172. <WrappedComponent
  173. organization={organization}
  174. location={router.location}
  175. router={router}
  176. />,
  177. {context: routerContext}
  178. );
  179. expect(screen.getByRole('navigation')).toHaveTextContent('PerformanceWeb Vitals');
  180. });
  181. describe('renders all vitals cards correctly', function () {
  182. const {organization, router, routerContext} = initialize();
  183. beforeEach(() => {
  184. render(
  185. <WrappedComponent
  186. organization={organization}
  187. location={router.location}
  188. router={router}
  189. />,
  190. {context: routerContext}
  191. );
  192. });
  193. it.each(vitals)('Renders %s', function (vital) {
  194. expect(screen.getByText(vital.heading)).toBeInTheDocument();
  195. expect(screen.getByText(vital.baseline)).toBeInTheDocument();
  196. });
  197. });
  198. describe('reset view', function () {
  199. it('disables button on default view', function () {
  200. const {organization, router, routerContext} = initialize();
  201. render(
  202. <WrappedComponent
  203. organization={organization}
  204. location={router.location}
  205. router={router}
  206. />,
  207. {context: routerContext}
  208. );
  209. expect(screen.getByRole('button', {name: 'Reset View'})).toBeDisabled();
  210. });
  211. it('enables button on left zoom', function () {
  212. const {organization, router, routerContext} = initialize({
  213. query: {
  214. lcpStart: '20',
  215. },
  216. });
  217. render(
  218. <WrappedComponent
  219. organization={organization}
  220. location={router.location}
  221. router={router}
  222. />,
  223. {context: routerContext}
  224. );
  225. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  226. });
  227. it('enables button on right zoom', function () {
  228. const {organization, router, routerContext} = initialize({
  229. query: {
  230. fpEnd: '20',
  231. },
  232. });
  233. render(
  234. <WrappedComponent
  235. organization={organization}
  236. location={router.location}
  237. router={router}
  238. />,
  239. {context: routerContext}
  240. );
  241. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  242. });
  243. it('enables button on left and right zoom', function () {
  244. const {organization, router, routerContext} = initialize({
  245. query: {
  246. fcpStart: '20',
  247. fcpEnd: '20',
  248. },
  249. });
  250. render(
  251. <WrappedComponent
  252. organization={organization}
  253. location={router.location}
  254. router={router}
  255. />,
  256. {context: routerContext}
  257. );
  258. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  259. });
  260. it('resets view properly', async function () {
  261. const {organization, router, routerContext} = initialize({
  262. query: {
  263. fidStart: '20',
  264. lcpEnd: '20',
  265. },
  266. });
  267. render(
  268. <WrappedComponent
  269. organization={organization}
  270. location={router.location}
  271. router={router}
  272. />,
  273. {context: routerContext}
  274. );
  275. await userEvent.click(screen.getByRole('button', {name: 'Reset View'}));
  276. expect(browserHistory.push).toHaveBeenCalledWith({
  277. query: expect.not.objectContaining(
  278. ZOOM_KEYS.reduce((obj, key) => {
  279. obj[key] = expect.anything();
  280. return obj;
  281. }, {})
  282. ),
  283. });
  284. });
  285. it('renders an info alert when missing web vitals data', async function () {
  286. MockApiClient.addMockResponse({
  287. url: '/organizations/org-slug/events-vitals/',
  288. body: {
  289. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  290. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  291. },
  292. });
  293. const {organization, router, routerContext} = initialize({
  294. query: {
  295. lcpStart: '20',
  296. },
  297. });
  298. render(
  299. <WrappedComponent
  300. organization={organization}
  301. location={router.location}
  302. router={router}
  303. />,
  304. {context: routerContext}
  305. );
  306. await waitForElementToBeRemoved(() =>
  307. screen.queryAllByTestId('loading-placeholder')
  308. );
  309. expect(
  310. screen.getByText(
  311. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  312. )
  313. ).toBeInTheDocument();
  314. });
  315. it('does not render an info alert when data from all web vitals is present', async function () {
  316. const {organization, router, routerContext} = initialize({
  317. query: {
  318. lcpStart: '20',
  319. },
  320. });
  321. render(
  322. <WrappedComponent
  323. organization={organization}
  324. location={router.location}
  325. router={router}
  326. />,
  327. {context: routerContext}
  328. );
  329. await waitForElementToBeRemoved(() =>
  330. screen.queryAllByTestId('loading-placeholder')
  331. );
  332. expect(
  333. screen.queryByText(
  334. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  335. )
  336. ).not.toBeInTheDocument();
  337. });
  338. });
  339. it('renders an info alert when some web vitals measurements has no data available', async function () {
  340. MockApiClient.addMockResponse({
  341. url: '/organizations/org-slug/events-vitals/',
  342. body: {
  343. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  344. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  345. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  346. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  347. 'measurements.lcp': {poor: 0, meh: 0, good: 0, total: 0, p75: null},
  348. },
  349. });
  350. const {organization, router, routerContext} = initialize({
  351. query: {
  352. lcpStart: '20',
  353. },
  354. });
  355. render(
  356. <WrappedComponent
  357. organization={organization}
  358. location={router.location}
  359. router={router}
  360. />,
  361. {context: routerContext}
  362. );
  363. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-placeholder'));
  364. expect(
  365. screen.getByText(
  366. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  367. )
  368. ).toBeInTheDocument();
  369. });
  370. });