index.spec.tsx 14 KB

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