index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 {DEFAULT_STATS_PERIOD} from 'sentry/views/performance/data';
  10. import VitalDetail from 'sentry/views/performance/vitalDetail';
  11. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  12. const api = new MockApiClient();
  13. const organization = TestStubs.Organization({
  14. features: ['discover-basic', 'performance-view'],
  15. projects: [TestStubs.Project()],
  16. });
  17. const {
  18. routerContext,
  19. organization: org,
  20. router,
  21. project,
  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. });
  204. afterEach(function () {
  205. MockApiClient.clearMockResponses();
  206. ProjectsStore.reset();
  207. });
  208. it('renders basic UI elements', async function () {
  209. render(<TestComponent />, {
  210. context: routerContext,
  211. organization: org,
  212. });
  213. // It shows a search bar
  214. expect(await screen.findByLabelText('Search events')).toBeInTheDocument();
  215. // It shows the vital card
  216. expect(
  217. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  218. ).toBeInTheDocument();
  219. expect(screen.getByText('Good 50%', {exact: false})).toBeInTheDocument();
  220. expect(screen.getByText('Meh 33%', {exact: false})).toBeInTheDocument();
  221. expect(screen.getByText('Poor 17%', {exact: false})).toBeInTheDocument();
  222. // It shows a chart
  223. expect(screen.getByText('Duration p75')).toBeInTheDocument();
  224. // It shows a table
  225. expect(screen.getByText('something').closest('td')).toBeInTheDocument();
  226. });
  227. it('triggers a navigation on search', async function () {
  228. render(<TestComponent />, {
  229. context: routerContext,
  230. organization: org,
  231. });
  232. // Fill out the search box, and submit it.
  233. await userEvent.click(await screen.findByLabelText('Search events'));
  234. await userEvent.paste('user.email:uhoh*');
  235. await userEvent.keyboard('{enter}');
  236. // Check the navigation.
  237. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  238. expect(browserHistory.push).toHaveBeenCalledWith({
  239. pathname: undefined,
  240. query: {
  241. project: '1',
  242. statsPeriod: '14d',
  243. query: 'user.email:uhoh*',
  244. },
  245. });
  246. });
  247. it('applies conditions when linking to transaction summary', async function () {
  248. const newRouter = {
  249. ...router,
  250. location: {
  251. ...router.location,
  252. query: {
  253. query: 'sometag:value',
  254. },
  255. },
  256. };
  257. const context = TestStubs.routerContext([
  258. {
  259. organization,
  260. project,
  261. router: newRouter,
  262. location: newRouter.location,
  263. },
  264. ]);
  265. render(<TestComponent router={newRouter} />, {
  266. context,
  267. organization: org,
  268. });
  269. expect(
  270. await screen.findByRole('heading', {name: 'Largest Contentful Paint'})
  271. ).toBeInTheDocument();
  272. await userEvent.click(
  273. screen.getByLabelText('See transaction summary of the transaction something')
  274. );
  275. expect(newRouter.push).toHaveBeenCalledWith({
  276. pathname: `/organizations/${organization.slug}/performance/summary/`,
  277. query: {
  278. transaction: 'something',
  279. project: undefined,
  280. environment: [],
  281. statsPeriod: DEFAULT_STATS_PERIOD,
  282. start: undefined,
  283. end: undefined,
  284. query: 'sometag:value has:measurements.lcp',
  285. referrer: 'performance-transaction-summary',
  286. unselectedSeries: ['p100()', 'avg()'],
  287. showTransactions: 'recent',
  288. display: 'vitals',
  289. trendFunction: undefined,
  290. trendColumn: undefined,
  291. },
  292. });
  293. });
  294. it('check CLS', async function () {
  295. const newRouter = {
  296. ...router,
  297. location: {
  298. ...router.location,
  299. query: {
  300. query: 'anothertag:value',
  301. vitalName: 'measurements.cls',
  302. },
  303. },
  304. };
  305. const context = TestStubs.routerContext([
  306. {
  307. organization,
  308. project,
  309. router: newRouter,
  310. location: newRouter.location,
  311. },
  312. ]);
  313. render(<TestComponent router={newRouter} />, {
  314. context,
  315. organization: org,
  316. });
  317. expect(await screen.findByText('Cumulative Layout Shift')).toBeInTheDocument();
  318. await userEvent.click(
  319. screen.getByLabelText('See transaction summary of the transaction something')
  320. );
  321. expect(newRouter.push).toHaveBeenCalledWith({
  322. pathname: `/organizations/${organization.slug}/performance/summary/`,
  323. query: {
  324. transaction: 'something',
  325. project: undefined,
  326. environment: [],
  327. statsPeriod: DEFAULT_STATS_PERIOD,
  328. start: undefined,
  329. end: undefined,
  330. query: 'anothertag:value has:measurements.cls',
  331. referrer: 'performance-transaction-summary',
  332. unselectedSeries: ['p100()', 'avg()'],
  333. showTransactions: 'recent',
  334. display: 'vitals',
  335. trendFunction: undefined,
  336. trendColumn: undefined,
  337. },
  338. });
  339. // Check cells are not in ms
  340. expect(screen.getByText('0.215').closest('td')).toBeInTheDocument();
  341. });
  342. it('can switch vitals with dropdown menu', async function () {
  343. const newRouter = {
  344. ...router,
  345. location: {
  346. ...router.location,
  347. query: {
  348. project: 1,
  349. query: 'tag:value',
  350. },
  351. },
  352. };
  353. const context = TestStubs.routerContext([
  354. {
  355. organization,
  356. project,
  357. router: newRouter,
  358. location: newRouter.location,
  359. },
  360. ]);
  361. render(<TestComponent router={newRouter} />, {
  362. context,
  363. organization: org,
  364. });
  365. const button = screen.getByRole('button', {name: /web vitals: lcp/i});
  366. expect(button).toBeInTheDocument();
  367. await userEvent.click(button);
  368. const menuItem = screen.getByRole('menuitemradio', {name: /fcp/i});
  369. expect(menuItem).toBeInTheDocument();
  370. await userEvent.click(menuItem);
  371. expect(browserHistory.push).toHaveBeenCalledTimes(1);
  372. expect(browserHistory.push).toHaveBeenCalledWith({
  373. pathname: undefined,
  374. query: {
  375. project: 1,
  376. query: 'tag:value',
  377. vitalName: 'measurements.fcp',
  378. },
  379. });
  380. });
  381. it('renders LCP vital correctly', async function () {
  382. render(<TestComponent />, {
  383. context: routerContext,
  384. organization: org,
  385. });
  386. expect(await screen.findByText('Largest Contentful Paint')).toBeInTheDocument();
  387. expect(
  388. screen.getByText(textWithMarkupMatcher('The p75 for all transactions is 4500ms'))
  389. ).toBeInTheDocument();
  390. expect(screen.getByText('4.50s').closest('td')).toBeInTheDocument();
  391. });
  392. it('correctly renders which browsers support LCP', function () {
  393. render(<TestComponent />, {
  394. context: routerContext,
  395. organization: org,
  396. });
  397. testSupportedBrowserRendering(WebVital.LCP);
  398. });
  399. it('correctly renders which browsers support CLS', function () {
  400. const newRouter = {
  401. ...router,
  402. location: {
  403. ...router.location,
  404. query: {
  405. vitalName: 'measurements.cls',
  406. },
  407. },
  408. };
  409. render(<TestComponent router={newRouter} />, {
  410. context: routerContext,
  411. organization: org,
  412. });
  413. testSupportedBrowserRendering(WebVital.CLS);
  414. });
  415. it('correctly renders which browsers support FCP', function () {
  416. const newRouter = {
  417. ...router,
  418. location: {
  419. ...router.location,
  420. query: {
  421. vitalName: 'measurements.fcp',
  422. },
  423. },
  424. };
  425. MockApiClient.addMockResponse({
  426. url: `/organizations/${organization.slug}/events/`,
  427. body: [],
  428. });
  429. render(<TestComponent router={newRouter} />, {
  430. context: routerContext,
  431. organization: org,
  432. });
  433. testSupportedBrowserRendering(WebVital.FCP);
  434. });
  435. it('correctly renders which browsers support FID', function () {
  436. const newRouter = {
  437. ...router,
  438. location: {
  439. ...router.location,
  440. query: {
  441. vitalName: 'measurements.fid',
  442. },
  443. },
  444. };
  445. MockApiClient.addMockResponse({
  446. url: `/organizations/${organization.slug}/events/`,
  447. body: [],
  448. });
  449. render(<TestComponent router={newRouter} />, {
  450. context: routerContext,
  451. organization: org,
  452. });
  453. testSupportedBrowserRendering(WebVital.FID);
  454. });
  455. });