index.spec.tsx 11 KB

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