index.spec.tsx 11 KB

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