index.spec.tsx 15 KB

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