index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/org-slug/projects/',
  64. body: [],
  65. });
  66. MockApiClient.addMockResponse({
  67. url: '/organizations/org-slug/tags/',
  68. body: [],
  69. });
  70. MockApiClient.addMockResponse({
  71. url: '/organizations/org-slug/events-stats/',
  72. body: {data: [[123, []]]},
  73. });
  74. MockApiClient.addMockResponse({
  75. url: '/organizations/org-slug/tags/user.email/values/',
  76. body: [],
  77. });
  78. MockApiClient.addMockResponse({
  79. url: '/organizations/org-slug/releases/stats/',
  80. body: [],
  81. });
  82. MockApiClient.addMockResponse({
  83. url: '/organizations/org-slug/users/',
  84. body: [],
  85. });
  86. MockApiClient.addMockResponse({
  87. url: '/organizations/org-slug/recent-searches/',
  88. body: [],
  89. });
  90. MockApiClient.addMockResponse({
  91. url: '/organizations/org-slug/recent-searches/',
  92. method: 'POST',
  93. body: [],
  94. });
  95. MockApiClient.addMockResponse({
  96. url: '/organizations/org-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/org-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/org-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/org-slug/key-transactions-list/`,
  183. body: [],
  184. });
  185. // Metrics Requests
  186. MockApiClient.addMockResponse({
  187. method: 'GET',
  188. url: `/organizations/org-slug/metrics/tags/`,
  189. body: [],
  190. });
  191. MockApiClient.addMockResponse({
  192. method: 'GET',
  193. url: `/organizations/org-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/org-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/org-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/org-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/org-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. unselectedSeries: 'p100()',
  325. showTransactions: 'recent',
  326. display: 'vitals',
  327. trendFunction: undefined,
  328. trendColumn: undefined,
  329. },
  330. });
  331. });
  332. it('check CLS', async function () {
  333. const newRouter = {
  334. ...router,
  335. location: {
  336. ...router.location,
  337. query: {
  338. query: 'anothertag:value',
  339. vitalName: 'measurements.cls',
  340. },
  341. },
  342. };
  343. const context = TestStubs.routerContext([
  344. {
  345. organization,
  346. project,
  347. router: newRouter,
  348. location: newRouter.location,
  349. },
  350. ]);
  351. render(<TestComponent router={newRouter} />, {
  352. context,
  353. organization: org,
  354. });
  355. expect(await screen.findByText('Cumulative Layout Shift')).toBeInTheDocument();
  356. userEvent.click(
  357. screen.getByLabelText('See transaction summary of the transaction something')
  358. );
  359. expect(newRouter.push).toHaveBeenCalledWith({
  360. pathname: '/organizations/org-slug/performance/summary/',
  361. query: {
  362. transaction: 'something',
  363. project: undefined,
  364. environment: [],
  365. statsPeriod: '24h',
  366. start: undefined,
  367. end: undefined,
  368. query: 'anothertag:value has:measurements.cls',
  369. unselectedSeries: 'p100()',
  370. showTransactions: 'recent',
  371. display: 'vitals',
  372. trendFunction: undefined,
  373. trendColumn: undefined,
  374. },
  375. });
  376. // Check cells are not in ms
  377. expect(screen.getByText('0.215').closest('td')).toBeInTheDocument();
  378. });
  379. it('can switch vitals with dropdown menu', function () {
  380. const newRouter = {
  381. ...router,
  382. location: {
  383. ...router.location,
  384. query: {
  385. project: 1,
  386. query: 'tag:value',
  387. },
  388. },
  389. };
  390. const context = TestStubs.routerContext([
  391. {
  392. organization,
  393. project,
  394. router: newRouter,
  395. location: newRouter.location,
  396. },
  397. ]);
  398. render(<TestComponent router={newRouter} />, {
  399. context,
  400. organization: org,
  401. });
  402. const button = screen.getByRole('button', {name: /web vitals: lcp/i});
  403. expect(button).toBeInTheDocument();
  404. userEvent.click(button);
  405. const menuItem = screen.getByRole('menuitemradio', {name: /fcp/i});
  406. expect(menuItem).toBeInTheDocument();
  407. userEvent.click(menuItem);
  408. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  409. expect(browserHistory.push).toHaveBeenCalledWith({
  410. pathname: undefined,
  411. query: {
  412. project: 1,
  413. query: 'tag:value',
  414. vitalName: 'measurements.fcp',
  415. },
  416. });
  417. });
  418. it('renders LCP vital correctly', async function () {
  419. render(<TestComponent />, {
  420. context: routerContext,
  421. organization: org,
  422. });
  423. expect(await screen.findByText('Largest Contentful Paint')).toBeInTheDocument();
  424. expect(
  425. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  426. ).toBeInTheDocument();
  427. expect(screen.getByText('4.50s').closest('td')).toBeInTheDocument();
  428. });
  429. it('correctly renders which browsers support LCP', function () {
  430. render(<TestComponent />, {
  431. context: routerContext,
  432. organization: org,
  433. });
  434. testSupportedBrowserRendering(WebVital.LCP);
  435. });
  436. it('correctly renders which browsers support CLS', function () {
  437. const newRouter = {
  438. ...router,
  439. location: {
  440. ...router.location,
  441. query: {
  442. vitalName: 'measurements.cls',
  443. },
  444. },
  445. };
  446. render(<TestComponent router={newRouter} />, {
  447. context: routerContext,
  448. organization: org,
  449. });
  450. testSupportedBrowserRendering(WebVital.CLS);
  451. });
  452. it('correctly renders which browsers support FCP', function () {
  453. const newRouter = {
  454. ...router,
  455. location: {
  456. ...router.location,
  457. query: {
  458. vitalName: 'measurements.fcp',
  459. },
  460. },
  461. };
  462. MockApiClient.warnOnMissingMocks();
  463. render(<TestComponent router={newRouter} />, {
  464. context: routerContext,
  465. organization: org,
  466. });
  467. testSupportedBrowserRendering(WebVital.FCP);
  468. });
  469. it('correctly renders which browsers support FID', function () {
  470. const newRouter = {
  471. ...router,
  472. location: {
  473. ...router.location,
  474. query: {
  475. vitalName: 'measurements.fid',
  476. },
  477. },
  478. };
  479. MockApiClient.warnOnMissingMocks();
  480. render(<TestComponent router={newRouter} />, {
  481. context: routerContext,
  482. organization: org,
  483. });
  484. testSupportedBrowserRendering(WebVital.FID);
  485. });
  486. });