index.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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(screen.getByText('/organizations/:orgId/')).toBeInTheDocument();
  183. ['navigation', 'main'].forEach(role => {
  184. expect(screen.getByRole(role)).toBeInTheDocument();
  185. });
  186. });
  187. it('renders the correct bread crumbs', function () {
  188. const {organization, router} = initialize();
  189. render(<TransactionVitals organization={organization} location={router.location} />, {
  190. router,
  191. organization,
  192. });
  193. expect(screen.getByRole('navigation')).toHaveTextContent('PerformanceWeb Vitals');
  194. });
  195. describe('renders all vitals cards correctly', function () {
  196. const {organization, router} = initialize();
  197. beforeEach(() => {
  198. render(
  199. <TransactionVitals organization={organization} location={router.location} />,
  200. {router, organization}
  201. );
  202. });
  203. it.each(vitals)('Renders %s', async function (vital) {
  204. expect(await screen.findByText(vital.heading)).toBeInTheDocument();
  205. expect(await screen.findByText(vital.baseline)).toBeInTheDocument();
  206. });
  207. });
  208. describe('reset view', function () {
  209. it('disables button on default view', function () {
  210. const {organization, router} = initialize();
  211. render(
  212. <TransactionVitals organization={organization} location={router.location} />,
  213. {router, organization}
  214. );
  215. expect(screen.getByRole('button', {name: 'Reset View'})).toBeDisabled();
  216. });
  217. it('enables button on left zoom', function () {
  218. const {organization, router} = initialize({
  219. query: {
  220. lcpStart: '20',
  221. },
  222. });
  223. render(
  224. <TransactionVitals organization={organization} location={router.location} />,
  225. {router, organization}
  226. );
  227. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  228. });
  229. it('enables button on right zoom', function () {
  230. const {organization, router} = initialize({
  231. query: {
  232. fpEnd: '20',
  233. },
  234. });
  235. render(
  236. <TransactionVitals organization={organization} location={router.location} />,
  237. {router, organization}
  238. );
  239. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  240. });
  241. it('enables button on left and right zoom', function () {
  242. const {organization, router} = initialize({
  243. query: {
  244. fcpStart: '20',
  245. fcpEnd: '20',
  246. },
  247. });
  248. render(
  249. <TransactionVitals organization={organization} location={router.location} />,
  250. {router, organization}
  251. );
  252. expect(screen.getByRole('button', {name: 'Reset View'})).toBeEnabled();
  253. });
  254. it('resets view properly', async function () {
  255. const {organization, router} = initialize({
  256. query: {
  257. fidStart: '20',
  258. lcpEnd: '20',
  259. },
  260. });
  261. render(
  262. <TransactionVitals organization={organization} location={router.location} />,
  263. {router, organization}
  264. );
  265. await userEvent.click(screen.getByRole('button', {name: 'Reset View'}));
  266. expect(browserHistory.push).toHaveBeenCalledWith({
  267. query: expect.not.objectContaining(
  268. ZOOM_KEYS.reduce((obj, key) => {
  269. obj[key] = expect.anything();
  270. return obj;
  271. }, {})
  272. ),
  273. });
  274. });
  275. it('renders an info alert when missing web vitals data', async function () {
  276. MockApiClient.addMockResponse({
  277. url: '/organizations/org-slug/events-vitals/',
  278. body: {
  279. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  280. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  281. },
  282. });
  283. const {organization, router} = initialize({
  284. query: {
  285. lcpStart: '20',
  286. },
  287. });
  288. render(
  289. <TransactionVitals organization={organization} location={router.location} />,
  290. {router, organization}
  291. );
  292. await waitForElementToBeRemoved(() =>
  293. screen.queryAllByTestId('loading-placeholder')
  294. );
  295. expect(
  296. screen.getByText(
  297. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  298. )
  299. ).toBeInTheDocument();
  300. });
  301. it('does not render an info alert when data from all web vitals is present', async function () {
  302. const {organization, router} = initialize({
  303. query: {
  304. lcpStart: '20',
  305. },
  306. });
  307. render(
  308. <TransactionVitals organization={organization} location={router.location} />,
  309. {router, organization}
  310. );
  311. await waitForElementToBeRemoved(() =>
  312. screen.queryAllByTestId('loading-placeholder')
  313. );
  314. expect(
  315. screen.queryByText(
  316. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  317. )
  318. ).not.toBeInTheDocument();
  319. });
  320. });
  321. it('renders an info alert when some web vitals measurements has no data available', async function () {
  322. MockApiClient.addMockResponse({
  323. url: '/organizations/org-slug/events-vitals/',
  324. body: {
  325. 'measurements.cls': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  326. 'measurements.fcp': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  327. 'measurements.fid': {poor: 1, meh: 2, good: 3, total: 6, p75: 4567},
  328. 'measurements.fp': {poor: 1, meh: 2, good: 3, total: 6, p75: 1456},
  329. 'measurements.lcp': {poor: 0, meh: 0, good: 0, total: 0, p75: null},
  330. },
  331. });
  332. const {organization, router} = initialize({
  333. query: {
  334. lcpStart: '20',
  335. },
  336. });
  337. render(<TransactionVitals organization={organization} location={router.location} />, {
  338. router,
  339. organization,
  340. });
  341. await waitForElementToBeRemoved(() => screen.queryAllByTestId('loading-placeholder'));
  342. expect(
  343. screen.getByText(
  344. 'If this page is looking a little bare, keep in mind not all browsers support these vitals.'
  345. )
  346. ).toBeInTheDocument();
  347. });
  348. });