index.spec.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import {browserHistory, InjectedRouter} from 'react-router';
  2. import {MetricsField} from 'sentry-fixture/metrics';
  3. import {Organization} from 'sentry-fixture/organization';
  4. import RouterContextFixture from 'sentry-fixture/routerContextFixture';
  5. import {initializeOrg} from 'sentry-test/initializeOrg';
  6. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  7. import {textWithMarkupMatcher} from 'sentry-test/utils';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import TeamStore from 'sentry/stores/teamStore';
  10. import {WebVital} from 'sentry/utils/fields';
  11. import {Browser} from 'sentry/utils/performance/vitals/constants';
  12. import {DEFAULT_STATS_PERIOD} from 'sentry/views/performance/data';
  13. import VitalDetail from 'sentry/views/performance/vitalDetail';
  14. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  15. const api = new MockApiClient();
  16. const organization = Organization({
  17. features: ['discover-basic', 'performance-view'],
  18. projects: [TestStubs.Project()],
  19. });
  20. const {
  21. routerContext,
  22. organization: org,
  23. router,
  24. project,
  25. } = initializeOrg({
  26. organization,
  27. router: {
  28. location: {
  29. query: {
  30. project: '1',
  31. },
  32. },
  33. },
  34. });
  35. function TestComponent(props: {router?: InjectedRouter} = {}) {
  36. return (
  37. <VitalDetail
  38. api={api}
  39. location={props.router?.location ?? router.location}
  40. router={props.router ?? router}
  41. params={{}}
  42. route={{}}
  43. routes={[]}
  44. routeParams={{}}
  45. />
  46. );
  47. }
  48. const testSupportedBrowserRendering = (webVital: WebVital) => {
  49. Object.values(Browser).forEach(browser => {
  50. const browserElement = screen.getByText(browser);
  51. expect(browserElement).toBeInTheDocument();
  52. const isSupported = vitalSupportedBrowsers[webVital]?.includes(browser);
  53. if (isSupported) {
  54. expect(within(browserElement).getByTestId('icon-check-mark')).toBeInTheDocument();
  55. } else {
  56. expect(within(browserElement).getByTestId('icon-close')).toBeInTheDocument();
  57. }
  58. });
  59. };
  60. describe('Performance > VitalDetail', function () {
  61. beforeEach(function () {
  62. TeamStore.loadInitialData([], false, null);
  63. ProjectsStore.loadInitialData(org.projects);
  64. browserHistory.push = jest.fn();
  65. MockApiClient.addMockResponse({
  66. url: `/organizations/${organization.slug}/projects/`,
  67. body: [],
  68. });
  69. MockApiClient.addMockResponse({
  70. url: `/organizations/${organization.slug}/tags/`,
  71. body: [],
  72. });
  73. MockApiClient.addMockResponse({
  74. url: `/organizations/${organization.slug}/events-stats/`,
  75. body: {data: [[123, []]]},
  76. });
  77. MockApiClient.addMockResponse({
  78. url: `/organizations/${organization.slug}/tags/user.email/values/`,
  79. body: [],
  80. });
  81. MockApiClient.addMockResponse({
  82. url: `/organizations/${organization.slug}/releases/stats/`,
  83. body: [],
  84. });
  85. MockApiClient.addMockResponse({
  86. url: `/organizations/${organization.slug}/users/`,
  87. body: [],
  88. });
  89. MockApiClient.addMockResponse({
  90. url: `/organizations/${organization.slug}/recent-searches/`,
  91. body: [],
  92. });
  93. MockApiClient.addMockResponse({
  94. url: `/organizations/${organization.slug}/recent-searches/`,
  95. method: 'POST',
  96. body: [],
  97. });
  98. MockApiClient.addMockResponse({
  99. url: `/organizations/${organization.slug}/events-vitals/`,
  100. body: {
  101. 'measurements.lcp': {
  102. poor: 1,
  103. meh: 2,
  104. good: 3,
  105. total: 6,
  106. p75: 4500,
  107. },
  108. },
  109. });
  110. MockApiClient.addMockResponse({
  111. url: `/organizations/${organization.slug}/events/`,
  112. body: {
  113. meta: {
  114. fields: {
  115. 'count()': 'integer',
  116. 'p95(measurements.lcp)': 'duration',
  117. transaction: 'string',
  118. 'p50(measurements.lcp)': 'duration',
  119. project: 'string',
  120. 'compare_numeric_aggregate(p75_measurements_lcp,greater,4000)': 'number',
  121. 'project.id': 'integer',
  122. 'count_unique_user()': 'integer',
  123. 'p75(measurements.lcp)': 'duration',
  124. },
  125. },
  126. data: [
  127. {
  128. 'count()': 100000,
  129. 'p95(measurements.lcp)': 5000,
  130. transaction: 'something',
  131. 'p50(measurements.lcp)': 3500,
  132. project: 'javascript',
  133. 'compare_numeric_aggregate(p75_measurements_lcp,greater,4000)': 1,
  134. 'count_unique_user()': 10000,
  135. 'p75(measurements.lcp)': 4500,
  136. },
  137. ],
  138. },
  139. match: [
  140. (_url, options) => {
  141. return options.query?.field?.find(f => f === 'p50(measurements.lcp)');
  142. },
  143. ],
  144. });
  145. MockApiClient.addMockResponse({
  146. url: `/organizations/${organization.slug}/events/`,
  147. body: {
  148. meta: {
  149. fields: {
  150. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.1)': 'number',
  151. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.25)': 'number',
  152. 'count()': 'integer',
  153. 'count_unique_user()': 'integer',
  154. team_key_transaction: 'boolean',
  155. 'p50(measurements.cls)': 'number',
  156. 'p75(measurements.cls)': 'number',
  157. 'p95(measurements.cls)': 'number',
  158. project: 'string',
  159. transaction: 'string',
  160. },
  161. },
  162. data: [
  163. {
  164. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.1)': 1,
  165. 'compare_numeric_aggregate(p75_measurements_cls,greater,0.25)': 0,
  166. 'count()': 10000,
  167. 'count_unique_user()': 2740,
  168. team_key_transaction: 1,
  169. 'p50(measurements.cls)': 0.143,
  170. 'p75(measurements.cls)': 0.215,
  171. 'p95(measurements.cls)': 0.302,
  172. project: 'javascript',
  173. transaction: 'something',
  174. },
  175. ],
  176. },
  177. match: [
  178. (_url, options) => {
  179. return options.query?.field?.find(f => f === 'p50(measurements.cls)');
  180. },
  181. ],
  182. });
  183. MockApiClient.addMockResponse({
  184. method: 'GET',
  185. url: `/organizations/${organization.slug}/key-transactions-list/`,
  186. body: [],
  187. });
  188. // Metrics Requests
  189. MockApiClient.addMockResponse({
  190. method: 'GET',
  191. url: `/organizations/${organization.slug}/metrics/tags/`,
  192. body: [],
  193. });
  194. MockApiClient.addMockResponse({
  195. method: 'GET',
  196. url: `/organizations/${organization.slug}/metrics/data/`,
  197. body: MetricsField('p75(sentry.transactions.measurements.lcp)'),
  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 = RouterContextFixture([
  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()', 'avg()'],
  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 = RouterContextFixture([
  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()', 'avg()'],
  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 = RouterContextFixture([
  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. });