index.spec.tsx 14 KB

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