index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import type {InjectedRouter} from 'react-router';
  2. import {MetricsFieldFixture} from 'sentry-fixture/metrics';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  6. import {textWithMarkupMatcher} from 'sentry-test/utils';
  7. import ProjectsStore from 'sentry/stores/projectsStore';
  8. import TeamStore from 'sentry/stores/teamStore';
  9. import {browserHistory} from 'sentry/utils/browserHistory';
  10. import {WebVital} from 'sentry/utils/fields';
  11. import {Browser} from 'sentry/utils/performance/vitals/constants';
  12. import {DEFAULT_STATS_PERIOD} from 'sentry/views/performance/data';
  13. import VitalDetail from 'sentry/views/performance/vitalDetail';
  14. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  15. const api = new MockApiClient();
  16. const organization = OrganizationFixture({
  17. features: ['discover-basic', 'performance-view'],
  18. });
  19. const {
  20. organization: org,
  21. project,
  22. router,
  23. } = initializeOrg({
  24. organization,
  25. router: {
  26. location: {
  27. query: {
  28. project: '1',
  29. },
  30. },
  31. },
  32. });
  33. function TestComponent(props: {router?: InjectedRouter} = {}) {
  34. return (
  35. <VitalDetail
  36. api={api}
  37. location={props.router?.location ?? router.location}
  38. router={props.router ?? router}
  39. params={{}}
  40. route={{}}
  41. routes={[]}
  42. routeParams={{}}
  43. />
  44. );
  45. }
  46. const testSupportedBrowserRendering = (webVital: WebVital) => {
  47. Object.values(Browser).forEach(browser => {
  48. const browserElement = screen.getByText(browser);
  49. expect(browserElement).toBeInTheDocument();
  50. const isSupported = vitalSupportedBrowsers[webVital]?.includes(browser);
  51. if (isSupported) {
  52. expect(within(browserElement).getByTestId('icon-check-mark')).toBeInTheDocument();
  53. } else {
  54. expect(within(browserElement).getByTestId('icon-close')).toBeInTheDocument();
  55. }
  56. });
  57. };
  58. describe('Performance > VitalDetail', function () {
  59. beforeEach(function () {
  60. TeamStore.loadInitialData([], false, null);
  61. ProjectsStore.loadInitialData([project]);
  62. browserHistory.push = jest.fn();
  63. MockApiClient.addMockResponse({
  64. url: `/organizations/${organization.slug}/projects/`,
  65. body: [],
  66. });
  67. MockApiClient.addMockResponse({
  68. url: `/organizations/${organization.slug}/tags/`,
  69. body: [],
  70. });
  71. MockApiClient.addMockResponse({
  72. url: `/organizations/${organization.slug}/events-stats/`,
  73. body: {data: [[123, []]]},
  74. });
  75. MockApiClient.addMockResponse({
  76. url: `/organizations/${organization.slug}/tags/user.email/values/`,
  77. body: [],
  78. });
  79. MockApiClient.addMockResponse({
  80. url: `/organizations/${organization.slug}/releases/stats/`,
  81. body: [],
  82. });
  83. MockApiClient.addMockResponse({
  84. url: `/organizations/${organization.slug}/users/`,
  85. body: [],
  86. });
  87. MockApiClient.addMockResponse({
  88. url: `/organizations/${organization.slug}/recent-searches/`,
  89. body: [],
  90. });
  91. MockApiClient.addMockResponse({
  92. url: `/organizations/${organization.slug}/recent-searches/`,
  93. method: 'POST',
  94. body: [],
  95. });
  96. MockApiClient.addMockResponse({
  97. url: `/organizations/${organization.slug}/events-vitals/`,
  98. body: {
  99. 'measurements.lcp': {
  100. poor: 1,
  101. meh: 2,
  102. good: 3,
  103. total: 6,
  104. p75: 4500,
  105. },
  106. },
  107. });
  108. MockApiClient.addMockResponse({
  109. url: `/organizations/${organization.slug}/events/`,
  110. body: {
  111. meta: {
  112. fields: {
  113. 'count()': 'integer',
  114. 'p95(measurements.lcp)': 'duration',
  115. transaction: 'string',
  116. 'p50(measurements.lcp)': 'duration',
  117. project: 'string',
  118. 'compare_numeric_aggregate(p75_measurements_lcp,greater,4000)': 'number',
  119. 'project.id': 'integer',
  120. 'count_unique_user()': 'integer',
  121. 'p75(measurements.lcp)': 'duration',
  122. },
  123. },
  124. data: [
  125. {
  126. 'count()': 100000,
  127. 'p95(measurements.lcp)': 5000,
  128. transaction: 'something',
  129. 'p50(measurements.lcp)': 3500,
  130. project: 'javascript',
  131. 'compare_numeric_aggregate(p75_measurements_lcp,greater,4000)': 1,
  132. 'count_unique_user()': 10000,
  133. 'p75(measurements.lcp)': 4500,
  134. },
  135. ],
  136. },
  137. match: [
  138. (_url, options) => {
  139. return options.query?.field?.find(f => f === 'p50(measurements.lcp)');
  140. },
  141. ],
  142. });
  143. MockApiClient.addMockResponse({
  144. url: `/organizations/${organization.slug}/events/`,
  145. body: {
  146. meta: {
  147. fields: {
  148. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.1)': 'number',
  149. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.25)': 'number',
  150. 'count()': 'integer',
  151. 'count_unique_user()': 'integer',
  152. team_key_transaction: 'boolean',
  153. 'p50(measurements.cls)': 'number',
  154. 'p75(measurements.cls)': 'number',
  155. 'p95(measurements.cls)': 'number',
  156. project: 'string',
  157. transaction: 'string',
  158. },
  159. },
  160. data: [
  161. {
  162. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.1)': 1,
  163. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.25)': 0,
  164. 'count()': 10000,
  165. 'count_unique_user()': 2740,
  166. team_key_transaction: 1,
  167. 'p50(measurements.cls)': 0.143,
  168. 'p75(measurements.cls)': 0.215,
  169. 'p95(measurements.cls)': 0.302,
  170. project: 'javascript',
  171. transaction: 'something',
  172. },
  173. ],
  174. },
  175. match: [
  176. (_url, options) => {
  177. return options.query?.field?.find(f => f === 'p50(measurements.cls)');
  178. },
  179. ],
  180. });
  181. MockApiClient.addMockResponse({
  182. method: 'GET',
  183. url: `/organizations/${organization.slug}/key-transactions-list/`,
  184. body: [],
  185. });
  186. // Metrics Requests
  187. MockApiClient.addMockResponse({
  188. method: 'GET',
  189. url: `/organizations/${organization.slug}/metrics/tags/`,
  190. body: [],
  191. });
  192. MockApiClient.addMockResponse({
  193. method: 'GET',
  194. url: `/organizations/${organization.slug}/metrics/data/`,
  195. body: MetricsFieldFixture('p75(sentry.transactions.measurements.lcp)'),
  196. match: [
  197. MockApiClient.matchQuery({
  198. field: ['p75(sentry.transactions.measurements.lcp)'],
  199. }),
  200. ],
  201. });
  202. });
  203. afterEach(function () {
  204. MockApiClient.clearMockResponses();
  205. ProjectsStore.reset();
  206. });
  207. it('renders basic UI elements', async function () {
  208. render(<TestComponent />, {
  209. router,
  210. organization: org,
  211. });
  212. // It shows a search bar
  213. expect(await screen.findByLabelText('Search events')).toBeInTheDocument();
  214. // It shows the vital card
  215. expect(
  216. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  217. ).toBeInTheDocument();
  218. expect(screen.getByText('Good 50%', {exact: false})).toBeInTheDocument();
  219. expect(screen.getByText('Meh 33%', {exact: false})).toBeInTheDocument();
  220. expect(screen.getByText('Poor 17%', {exact: false})).toBeInTheDocument();
  221. // It shows a chart
  222. expect(screen.getByText('Duration p75')).toBeInTheDocument();
  223. // It shows a table
  224. expect(screen.getByText('something').closest('td')).toBeInTheDocument();
  225. });
  226. it('triggers a navigation on search', async function () {
  227. render(<TestComponent />, {
  228. router,
  229. organization: org,
  230. });
  231. // Fill out the search box, and submit it.
  232. await userEvent.click(await screen.findByLabelText('Search events'));
  233. await userEvent.paste('user.email:uhoh*');
  234. await userEvent.keyboard('{enter}');
  235. // Check the navigation.
  236. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  237. expect(browserHistory.push).toHaveBeenCalledWith({
  238. pathname: undefined,
  239. query: {
  240. project: '1',
  241. statsPeriod: '14d',
  242. query: 'user.email:uhoh*',
  243. },
  244. });
  245. });
  246. it('applies conditions when linking to transaction summary', async function () {
  247. const newRouter = {
  248. ...router,
  249. location: {
  250. ...router.location,
  251. query: {
  252. query: 'sometag:value',
  253. },
  254. },
  255. };
  256. render(<TestComponent router={newRouter} />, {
  257. router: newRouter,
  258. organization: org,
  259. });
  260. expect(
  261. await screen.findByRole('heading', {name: 'Largest Contentful Paint'})
  262. ).toBeInTheDocument();
  263. await userEvent.click(
  264. screen.getByLabelText('See transaction summary of the transaction something')
  265. );
  266. expect(newRouter.push).toHaveBeenCalledWith({
  267. pathname: `/organizations/${organization.slug}/performance/summary/`,
  268. query: {
  269. transaction: 'something',
  270. project: undefined,
  271. environment: undefined,
  272. statsPeriod: DEFAULT_STATS_PERIOD,
  273. start: undefined,
  274. end: undefined,
  275. query: 'sometag:value has:measurements.lcp',
  276. referrer: 'performance-transaction-summary',
  277. unselectedSeries: ['p100()', 'avg()'],
  278. showTransactions: 'recent',
  279. display: 'vitals',
  280. trendFunction: undefined,
  281. trendColumn: undefined,
  282. },
  283. });
  284. });
  285. it('check CLS', async function () {
  286. const newRouter = {
  287. ...router,
  288. location: {
  289. ...router.location,
  290. query: {
  291. query: 'anothertag:value',
  292. vitalName: 'measurements.cls',
  293. },
  294. },
  295. };
  296. render(<TestComponent router={newRouter} />, {
  297. router: newRouter,
  298. organization: org,
  299. });
  300. expect(await screen.findByText('Cumulative Layout Shift')).toBeInTheDocument();
  301. await userEvent.click(
  302. screen.getByLabelText('See transaction summary of the transaction something')
  303. );
  304. expect(newRouter.push).toHaveBeenCalledWith({
  305. pathname: `/organizations/${organization.slug}/performance/summary/`,
  306. query: {
  307. transaction: 'something',
  308. project: undefined,
  309. environment: undefined,
  310. statsPeriod: DEFAULT_STATS_PERIOD,
  311. start: undefined,
  312. end: undefined,
  313. query: 'anothertag:value has:measurements.cls',
  314. referrer: 'performance-transaction-summary',
  315. unselectedSeries: ['p100()', 'avg()'],
  316. showTransactions: 'recent',
  317. display: 'vitals',
  318. trendFunction: undefined,
  319. trendColumn: undefined,
  320. },
  321. });
  322. // Check cells are not in ms
  323. expect(screen.getByText('0.215').closest('td')).toBeInTheDocument();
  324. });
  325. it('can switch vitals with dropdown menu', async function () {
  326. const newRouter = {
  327. ...router,
  328. location: {
  329. ...router.location,
  330. query: {
  331. project: 1,
  332. query: 'tag:value',
  333. },
  334. },
  335. };
  336. render(<TestComponent router={newRouter} />, {
  337. router: newRouter,
  338. organization: org,
  339. });
  340. const button = screen.getByRole('button', {name: /web vitals: lcp/i});
  341. expect(button).toBeInTheDocument();
  342. await userEvent.click(button);
  343. const menuItem = screen.getByRole('menuitemradio', {name: /fcp/i});
  344. expect(menuItem).toBeInTheDocument();
  345. await userEvent.click(menuItem);
  346. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  347. expect(browserHistory.push).toHaveBeenCalledWith({
  348. pathname: undefined,
  349. query: {
  350. project: 1,
  351. query: 'tag:value',
  352. vitalName: 'measurements.fcp',
  353. },
  354. });
  355. });
  356. it('renders LCP vital correctly', async function () {
  357. render(<TestComponent />, {
  358. router,
  359. organization: org,
  360. });
  361. expect(await screen.findByText('Largest Contentful Paint')).toBeInTheDocument();
  362. expect(
  363. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  364. ).toBeInTheDocument();
  365. expect(screen.getByText('4.50s').closest('td')).toBeInTheDocument();
  366. });
  367. it('correctly renders which browsers support LCP', async function () {
  368. render(<TestComponent />, {
  369. router,
  370. organization: org,
  371. });
  372. expect(await screen.findAllByText(/Largest Contentful Paint/)).toHaveLength(2);
  373. testSupportedBrowserRendering(WebVital.LCP);
  374. });
  375. it('correctly renders which browsers support CLS', async function () {
  376. const newRouter = {
  377. ...router,
  378. location: {
  379. ...router.location,
  380. query: {
  381. vitalName: 'measurements.cls',
  382. },
  383. },
  384. };
  385. render(<TestComponent router={newRouter} />, {
  386. router,
  387. organization: org,
  388. });
  389. expect(await screen.findAllByText(/Cumulative Layout Shift/)).toHaveLength(2);
  390. testSupportedBrowserRendering(WebVital.CLS);
  391. });
  392. it('correctly renders which browsers support FCP', async function () {
  393. const newRouter = {
  394. ...router,
  395. location: {
  396. ...router.location,
  397. query: {
  398. vitalName: 'measurements.fcp',
  399. },
  400. },
  401. };
  402. MockApiClient.addMockResponse({
  403. url: `/organizations/${organization.slug}/events/`,
  404. body: [],
  405. });
  406. render(<TestComponent router={newRouter} />, {
  407. router,
  408. organization: org,
  409. });
  410. expect(await screen.findAllByText(/First Contentful Paint/)).toHaveLength(2);
  411. testSupportedBrowserRendering(WebVital.FCP);
  412. });
  413. it('correctly renders which browsers support FID', async function () {
  414. const newRouter = {
  415. ...router,
  416. location: {
  417. ...router.location,
  418. query: {
  419. vitalName: 'measurements.fid',
  420. },
  421. },
  422. };
  423. MockApiClient.addMockResponse({
  424. url: `/organizations/${organization.slug}/events/`,
  425. body: [],
  426. });
  427. render(<TestComponent router={newRouter} />, {
  428. router,
  429. organization: org,
  430. });
  431. expect(await screen.findAllByText(/First Input Delay/)).toHaveLength(2);
  432. testSupportedBrowserRendering(WebVital.FID);
  433. });
  434. });