index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. import {browserHistory, InjectedRouter} from 'react-router';
  2. import {MetricsField} from 'sentry-fixture/metrics';
  3. import {initializeOrg} from 'sentry-test/initializeOrg';
  4. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  5. import {textWithMarkupMatcher} from 'sentry-test/utils';
  6. import ProjectsStore from 'sentry/stores/projectsStore';
  7. import TeamStore from 'sentry/stores/teamStore';
  8. import {WebVital} from 'sentry/utils/fields';
  9. import {Browser} from 'sentry/utils/performance/vitals/constants';
  10. import {DEFAULT_STATS_PERIOD} from 'sentry/views/performance/data';
  11. import VitalDetail from 'sentry/views/performance/vitalDetail';
  12. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  13. const api = new MockApiClient();
  14. const organization = TestStubs.Organization({
  15. features: ['discover-basic', 'performance-view'],
  16. projects: [TestStubs.Project()],
  17. });
  18. const {
  19. routerContext,
  20. organization: org,
  21. router,
  22. project,
  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(org.projects);
  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: MetricsField('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. context: routerContext,
  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. context: routerContext,
  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. const context = TestStubs.routerContext([
  257. {
  258. organization,
  259. project,
  260. router: newRouter,
  261. location: newRouter.location,
  262. },
  263. ]);
  264. render(<TestComponent router={newRouter} />, {
  265. context,
  266. organization: org,
  267. });
  268. expect(
  269. await screen.findByRole('heading', {name: 'Largest Contentful Paint'})
  270. ).toBeInTheDocument();
  271. await userEvent.click(
  272. screen.getByLabelText('See transaction summary of the transaction something')
  273. );
  274. expect(newRouter.push).toHaveBeenCalledWith({
  275. pathname: `/organizations/${organization.slug}/performance/summary/`,
  276. query: {
  277. transaction: 'something',
  278. project: undefined,
  279. environment: [],
  280. statsPeriod: DEFAULT_STATS_PERIOD,
  281. start: undefined,
  282. end: undefined,
  283. query: 'sometag:value has:measurements.lcp',
  284. referrer: 'performance-transaction-summary',
  285. unselectedSeries: ['p100()', 'avg()'],
  286. showTransactions: 'recent',
  287. display: 'vitals',
  288. trendFunction: undefined,
  289. trendColumn: undefined,
  290. },
  291. });
  292. });
  293. it('check CLS', async function () {
  294. const newRouter = {
  295. ...router,
  296. location: {
  297. ...router.location,
  298. query: {
  299. query: 'anothertag:value',
  300. vitalName: 'measurements.cls',
  301. },
  302. },
  303. };
  304. const context = TestStubs.routerContext([
  305. {
  306. organization,
  307. project,
  308. router: newRouter,
  309. location: newRouter.location,
  310. },
  311. ]);
  312. render(<TestComponent router={newRouter} />, {
  313. context,
  314. organization: org,
  315. });
  316. expect(await screen.findByText('Cumulative Layout Shift')).toBeInTheDocument();
  317. await userEvent.click(
  318. screen.getByLabelText('See transaction summary of the transaction something')
  319. );
  320. expect(newRouter.push).toHaveBeenCalledWith({
  321. pathname: `/organizations/${organization.slug}/performance/summary/`,
  322. query: {
  323. transaction: 'something',
  324. project: undefined,
  325. environment: [],
  326. statsPeriod: DEFAULT_STATS_PERIOD,
  327. start: undefined,
  328. end: undefined,
  329. query: 'anothertag:value has:measurements.cls',
  330. referrer: 'performance-transaction-summary',
  331. unselectedSeries: ['p100()', 'avg()'],
  332. showTransactions: 'recent',
  333. display: 'vitals',
  334. trendFunction: undefined,
  335. trendColumn: undefined,
  336. },
  337. });
  338. // Check cells are not in ms
  339. expect(screen.getByText('0.215').closest('td')).toBeInTheDocument();
  340. });
  341. it('can switch vitals with dropdown menu', async function () {
  342. const newRouter = {
  343. ...router,
  344. location: {
  345. ...router.location,
  346. query: {
  347. project: 1,
  348. query: 'tag:value',
  349. },
  350. },
  351. };
  352. const context = TestStubs.routerContext([
  353. {
  354. organization,
  355. project,
  356. router: newRouter,
  357. location: newRouter.location,
  358. },
  359. ]);
  360. render(<TestComponent router={newRouter} />, {
  361. context,
  362. organization: org,
  363. });
  364. const button = screen.getByRole('button', {name: /web vitals: lcp/i});
  365. expect(button).toBeInTheDocument();
  366. await userEvent.click(button);
  367. const menuItem = screen.getByRole('menuitemradio', {name: /fcp/i});
  368. expect(menuItem).toBeInTheDocument();
  369. await userEvent.click(menuItem);
  370. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  371. expect(browserHistory.push).toHaveBeenCalledWith({
  372. pathname: undefined,
  373. query: {
  374. project: 1,
  375. query: 'tag:value',
  376. vitalName: 'measurements.fcp',
  377. },
  378. });
  379. });
  380. it('renders LCP vital correctly', async function () {
  381. render(<TestComponent />, {
  382. context: routerContext,
  383. organization: org,
  384. });
  385. expect(await screen.findByText('Largest Contentful Paint')).toBeInTheDocument();
  386. expect(
  387. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  388. ).toBeInTheDocument();
  389. expect(screen.getByText('4.50s').closest('td')).toBeInTheDocument();
  390. });
  391. it('correctly renders which browsers support LCP', function () {
  392. render(<TestComponent />, {
  393. context: routerContext,
  394. organization: org,
  395. });
  396. testSupportedBrowserRendering(WebVital.LCP);
  397. });
  398. it('correctly renders which browsers support CLS', function () {
  399. const newRouter = {
  400. ...router,
  401. location: {
  402. ...router.location,
  403. query: {
  404. vitalName: 'measurements.cls',
  405. },
  406. },
  407. };
  408. render(<TestComponent router={newRouter} />, {
  409. context: routerContext,
  410. organization: org,
  411. });
  412. testSupportedBrowserRendering(WebVital.CLS);
  413. });
  414. it('correctly renders which browsers support FCP', function () {
  415. const newRouter = {
  416. ...router,
  417. location: {
  418. ...router.location,
  419. query: {
  420. vitalName: 'measurements.fcp',
  421. },
  422. },
  423. };
  424. MockApiClient.addMockResponse({
  425. url: `/organizations/${organization.slug}/events/`,
  426. body: [],
  427. });
  428. render(<TestComponent router={newRouter} />, {
  429. context: routerContext,
  430. organization: org,
  431. });
  432. testSupportedBrowserRendering(WebVital.FCP);
  433. });
  434. it('correctly renders which browsers support FID', function () {
  435. const newRouter = {
  436. ...router,
  437. location: {
  438. ...router.location,
  439. query: {
  440. vitalName: 'measurements.fid',
  441. },
  442. },
  443. };
  444. MockApiClient.addMockResponse({
  445. url: `/organizations/${organization.slug}/events/`,
  446. body: [],
  447. });
  448. render(<TestComponent router={newRouter} />, {
  449. context: routerContext,
  450. organization: org,
  451. });
  452. testSupportedBrowserRendering(WebVital.FID);
  453. });
  454. });