index.spec.tsx 11 KB

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