index.spec.tsx 11 KB

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