index.spec.tsx 11 KB

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