trace.spec.tsx 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. import * as Sentry from '@sentry/react';
  2. import MockDate from 'mockdate';
  3. import {TransactionEventFixture} from 'sentry-fixture/event';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {
  6. render,
  7. screen,
  8. userEvent,
  9. waitFor,
  10. within,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import {EntryType, type EventTransaction} from 'sentry/types/event';
  13. import type {TraceFullDetailed} from 'sentry/utils/performance/quickTrace/types';
  14. import {TraceView} from 'sentry/views/performance/newTraceDetails/index';
  15. import {
  16. makeEventTransaction,
  17. makeSpan,
  18. makeTraceError,
  19. makeTransaction,
  20. } from 'sentry/views/performance/newTraceDetails/traceModels/traceTreeTestUtils';
  21. import type {TracePreferencesState} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences';
  22. import {DEFAULT_TRACE_VIEW_PREFERENCES} from 'sentry/views/performance/newTraceDetails/traceState/tracePreferences';
  23. class MockResizeObserver {
  24. callback: ResizeObserverCallback;
  25. constructor(callback: ResizeObserverCallback) {
  26. this.callback = callback;
  27. }
  28. unobserve(_element: HTMLElement) {
  29. return;
  30. }
  31. observe(element: HTMLElement) {
  32. setTimeout(() => {
  33. this.callback(
  34. [
  35. {
  36. target: element,
  37. // @ts-expect-error partial mock
  38. contentRect: {width: 1000, height: 24 * 20 - 1},
  39. },
  40. ],
  41. this
  42. );
  43. }, 0);
  44. }
  45. disconnect() {}
  46. }
  47. type Arguments<F extends Function> = F extends (...args: infer A) => any ? A : never;
  48. type ResponseType = Arguments<typeof MockApiClient.addMockResponse>[0];
  49. function mockQueryString(queryString: string) {
  50. Object.defineProperty(window, 'location', {
  51. value: {
  52. search: queryString,
  53. },
  54. });
  55. }
  56. function mockTracePreferences(preferences: Partial<TracePreferencesState>) {
  57. const merged: TracePreferencesState = {
  58. ...DEFAULT_TRACE_VIEW_PREFERENCES,
  59. ...preferences,
  60. autogroup: {
  61. ...DEFAULT_TRACE_VIEW_PREFERENCES.autogroup,
  62. ...preferences.autogroup,
  63. },
  64. drawer: {
  65. ...DEFAULT_TRACE_VIEW_PREFERENCES.drawer,
  66. ...preferences.drawer,
  67. },
  68. list: {
  69. ...DEFAULT_TRACE_VIEW_PREFERENCES.list,
  70. ...preferences.list,
  71. },
  72. };
  73. localStorage.setItem('trace-view-preferences', JSON.stringify(merged));
  74. }
  75. function mockTraceResponse(resp?: Partial<ResponseType>) {
  76. MockApiClient.addMockResponse({
  77. url: '/organizations/org-slug/events-trace/trace-id/',
  78. method: 'GET',
  79. asyncDelay: 1,
  80. ...(resp ?? {body: {}}),
  81. });
  82. }
  83. function mockPerformanceSubscriptionDetailsResponse(resp?: Partial<ResponseType>) {
  84. MockApiClient.addMockResponse({
  85. url: '/subscriptions/org-slug/',
  86. method: 'GET',
  87. asyncDelay: 1,
  88. ...(resp ?? {body: {}}),
  89. });
  90. }
  91. function mockTraceMetaResponse(resp?: Partial<ResponseType>) {
  92. MockApiClient.addMockResponse({
  93. url: '/organizations/org-slug/events-trace-meta/trace-id/',
  94. method: 'GET',
  95. asyncDelay: 1,
  96. ...(resp ?? {
  97. body: {
  98. errors: 0,
  99. performance_issues: 0,
  100. projects: 0,
  101. transactions: 0,
  102. transaction_child_count_map: [],
  103. },
  104. }),
  105. });
  106. }
  107. function mockTraceTagsResponse(resp?: Partial<ResponseType>) {
  108. MockApiClient.addMockResponse({
  109. url: '/organizations/org-slug/events-facets/',
  110. method: 'GET',
  111. asyncDelay: 1,
  112. ...(resp ?? {body: []}),
  113. });
  114. }
  115. // function _mockTraceDetailsResponse(id: string, resp?: Partial<ResponseType>) {
  116. // MockApiClient.addMockResponse({
  117. // url: `/organizations/org-slug/events/project_slug:transaction-${id}`,
  118. // method: 'GET',
  119. // asyncDelay: 1,
  120. // ...(resp ?? {}),
  121. // });
  122. // }
  123. function mockTransactionDetailsResponse(id: string, resp?: Partial<ResponseType>) {
  124. MockApiClient.addMockResponse({
  125. url: `/organizations/org-slug/events/project_slug:${id}/`,
  126. method: 'GET',
  127. asyncDelay: 1,
  128. ...(resp ?? {body: TransactionEventFixture()}),
  129. });
  130. }
  131. function mockTraceRootEvent(id: string, resp?: Partial<ResponseType>) {
  132. MockApiClient.addMockResponse({
  133. url: `/organizations/org-slug/events/project_slug:${id}/`,
  134. method: 'GET',
  135. asyncDelay: 1,
  136. ...(resp ?? {body: TransactionEventFixture()}),
  137. });
  138. }
  139. function mockTraceRootFacets(resp?: Partial<ResponseType>) {
  140. MockApiClient.addMockResponse({
  141. url: `/organizations/org-slug/events-facets/`,
  142. method: 'GET',
  143. asyncDelay: 1,
  144. body: {},
  145. ...(resp ?? {}),
  146. });
  147. }
  148. function mockTraceEventDetails(resp?: Partial<ResponseType>) {
  149. MockApiClient.addMockResponse({
  150. url: `/organizations/org-slug/events/`,
  151. method: 'GET',
  152. asyncDelay: 1,
  153. body: {},
  154. ...(resp ?? {body: TransactionEventFixture()}),
  155. });
  156. }
  157. function mockSpansResponse(
  158. id: string,
  159. resp?: Partial<ResponseType>,
  160. body: Partial<EventTransaction> = {}
  161. ) {
  162. return MockApiClient.addMockResponse({
  163. url: `/organizations/org-slug/events/project_slug:${id}/?averageColumn=span.self_time&averageColumn=span.duration`,
  164. method: 'GET',
  165. asyncDelay: 1,
  166. body,
  167. ...(resp ?? {}),
  168. });
  169. }
  170. function mockTransactionSpansResponse(
  171. id: string,
  172. resp?: Partial<ResponseType>,
  173. body: Partial<EventTransaction> = {}
  174. ) {
  175. return MockApiClient.addMockResponse({
  176. url: `/organizations/org-slug/events/project_slug:${id}/`,
  177. method: 'GET',
  178. asyncDelay: 1,
  179. body,
  180. ...(resp ?? {}),
  181. });
  182. }
  183. const {router} = initializeOrg({
  184. router: {
  185. params: {orgId: 'org-slug', traceSlug: 'trace-id'},
  186. },
  187. });
  188. function mockMetricsResponse() {
  189. MockApiClient.addMockResponse({
  190. url: '/organizations/org-slug/metrics/query/',
  191. method: 'POST',
  192. body: {
  193. data: [],
  194. queries: [],
  195. },
  196. });
  197. }
  198. function mockEventsResponse() {
  199. MockApiClient.addMockResponse({
  200. url: '/organizations/org-slug/events/',
  201. method: 'GET',
  202. body: {
  203. data: [],
  204. queries: [],
  205. },
  206. });
  207. }
  208. function getVirtualizedContainer(): HTMLElement {
  209. const virtualizedContainer = screen.queryByTestId('trace-virtualized-list');
  210. if (!virtualizedContainer) {
  211. throw new Error('Virtualized container not found');
  212. }
  213. return virtualizedContainer;
  214. }
  215. function getVirtualizedScrollContainer(): HTMLElement {
  216. const virtualizedScrollContainer = screen.queryByTestId(
  217. 'trace-virtualized-list-scroll-container'
  218. );
  219. if (!virtualizedScrollContainer) {
  220. throw new Error('Virtualized scroll container not found');
  221. }
  222. return virtualizedScrollContainer;
  223. }
  224. function getVirtualizedRows(container: HTMLElement) {
  225. return Array.from(container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR));
  226. }
  227. async function keyboardNavigationTestSetup() {
  228. mockPerformanceSubscriptionDetailsResponse();
  229. const keyboard_navigation_transactions: TraceFullDetailed[] = [];
  230. for (let i = 0; i < 1e2; i++) {
  231. keyboard_navigation_transactions.push(
  232. makeTransaction({
  233. span_id: i + '',
  234. event_id: i + '',
  235. transaction: 'transaction-name-' + i,
  236. 'transaction.op': 'transaction-op-' + i,
  237. project_slug: 'project_slug',
  238. })
  239. );
  240. mockTransactionDetailsResponse(`${i}`);
  241. }
  242. mockTraceResponse({
  243. body: {
  244. transactions: keyboard_navigation_transactions,
  245. orphan_errors: [],
  246. },
  247. });
  248. mockTraceMetaResponse({
  249. body: {
  250. errors: 0,
  251. performance_issues: 0,
  252. projects: 0,
  253. transactions: 0,
  254. transaction_child_count_map: keyboard_navigation_transactions.map(t => ({
  255. 'transaction.id': t.event_id,
  256. count: 5,
  257. })),
  258. },
  259. });
  260. mockTraceRootFacets();
  261. mockTraceRootEvent('0');
  262. mockTraceEventDetails();
  263. mockMetricsResponse();
  264. mockEventsResponse();
  265. const value = render(<TraceView />, {router});
  266. const virtualizedContainer = getVirtualizedContainer();
  267. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  268. // Awaits for the placeholder rendering rows to be removed
  269. try {
  270. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  271. timeout: 5000,
  272. });
  273. } catch (e) {
  274. printVirtualizedList(virtualizedContainer);
  275. throw e;
  276. }
  277. return {...value, virtualizedContainer, virtualizedScrollContainer};
  278. }
  279. async function pageloadTestSetup() {
  280. mockPerformanceSubscriptionDetailsResponse();
  281. const pageloadTransactions: TraceFullDetailed[] = [];
  282. for (let i = 0; i < 1e3; i++) {
  283. pageloadTransactions.push(
  284. makeTransaction({
  285. span_id: i + '',
  286. event_id: i + '',
  287. transaction: 'transaction-name-' + i,
  288. 'transaction.op': 'transaction-op-' + i,
  289. project_slug: 'project_slug',
  290. })
  291. );
  292. mockTransactionDetailsResponse(`${i}`);
  293. }
  294. mockTraceResponse({
  295. body: {
  296. transactions: pageloadTransactions,
  297. orphan_errors: [],
  298. },
  299. });
  300. mockTraceMetaResponse({
  301. body: {
  302. errors: 0,
  303. performance_issues: 0,
  304. projects: 0,
  305. transactions: 0,
  306. transaction_child_count_map: pageloadTransactions.map(t => ({
  307. 'transaction.id': t.event_id,
  308. count: 5,
  309. })),
  310. },
  311. });
  312. mockTraceRootFacets();
  313. mockTraceRootEvent('0');
  314. mockTraceEventDetails();
  315. mockMetricsResponse();
  316. mockEventsResponse();
  317. const value = render(<TraceView />, {router});
  318. const virtualizedContainer = getVirtualizedContainer();
  319. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  320. // Awaits for the placeholder rendering rows to be removed
  321. try {
  322. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  323. timeout: 5000,
  324. });
  325. } catch (e) {
  326. printVirtualizedList(virtualizedContainer);
  327. throw e;
  328. }
  329. return {...value, virtualizedContainer, virtualizedScrollContainer};
  330. }
  331. async function nestedTransactionsTestSetup() {
  332. mockPerformanceSubscriptionDetailsResponse();
  333. const transactions: TraceFullDetailed[] = [];
  334. let txn = makeTransaction({
  335. span_id: '0',
  336. event_id: '0',
  337. transaction: 'transaction-name-0',
  338. 'transaction.op': 'transaction-op-0',
  339. project_slug: 'project_slug',
  340. });
  341. transactions.push(txn);
  342. for (let i = 0; i < 100; i++) {
  343. const next = makeTransaction({
  344. span_id: i + '',
  345. event_id: i + '',
  346. transaction: 'transaction-name-' + i,
  347. 'transaction.op': 'transaction-op-' + i,
  348. project_slug: 'project_slug',
  349. });
  350. txn.children.push(next);
  351. txn = next;
  352. transactions.push(next);
  353. mockTransactionDetailsResponse(`${i}`);
  354. }
  355. mockTraceResponse({
  356. body: {
  357. transactions,
  358. orphan_errors: [],
  359. },
  360. });
  361. mockTraceMetaResponse();
  362. mockTraceRootFacets();
  363. mockTraceRootEvent('0');
  364. mockTraceEventDetails();
  365. mockMetricsResponse();
  366. mockEventsResponse();
  367. const value = render(<TraceView />, {router});
  368. const virtualizedContainer = getVirtualizedContainer();
  369. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  370. // Awaits for the placeholder rendering rows to be removed
  371. try {
  372. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  373. timeout: 5000,
  374. });
  375. } catch (e) {
  376. printVirtualizedList(virtualizedContainer);
  377. throw e;
  378. }
  379. return {...value, virtualizedContainer, virtualizedScrollContainer};
  380. }
  381. async function searchTestSetup() {
  382. mockPerformanceSubscriptionDetailsResponse();
  383. const transactions: TraceFullDetailed[] = [];
  384. for (let i = 0; i < 11; i++) {
  385. transactions.push(
  386. makeTransaction({
  387. span_id: i + '',
  388. event_id: i + '',
  389. transaction: 'transaction-name' + i,
  390. 'transaction.op': 'transaction-op-' + i,
  391. project_slug: 'project_slug',
  392. })
  393. );
  394. mockTransactionDetailsResponse(`${i}`);
  395. }
  396. mockTraceResponse({
  397. body: {
  398. transactions,
  399. orphan_errors: [],
  400. },
  401. });
  402. mockTraceMetaResponse({
  403. body: {
  404. errors: 0,
  405. performance_issues: 0,
  406. projects: 0,
  407. transactions: 0,
  408. transaction_child_count_map: transactions.map(t => ({
  409. 'transaction.id': t.event_id,
  410. count: 5,
  411. })),
  412. },
  413. });
  414. mockTraceRootFacets();
  415. mockTraceRootEvent('0');
  416. mockTraceEventDetails();
  417. mockMetricsResponse();
  418. mockEventsResponse();
  419. const value = render(<TraceView />, {router});
  420. const virtualizedContainer = getVirtualizedContainer();
  421. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  422. // Awaits for the placeholder rendering rows to be removed
  423. try {
  424. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  425. timeout: 5000,
  426. });
  427. } catch (e) {
  428. printVirtualizedList(virtualizedContainer);
  429. throw e;
  430. }
  431. return {...value, virtualizedContainer, virtualizedScrollContainer};
  432. }
  433. async function simpleTestSetup() {
  434. mockPerformanceSubscriptionDetailsResponse();
  435. const transactions: TraceFullDetailed[] = [];
  436. let parent: any;
  437. for (let i = 0; i < 1e3; i++) {
  438. const next = makeTransaction({
  439. span_id: i + '',
  440. event_id: i + '',
  441. transaction: 'transaction-name' + i,
  442. 'transaction.op': 'transaction-op-' + i,
  443. project_slug: 'project_slug',
  444. });
  445. if (parent) {
  446. parent.children.push(next);
  447. } else {
  448. transactions.push(next);
  449. }
  450. parent = next;
  451. mockTransactionDetailsResponse(`${i}`);
  452. }
  453. mockTraceResponse({
  454. body: {
  455. transactions,
  456. orphan_errors: [],
  457. },
  458. });
  459. mockTraceMetaResponse({
  460. body: {
  461. errors: 0,
  462. performance_issues: 0,
  463. projects: 0,
  464. transactions: 0,
  465. transaction_child_count_map: transactions.map(t => ({
  466. 'transaction.id': t.event_id,
  467. count: 5,
  468. })),
  469. },
  470. });
  471. mockTraceRootFacets();
  472. mockTraceRootEvent('0');
  473. mockTraceEventDetails();
  474. mockMetricsResponse();
  475. mockEventsResponse();
  476. const value = render(<TraceView />, {router});
  477. const virtualizedContainer = getVirtualizedContainer();
  478. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  479. // Awaits for the placeholder rendering rows to be removed
  480. try {
  481. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  482. timeout: 5000,
  483. });
  484. } catch (e) {
  485. printVirtualizedList(virtualizedContainer);
  486. throw e;
  487. }
  488. return {...value, virtualizedContainer, virtualizedScrollContainer};
  489. }
  490. async function completeTestSetup() {
  491. mockPerformanceSubscriptionDetailsResponse();
  492. const start = Date.now() / 1e3;
  493. mockTraceResponse({
  494. body: {
  495. transactions: [
  496. makeTransaction({
  497. event_id: '0',
  498. transaction: 'transaction-name-0',
  499. 'transaction.op': 'transaction-op-0',
  500. project_slug: 'project_slug',
  501. start_timestamp: start,
  502. timestamp: start + 2,
  503. children: [
  504. makeTransaction({
  505. event_id: '1',
  506. transaction: 'transaction-name-1',
  507. 'transaction.op': 'transaction-op-1',
  508. project_slug: 'project_slug',
  509. start_timestamp: start,
  510. timestamp: start + 2,
  511. }),
  512. ],
  513. }),
  514. makeTransaction({
  515. event_id: '2',
  516. transaction: 'transaction-name-2',
  517. 'transaction.op': 'transaction-op-2',
  518. project_slug: 'project_slug',
  519. start_timestamp: start,
  520. timestamp: start + 2,
  521. }),
  522. makeTransaction({
  523. event_id: '3',
  524. transaction: 'transaction-name-3',
  525. 'transaction.op': 'transaction-op-3',
  526. project_slug: 'project_slug',
  527. start_timestamp: start,
  528. timestamp: start + 2,
  529. }),
  530. ],
  531. orphan_errors: [
  532. makeTraceError({
  533. event_id: 'error0',
  534. issue: 'error-issue',
  535. project_id: 0,
  536. project_slug: 'project_slug',
  537. issue_id: 0,
  538. title: 'error-title',
  539. level: 'fatal',
  540. timestamp: start + 2,
  541. }),
  542. ],
  543. },
  544. });
  545. mockTraceMetaResponse({
  546. body: {
  547. errors: 0,
  548. performance_issues: 0,
  549. projects: 0,
  550. transactions: 0,
  551. transaction_child_count_map: [
  552. {
  553. 'transaction.id': '0',
  554. count: 2,
  555. },
  556. {
  557. 'transaction.id': '1',
  558. count: 2,
  559. },
  560. {
  561. 'transaction.id': '2',
  562. count: 2,
  563. },
  564. {
  565. 'transaction.id': '3',
  566. count: 2,
  567. },
  568. ],
  569. },
  570. });
  571. mockTraceRootFacets();
  572. mockTraceRootEvent('0');
  573. mockTraceEventDetails();
  574. mockMetricsResponse();
  575. mockEventsResponse();
  576. MockApiClient.addMockResponse({
  577. url: '/organizations/org-slug/events/project_slug:error0/',
  578. body: {
  579. tags: [],
  580. contexts: {},
  581. entries: [],
  582. },
  583. });
  584. const transactionWithSpans = makeEventTransaction({
  585. entries: [
  586. {
  587. type: EntryType.SPANS,
  588. data: [
  589. makeSpan({
  590. span_id: 'span0',
  591. op: 'http',
  592. description: 'request',
  593. start_timestamp: start,
  594. timestamp: start + 0.1,
  595. }),
  596. // Parent autogroup chain
  597. makeSpan({
  598. op: 'db',
  599. description: 'redis',
  600. parent_span_id: 'span0',
  601. span_id: 'redis0',
  602. start_timestamp: start + 0.1,
  603. timestamp: start + 0.2,
  604. }),
  605. makeSpan({
  606. op: 'db',
  607. description: 'redis',
  608. parent_span_id: 'redis0',
  609. span_id: 'redis1',
  610. start_timestamp: start + 0.2,
  611. timestamp: start + 0.3,
  612. }),
  613. // Sibling autogroup chain
  614. makeSpan({
  615. op: 'http',
  616. description: 'request',
  617. parent_span_id: 'span0',
  618. span_id: 'http0',
  619. start_timestamp: start + 0.3,
  620. timestamp: start + 0.4,
  621. }),
  622. makeSpan({
  623. op: 'http',
  624. description: 'request',
  625. parent_span_id: 'span0',
  626. span_id: 'http1',
  627. start_timestamp: start + 0.4,
  628. timestamp: start + 0.5,
  629. }),
  630. makeSpan({
  631. op: 'http',
  632. description: 'request',
  633. parent_span_id: 'span0',
  634. span_id: 'http2',
  635. start_timestamp: start + 0.5,
  636. timestamp: start + 0.6,
  637. }),
  638. makeSpan({
  639. op: 'http',
  640. description: 'request',
  641. parent_span_id: 'span0',
  642. span_id: 'http3',
  643. start_timestamp: start + 0.6,
  644. timestamp: start + 0.7,
  645. }),
  646. makeSpan({
  647. op: 'http',
  648. description: 'request',
  649. parent_span_id: 'span0',
  650. span_id: 'http4',
  651. start_timestamp: start + 0.7,
  652. timestamp: start + 0.8,
  653. }),
  654. // No instrumentation gap
  655. makeSpan({
  656. op: 'queue',
  657. description: 'process',
  658. parent_span_id: 'span0',
  659. span_id: 'queueprocess0',
  660. start_timestamp: start + 0.8,
  661. timestamp: start + 0.9,
  662. }),
  663. makeSpan({
  664. op: 'queue',
  665. description: 'process',
  666. parent_span_id: 'span0',
  667. span_id: 'queueprocess1',
  668. start_timestamp: start + 1.1,
  669. timestamp: start + 1.2,
  670. }),
  671. ],
  672. },
  673. ],
  674. });
  675. const transactionWithoutSpans = makeEventTransaction({});
  676. mockTransactionSpansResponse('1', {}, transactionWithSpans);
  677. mockSpansResponse('1', {}, transactionWithSpans);
  678. // Mock empty response for txn without spans
  679. mockTransactionSpansResponse('0', {}, transactionWithoutSpans);
  680. mockSpansResponse('0', {}, transactionWithoutSpans);
  681. const value = render(<TraceView />, {router});
  682. const virtualizedContainer = getVirtualizedContainer();
  683. const virtualizedScrollContainer = getVirtualizedScrollContainer();
  684. // Awaits for the placeholder rendering rows to be removed
  685. try {
  686. await within(virtualizedContainer).findAllByText(/transaction-op-/i, undefined, {
  687. timeout: 5000,
  688. });
  689. } catch (e) {
  690. printVirtualizedList(virtualizedContainer);
  691. throw e;
  692. }
  693. return {...value, virtualizedContainer, virtualizedScrollContainer};
  694. }
  695. const DRAWER_TABS_TEST_ID = 'trace-drawer-tab';
  696. const DRAWER_TABS_PIN_BUTTON_TEST_ID = 'trace-drawer-tab-pin-button';
  697. const VISIBLE_TRACE_ROW_SELECTOR = '.TraceRow:not(.Hidden)';
  698. const ACTIVE_SEARCH_HIGHLIGHT_ROW = '.TraceRow.SearchResult.Highlight:not(.Hidden)';
  699. const searchToResolve = async (): Promise<void> => {
  700. await screen.findByTestId('trace-search-success');
  701. };
  702. function printVirtualizedList(container: HTMLElement) {
  703. const stdout: string[] = [];
  704. const scrollContainer = screen.queryByTestId(
  705. 'trace-virtualized-list-scroll-container'
  706. )!;
  707. const rows = Array.from(container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR));
  708. const searchResultIterator = screen.queryByTestId('trace-search-result-iterator');
  709. const searchInput = screen.queryByPlaceholderText(
  710. 'Search in trace'
  711. ) as HTMLInputElement;
  712. const loading = screen.queryByTestId('trace-search-loading');
  713. const success = screen.queryByTestId('trace-search-success');
  714. stdout.push(
  715. 'Debug Information: ' +
  716. 'Rows=' +
  717. rows.length +
  718. ' ' +
  719. 'Search Query:' +
  720. (searchInput?.value || '<empty>') +
  721. ' ' +
  722. (searchResultIterator?.textContent || '<empty>') +
  723. ' ' +
  724. 'Search Status:' +
  725. (loading ? 'loading' : success ? 'success' : '<empty>') +
  726. ' ' +
  727. 'Scroll=' +
  728. 'top:' +
  729. scrollContainer.scrollTop +
  730. ' ' +
  731. 'left:' +
  732. scrollContainer.scrollLeft +
  733. ' '
  734. );
  735. for (const r of [...rows]) {
  736. const count = (r.querySelector('.TraceChildrenCount') as HTMLElement)?.textContent;
  737. const op = (r.querySelector('.TraceOperation') as HTMLElement)?.textContent;
  738. const desc = (r.querySelector('.TraceDescription') as HTMLElement)?.textContent;
  739. let t = (count ?? '') + ' ' + (op ?? '') + ' — ' + (desc ?? '');
  740. if (r.classList.contains('SearchResult')) {
  741. t = t + ' search';
  742. }
  743. if (r.classList.contains('Highlight')) {
  744. t = t + ' highlight';
  745. }
  746. if (document.activeElement === r) {
  747. t = t + ' ⬅ focused ';
  748. }
  749. const leftColumn = r.querySelector('.TraceLeftColumnInner') as HTMLElement;
  750. const left = Math.round(parseInt(leftColumn.style.paddingLeft, 10) / 10);
  751. stdout.push(' '.repeat(left) + t);
  752. }
  753. // This is a debug fn, we need it to log
  754. // eslint-disable-next-line
  755. console.log(stdout.join('\n'));
  756. }
  757. // @ts-expect-error ignore this line
  758. // eslint-disable-next-line
  759. function printTabs() {
  760. const tabs = screen.queryAllByTestId(DRAWER_TABS_TEST_ID);
  761. const stdout: string[] = [];
  762. for (const tab of tabs) {
  763. let text = tab.textContent ?? 'empty tab??';
  764. if (tab.hasAttribute('aria-selected')) {
  765. text = 'active' + text;
  766. }
  767. stdout.push(text);
  768. }
  769. // This is a debug fn, we need it to log
  770. // eslint-disable-next-line
  771. console.log(stdout.join(' | '));
  772. }
  773. async function assertHighlightedRowAtIndex(
  774. virtualizedContainer: HTMLElement,
  775. index: number
  776. ) {
  777. await waitFor(() => {
  778. expect(virtualizedContainer.querySelectorAll('.TraceRow.Highlight')).toHaveLength(1);
  779. });
  780. await waitFor(() => {
  781. const highlighted_row = virtualizedContainer.querySelector(
  782. ACTIVE_SEARCH_HIGHLIGHT_ROW
  783. );
  784. const r = Array.from(
  785. virtualizedContainer.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR)
  786. );
  787. expect(r.indexOf(highlighted_row!)).toBe(index);
  788. });
  789. }
  790. describe('trace view', () => {
  791. beforeEach(() => {
  792. globalThis.ResizeObserver = MockResizeObserver as any;
  793. mockQueryString('');
  794. MockDate.reset();
  795. });
  796. afterEach(() => {
  797. mockQueryString('');
  798. // @ts-expect-error clear mock
  799. globalThis.ResizeObserver = undefined;
  800. });
  801. it('renders loading state', async () => {
  802. mockPerformanceSubscriptionDetailsResponse();
  803. mockTraceResponse();
  804. mockTraceMetaResponse();
  805. mockTraceTagsResponse();
  806. mockEventsResponse();
  807. render(<TraceView />, {router});
  808. expect(await screen.findByText(/assembling the trace/i)).toBeInTheDocument();
  809. });
  810. it('renders error state if trace fails to load', async () => {
  811. mockPerformanceSubscriptionDetailsResponse();
  812. mockTraceResponse({statusCode: 404});
  813. mockTraceMetaResponse({statusCode: 404});
  814. mockTraceTagsResponse({statusCode: 404});
  815. mockEventsResponse();
  816. render(<TraceView />, {router});
  817. expect(await screen.findByText(/we failed to load your trace/i)).toBeInTheDocument();
  818. });
  819. it('renders error state if meta fails to load', async () => {
  820. mockPerformanceSubscriptionDetailsResponse();
  821. mockTraceResponse({
  822. statusCode: 200,
  823. body: {
  824. transactions: [makeTransaction()],
  825. orphan_errors: [],
  826. },
  827. });
  828. mockTraceMetaResponse({statusCode: 404});
  829. mockTraceTagsResponse({statusCode: 404});
  830. mockEventsResponse();
  831. render(<TraceView />, {router});
  832. expect(await screen.findByText(/we failed to load your trace/i)).toBeInTheDocument();
  833. });
  834. it('renders empty state', async () => {
  835. mockPerformanceSubscriptionDetailsResponse();
  836. mockTraceResponse({
  837. body: {
  838. transactions: [],
  839. orphan_errors: [],
  840. },
  841. });
  842. mockTraceMetaResponse();
  843. mockTraceTagsResponse();
  844. mockEventsResponse();
  845. render(<TraceView />, {router});
  846. expect(
  847. await screen.findByText(/trace does not contain any data/i)
  848. ).toBeInTheDocument();
  849. });
  850. describe('pageload', () => {
  851. it('scrolls to trace root', async () => {
  852. mockQueryString('?node=trace-root');
  853. const {virtualizedContainer} = await completeTestSetup();
  854. await waitFor(() => {
  855. const rows = getVirtualizedRows(virtualizedContainer);
  856. expect(rows[0]).toHaveFocus();
  857. });
  858. });
  859. it('scrolls to transaction', async () => {
  860. mockQueryString('?node=txn-1');
  861. const {virtualizedContainer} = await completeTestSetup();
  862. await waitFor(() => {
  863. const rows = getVirtualizedRows(virtualizedContainer);
  864. expect(rows[2]).toHaveFocus();
  865. });
  866. });
  867. it('scrolls to span that is a child of transaction', async () => {
  868. mockQueryString('?node=span-span0&node=txn-1');
  869. const {virtualizedContainer} = await completeTestSetup();
  870. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  871. // We need to await a tick because the row is not focused until the next tick
  872. const rows = getVirtualizedRows(virtualizedContainer);
  873. await waitFor(() => {
  874. expect(rows[3]).toHaveFocus();
  875. });
  876. expect(rows[3]!.textContent?.includes('http — request')).toBe(true);
  877. });
  878. it('scrolls to parent autogroup node', async () => {
  879. mockQueryString('?node=ag-redis0&node=txn-1');
  880. const {virtualizedContainer} = await completeTestSetup();
  881. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  882. // We need to await a tick because the row is not focused until the next tick
  883. const rows = getVirtualizedRows(virtualizedContainer);
  884. await waitFor(() => {
  885. expect(rows[4]).toHaveFocus();
  886. });
  887. expect(rows[4]!.textContent?.includes('Autogrouped')).toBe(true);
  888. });
  889. it('scrolls to child of parent autogroup node', async () => {
  890. mockQueryString('?node=span-redis0&node=txn-1');
  891. const {virtualizedContainer} = await completeTestSetup();
  892. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  893. // We need to await a tick because the row is not focused until the next tick
  894. const rows = getVirtualizedRows(virtualizedContainer);
  895. await waitFor(() => {
  896. expect(rows[5]).toHaveFocus();
  897. });
  898. expect(rows[5]!.textContent?.includes('db — redis')).toBe(true);
  899. });
  900. it('scrolls to sibling autogroup node', async () => {
  901. mockQueryString('?node=ag-http0&node=txn-1');
  902. const {virtualizedContainer} = await completeTestSetup();
  903. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  904. // We need to await a tick because the row is not focused until the next tick
  905. const rows = getVirtualizedRows(virtualizedContainer);
  906. await waitFor(() => {
  907. expect(rows[5]).toHaveFocus();
  908. });
  909. expect(rows[5]!.textContent?.includes('5Autogrouped')).toBe(true);
  910. });
  911. it('scrolls to child of sibling autogroup node', async () => {
  912. mockQueryString('?node=span-http0&node=txn-1');
  913. const {virtualizedContainer} = await completeTestSetup();
  914. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  915. // We need to await a tick because the row is not focused until the next tick
  916. const rows = getVirtualizedRows(virtualizedContainer);
  917. await waitFor(() => {
  918. expect(rows[6]).toHaveFocus();
  919. });
  920. expect(rows[6]!.textContent?.includes('http — request')).toBe(true);
  921. });
  922. it('scrolls to missing instrumentation node', async () => {
  923. mockQueryString('?node=ms-queueprocess0&node=txn-1');
  924. const {virtualizedContainer} = await completeTestSetup();
  925. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  926. // We need to await a tick because the row is not focused until the next ticks
  927. const rows = getVirtualizedRows(virtualizedContainer);
  928. await waitFor(() => {
  929. expect(rows[7]).toHaveFocus();
  930. });
  931. expect(rows[7]!.textContent?.includes('No Instrumentation')).toBe(true);
  932. });
  933. it('scrolls to trace error node', async () => {
  934. mockQueryString('?node=error-error0&node=txn-1');
  935. const {virtualizedContainer} = await completeTestSetup();
  936. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  937. // We need to await a tick because the row is not focused until the next ticks
  938. const rows = getVirtualizedRows(virtualizedContainer);
  939. await waitFor(() => {
  940. expect(rows[11]).toHaveFocus();
  941. });
  942. expect(rows[11]!.textContent?.includes('error-title')).toBe(true);
  943. });
  944. it('scrolls to event id query param', async () => {
  945. mockQueryString('?eventId=1');
  946. const {virtualizedContainer} = await completeTestSetup();
  947. await waitFor(() => {
  948. const rows = getVirtualizedRows(virtualizedContainer);
  949. expect(rows[2]).toHaveFocus();
  950. });
  951. });
  952. it('supports expanded node path', async () => {
  953. mockQueryString('?node=span-span0&node=txn-1&span-0&node=txn-0');
  954. const {virtualizedContainer} = await completeTestSetup();
  955. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  956. const rows = getVirtualizedRows(virtualizedContainer);
  957. await waitFor(() => {
  958. expect(rows[3]).toHaveFocus();
  959. });
  960. expect(rows[3]!.textContent?.includes('http — request')).toBe(true);
  961. });
  962. it.each([
  963. '?eventId=doesnotexist',
  964. '?node=txn-doesnotexist',
  965. // Invalid path
  966. '?node=span-does-notexist',
  967. ])('logs if path is not found: %s', async path => {
  968. mockQueryString(path);
  969. const sentryScopeMock = {
  970. setFingerprint: jest.fn(),
  971. captureMessage: jest.fn(),
  972. } as any;
  973. jest.spyOn(Sentry, 'withScope').mockImplementation((f: any) => f(sentryScopeMock));
  974. await pageloadTestSetup();
  975. await waitFor(() => {
  976. expect(sentryScopeMock.captureMessage).toHaveBeenCalledWith(
  977. 'Failed to scroll to node in trace tree'
  978. );
  979. });
  980. });
  981. it('does not autogroup if user preference is disabled', async () => {
  982. mockTracePreferences({autogroup: {parent: false, sibling: false}});
  983. mockQueryString('?node=span-span0&node=txn-1');
  984. const {virtualizedContainer} = await completeTestSetup();
  985. await within(virtualizedContainer).findAllByText(/process/i);
  986. expect(screen.queryByText(/Autogrouped/i)).not.toBeInTheDocument();
  987. });
  988. it('does not inject missing instrumentation if user preference is disabled', async () => {
  989. mockTracePreferences({missing_instrumentation: false});
  990. mockQueryString('?node=span-span0&node=txn-1');
  991. const {virtualizedContainer} = await completeTestSetup();
  992. await within(virtualizedContainer).findAllByText(/process/i);
  993. expect(screen.queryByText(/Missing instrumentation/i)).not.toBeInTheDocument();
  994. });
  995. describe('preferences', () => {
  996. it('toggles autogrouping', async () => {
  997. mockTracePreferences({autogroup: {parent: true, sibling: true}});
  998. mockQueryString('?node=span-span0&node=txn-1');
  999. const {virtualizedContainer} = await completeTestSetup();
  1000. await within(virtualizedContainer).findAllByText(/Autogrouped/i);
  1001. const preferencesDropdownTrigger = screen.getByLabelText('Trace Preferences');
  1002. await userEvent.click(preferencesDropdownTrigger);
  1003. expect(await screen.findByText('Autogrouping')).toBeInTheDocument();
  1004. // Toggle autogrouping off
  1005. const autogroupingOption = await screen.findByText('Autogrouping');
  1006. await userEvent.click(autogroupingOption);
  1007. await waitFor(() => {
  1008. expect(screen.queryByText('Autogrouped')).not.toBeInTheDocument();
  1009. });
  1010. // Toggle autogrouping back on
  1011. await userEvent.click(await screen.findByText('Autogrouping'));
  1012. expect(await screen.findAllByText('Autogrouped')).toHaveLength(2);
  1013. });
  1014. it('toggles missing instrumentation', async () => {
  1015. mockTracePreferences({missing_instrumentation: true});
  1016. mockQueryString('?node=span-span0&node=txn-1');
  1017. const {virtualizedContainer} = await completeTestSetup();
  1018. await within(virtualizedContainer).findAllByText(/No Instrumentation/i);
  1019. const preferencesDropdownTrigger = screen.getByLabelText('Trace Preferences');
  1020. await userEvent.click(preferencesDropdownTrigger);
  1021. expect(await screen.findAllByText('No Instrumentation')).toHaveLength(2);
  1022. // Toggle autogrouping off
  1023. const autogroupingOption = await screen.findByTestId('no-instrumentation');
  1024. await userEvent.click(autogroupingOption);
  1025. await waitFor(async () => {
  1026. expect(await screen.findAllByText('No Instrumentation')).toHaveLength(1);
  1027. });
  1028. // Toggle autogrouping back on
  1029. await userEvent.click(autogroupingOption);
  1030. await waitFor(async () => {
  1031. expect(await screen.findAllByText('No Instrumentation')).toHaveLength(2);
  1032. });
  1033. });
  1034. });
  1035. });
  1036. describe('keyboard navigation', () => {
  1037. it('arrow down', async () => {
  1038. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1039. const rows = getVirtualizedRows(virtualizedContainer);
  1040. await userEvent.click(rows[0]!);
  1041. await waitFor(() => expect(rows[0]).toHaveFocus());
  1042. await userEvent.keyboard('{arrowdown}');
  1043. await waitFor(() => expect(rows[1]).toHaveFocus());
  1044. });
  1045. it('arrow up', async () => {
  1046. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1047. const rows = getVirtualizedRows(virtualizedContainer);
  1048. await userEvent.click(rows[1]!);
  1049. await waitFor(() => expect(rows[1]).toHaveFocus());
  1050. await userEvent.keyboard('{arrowup}');
  1051. await waitFor(() => expect(rows[0]).toHaveFocus());
  1052. });
  1053. it('arrow right expands row and fetches data', async () => {
  1054. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1055. const rows = getVirtualizedRows(virtualizedContainer);
  1056. mockSpansResponse(
  1057. '0',
  1058. {},
  1059. {
  1060. entries: [
  1061. {type: EntryType.SPANS, data: [makeSpan({span_id: '0', op: 'special-span'})]},
  1062. ],
  1063. }
  1064. );
  1065. await userEvent.click(rows[1]!);
  1066. await waitFor(() => expect(rows[1]).toHaveFocus());
  1067. await userEvent.keyboard('{arrowright}');
  1068. await waitFor(() => {
  1069. expect(screen.getByText('special-span')).toBeInTheDocument();
  1070. });
  1071. });
  1072. it('arrow left collapses row', async () => {
  1073. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1074. const rows = getVirtualizedRows(virtualizedContainer);
  1075. mockSpansResponse(
  1076. '0',
  1077. {},
  1078. {
  1079. entries: [
  1080. {type: EntryType.SPANS, data: [makeSpan({span_id: '0', op: 'special-span'})]},
  1081. ],
  1082. }
  1083. );
  1084. await userEvent.click(rows[1]!);
  1085. await waitFor(() => expect(rows[1]).toHaveFocus());
  1086. await userEvent.keyboard('{arrowright}');
  1087. expect(await screen.findByText('special-span')).toBeInTheDocument();
  1088. await userEvent.keyboard('{arrowleft}');
  1089. await waitFor(() => {
  1090. expect(screen.queryByText('special-span')).not.toBeInTheDocument();
  1091. });
  1092. });
  1093. it('arrow left does not collapse trace root row', async () => {
  1094. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1095. const rows = getVirtualizedRows(virtualizedContainer);
  1096. await userEvent.click(rows[0]!);
  1097. await waitFor(() => expect(rows[0]).toHaveFocus());
  1098. await userEvent.keyboard('{arrowleft}');
  1099. expect(await screen.findByText('transaction-name-1')).toBeInTheDocument();
  1100. });
  1101. it('arrow left on transaction row still renders transaction children', async () => {
  1102. const {virtualizedContainer} = await nestedTransactionsTestSetup();
  1103. const rows = getVirtualizedRows(virtualizedContainer);
  1104. await userEvent.click(rows[1]!);
  1105. await waitFor(() => expect(rows[1]).toHaveFocus());
  1106. await userEvent.keyboard('{arrowleft}');
  1107. expect(await screen.findByText('transaction-name-2')).toBeInTheDocument();
  1108. });
  1109. it('roving updates the element in the drawer', async () => {
  1110. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1111. const rows = getVirtualizedRows(virtualizedContainer);
  1112. mockSpansResponse(
  1113. '0',
  1114. {},
  1115. {
  1116. entries: [
  1117. {type: EntryType.SPANS, data: [makeSpan({span_id: '0', op: 'special-span'})]},
  1118. ],
  1119. }
  1120. );
  1121. await userEvent.click(rows[1]!);
  1122. await waitFor(() => expect(rows[1]).toHaveFocus());
  1123. expect(await screen.findByTestId('trace-drawer-title')).toHaveTextContent(
  1124. 'transaction-op-0'
  1125. );
  1126. await userEvent.keyboard('{arrowright}');
  1127. expect(await screen.findByText('special-span')).toBeInTheDocument();
  1128. await userEvent.keyboard('{arrowdown}');
  1129. await waitFor(() => {
  1130. const updatedRows = virtualizedContainer.querySelectorAll(
  1131. VISIBLE_TRACE_ROW_SELECTOR
  1132. );
  1133. expect(updatedRows[2]).toHaveFocus();
  1134. });
  1135. expect(await screen.findByTestId('trace-drawer-title')).toHaveTextContent(
  1136. 'special-span'
  1137. );
  1138. });
  1139. it('arrowup on first node jumps to end', async () => {
  1140. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1141. let rows = getVirtualizedRows(virtualizedContainer);
  1142. await userEvent.click(rows[0]!);
  1143. await waitFor(() => expect(rows[0]).toHaveFocus());
  1144. await userEvent.keyboard('{arrowup}');
  1145. expect(
  1146. await within(virtualizedContainer).findByText(/transaction-op-99/i)
  1147. ).toBeInTheDocument();
  1148. await waitFor(() => {
  1149. rows = getVirtualizedRows(virtualizedContainer);
  1150. expect(rows[rows.length - 1]).toHaveFocus();
  1151. });
  1152. });
  1153. it('arrowdown on last node jumps to start', async () => {
  1154. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1155. let rows = getVirtualizedRows(virtualizedContainer);
  1156. await userEvent.click(rows[0]!);
  1157. await waitFor(() => expect(rows[0]).toHaveFocus());
  1158. await userEvent.keyboard('{arrowup}');
  1159. await waitFor(() => {
  1160. rows = getVirtualizedRows(virtualizedContainer);
  1161. expect(rows[rows.length - 1]).toHaveFocus();
  1162. });
  1163. expect(
  1164. await within(virtualizedContainer).findByText(/transaction-op-99/i)
  1165. ).toBeInTheDocument();
  1166. await userEvent.keyboard('{arrowdown}');
  1167. await waitFor(() => {
  1168. rows = getVirtualizedRows(virtualizedContainer);
  1169. expect(rows[0]).toHaveFocus();
  1170. });
  1171. expect(
  1172. await within(virtualizedContainer).findByText(/transaction-op-0/i)
  1173. ).toBeInTheDocument();
  1174. });
  1175. it('tab scrolls to next node', async () => {
  1176. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1177. let rows = getVirtualizedRows(virtualizedContainer);
  1178. await userEvent.click(rows[0]!);
  1179. await waitFor(() => expect(rows[0]).toHaveFocus());
  1180. await userEvent.keyboard('{tab}');
  1181. await waitFor(() => {
  1182. rows = getVirtualizedRows(virtualizedContainer);
  1183. expect(rows[1]).toHaveFocus();
  1184. });
  1185. });
  1186. it('shift+tab scrolls to previous node', async () => {
  1187. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1188. let rows = getVirtualizedRows(virtualizedContainer);
  1189. await userEvent.click(rows[1]!);
  1190. await waitFor(() => {
  1191. rows = getVirtualizedRows(virtualizedContainer);
  1192. expect(rows[1]).toHaveFocus();
  1193. });
  1194. await userEvent.keyboard('{Shift>}{tab}{/Shift}');
  1195. await waitFor(() => {
  1196. rows = getVirtualizedRows(virtualizedContainer);
  1197. expect(rows[0]).toHaveFocus();
  1198. });
  1199. });
  1200. it('arrowdown+shift scrolls to the end of the list', async () => {
  1201. const {container, virtualizedContainer} = await keyboardNavigationTestSetup();
  1202. let rows = container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR);
  1203. await userEvent.click(rows[0]!);
  1204. await waitFor(() => {
  1205. rows = container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR);
  1206. expect(rows[0]).toHaveFocus();
  1207. });
  1208. await userEvent.keyboard('{Shift>}{arrowdown}{/Shift}');
  1209. expect(
  1210. await within(virtualizedContainer).findByText(/transaction-op-99/i)
  1211. ).toBeInTheDocument();
  1212. await waitFor(() => {
  1213. rows = container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR);
  1214. expect(rows[rows.length - 1]).toHaveFocus();
  1215. });
  1216. });
  1217. it('arrowup+shift scrolls to the start of the list', async () => {
  1218. const {virtualizedContainer} = await keyboardNavigationTestSetup();
  1219. let rows = getVirtualizedRows(virtualizedContainer);
  1220. await userEvent.click(rows[1]!);
  1221. await waitFor(() => {
  1222. rows = getVirtualizedRows(virtualizedContainer);
  1223. expect(rows[1]).toHaveFocus();
  1224. });
  1225. await userEvent.keyboard('{Shift>}{arrowdown}{/Shift}');
  1226. expect(
  1227. await within(virtualizedContainer).findByText(/transaction-op-99/i)
  1228. ).toBeInTheDocument();
  1229. await waitFor(() => {
  1230. rows = getVirtualizedRows(virtualizedContainer);
  1231. expect(rows[rows.length - 1]).toHaveFocus();
  1232. });
  1233. await userEvent.keyboard('{Shift>}{arrowup}{/Shift}');
  1234. expect(
  1235. await within(virtualizedContainer).findByText(/transaction-op-0/i)
  1236. ).toBeInTheDocument();
  1237. await waitFor(() => {
  1238. rows = getVirtualizedRows(virtualizedContainer);
  1239. expect(rows[0]).toHaveFocus();
  1240. });
  1241. });
  1242. });
  1243. describe('search', () => {
  1244. it('triggers search on load but does not steal focus from node param', async () => {
  1245. mockQueryString('?search=transaction-op-99&node=txn-0');
  1246. const {virtualizedContainer} = await pageloadTestSetup();
  1247. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1248. expect(searchInput).toHaveValue('transaction-op-99');
  1249. await waitFor(() => {
  1250. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1251. '-/1'
  1252. );
  1253. });
  1254. const rows = getVirtualizedRows(virtualizedContainer);
  1255. expect(rows[1]).toHaveFocus();
  1256. });
  1257. it('if search on load does not match anything, it does not steal focus or highlight first result', async () => {
  1258. mockQueryString('?search=dead&node=txn-5');
  1259. const {container} = await pageloadTestSetup();
  1260. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1261. expect(searchInput).toHaveValue('dead');
  1262. await waitFor(() => {
  1263. expect(screen.getByTestId('trace-search-result-iterator')).toHaveTextContent(
  1264. 'no results'
  1265. );
  1266. });
  1267. await waitFor(() => {
  1268. const rows = container.querySelectorAll(VISIBLE_TRACE_ROW_SELECTOR);
  1269. expect(rows[6]).toHaveFocus();
  1270. });
  1271. });
  1272. it('searches in transaction', async () => {
  1273. const {container} = await searchTestSetup();
  1274. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1275. await userEvent.type(searchInput, 'transaction-op');
  1276. expect(searchInput).toHaveValue('transaction-op');
  1277. await searchToResolve();
  1278. await assertHighlightedRowAtIndex(container, 1);
  1279. });
  1280. it('supports roving with arrowup and arrowdown', async () => {
  1281. const {container} = await searchTestSetup();
  1282. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1283. await userEvent.type(searchInput, 'transaction-op');
  1284. expect(searchInput).toHaveValue('transaction-op');
  1285. await searchToResolve();
  1286. for (const action of [
  1287. // starting at the top, jump bottom with shift+arrowdown
  1288. ['{Shift>}{arrowdown}{/Shift}', 11],
  1289. // move to row above with arrowup
  1290. ['{arrowup}', 10],
  1291. // and jump back to top with shift+arrowup
  1292. ['{Shift>}{arrowup}{/Shift}', 1],
  1293. // and jump to next row with arrowdown
  1294. ['{arrowdown}', 2],
  1295. ] as const) {
  1296. await userEvent.keyboard(action[0]);
  1297. await assertHighlightedRowAtIndex(container, action[1]);
  1298. }
  1299. });
  1300. it('search roving updates the element in the drawer', async () => {
  1301. await searchTestSetup();
  1302. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1303. await userEvent.type(searchInput, 'transaction-op');
  1304. expect(searchInput).toHaveValue('transaction-op');
  1305. // Wait for the search results to resolve
  1306. await searchToResolve();
  1307. expect(await screen.findByTestId('trace-drawer-title')).toHaveTextContent(
  1308. 'transaction-op-0'
  1309. );
  1310. // assert that focus on search input is never lost
  1311. expect(searchInput).toHaveFocus();
  1312. await userEvent.keyboard('{arrowdown}');
  1313. await waitFor(() => {
  1314. expect(screen.getByTestId('trace-drawer-title')).toHaveTextContent(
  1315. 'transaction-op-1'
  1316. );
  1317. });
  1318. });
  1319. it('highlighted node narrows down on the first result', async () => {
  1320. const {container} = await searchTestSetup();
  1321. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1322. await userEvent.type(searchInput, 'transaction-op-1');
  1323. expect(searchInput).toHaveValue('transaction-op-1');
  1324. await searchToResolve();
  1325. await assertHighlightedRowAtIndex(container, 2);
  1326. await userEvent.clear(searchInput);
  1327. await userEvent.type(searchInput, 'transaction-op-5');
  1328. await searchToResolve();
  1329. await assertHighlightedRowAtIndex(container, 6);
  1330. });
  1331. it('highlighted is persisted on node while it is part of the search results', async () => {
  1332. const {container} = await searchTestSetup();
  1333. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1334. await userEvent.type(searchInput, 'trans');
  1335. expect(searchInput).toHaveValue('trans');
  1336. // Wait for the search results to resolve
  1337. await searchToResolve();
  1338. await userEvent.keyboard('{arrowdown}');
  1339. await searchToResolve();
  1340. await assertHighlightedRowAtIndex(container, 2);
  1341. await userEvent.type(searchInput, 'act');
  1342. expect(searchInput).toHaveValue('transact');
  1343. await searchToResolve();
  1344. // Highlighting is persisted on the row
  1345. await assertHighlightedRowAtIndex(container, 2);
  1346. await userEvent.clear(searchInput);
  1347. await userEvent.type(searchInput, 'this wont match anything');
  1348. expect(searchInput).toHaveValue('this wont match anything');
  1349. await searchToResolve();
  1350. // When there is no match, the highlighting is removed
  1351. expect(container.querySelectorAll('.TraceRow.Highlight')).toHaveLength(0);
  1352. });
  1353. it('auto highlights the first result when search begins', async () => {
  1354. const {container} = await searchTestSetup();
  1355. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1356. // Nothing is highlighted
  1357. expect(container.querySelectorAll('.TraceRow.Highlight')).toHaveLength(0);
  1358. await userEvent.type(searchInput, 't');
  1359. expect(searchInput).toHaveValue('t');
  1360. // Wait for the search results to resolve
  1361. await searchToResolve();
  1362. await assertHighlightedRowAtIndex(container, 1);
  1363. });
  1364. it('clicking a row that is also a search result updates the result index', async () => {
  1365. const {container, virtualizedContainer} = await searchTestSetup();
  1366. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1367. await userEvent.type(searchInput, 'transaction-op-1');
  1368. expect(searchInput).toHaveValue('transaction-op-1');
  1369. await searchToResolve();
  1370. await assertHighlightedRowAtIndex(container, 2);
  1371. const rows = getVirtualizedRows(virtualizedContainer);
  1372. // By default, we highlight the first result
  1373. expect(await screen.findByTestId('trace-search-result-iterator')).toHaveTextContent(
  1374. '1/2'
  1375. );
  1376. // Click on a random row in the list that is not a search result
  1377. await userEvent.click(rows[5]!);
  1378. await waitFor(() => {
  1379. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1380. '-/2'
  1381. );
  1382. });
  1383. // Click on a the row in the list that is a search result
  1384. await userEvent.click(rows[2]!);
  1385. await waitFor(() => {
  1386. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1387. '1/2'
  1388. );
  1389. });
  1390. });
  1391. it('during search, expanding a row retriggers search', async () => {
  1392. mockPerformanceSubscriptionDetailsResponse();
  1393. mockTraceRootFacets();
  1394. mockTraceRootEvent('0');
  1395. mockTraceEventDetails();
  1396. mockMetricsResponse();
  1397. mockEventsResponse();
  1398. mockTraceResponse({
  1399. body: {
  1400. transactions: [
  1401. makeTransaction({
  1402. span_id: '0',
  1403. event_id: '0',
  1404. transaction: 'transaction-name-0',
  1405. 'transaction.op': 'transaction-op-0',
  1406. project_slug: 'project_slug',
  1407. }),
  1408. makeTransaction({
  1409. span_id: '1',
  1410. event_id: '1',
  1411. transaction: 'transaction-name-1',
  1412. 'transaction.op': 'transaction-op-1',
  1413. project_slug: 'project_slug',
  1414. }),
  1415. makeTransaction({
  1416. span_id: '2',
  1417. event_id: '2',
  1418. transaction: 'transaction-name-2',
  1419. 'transaction.op': 'transaction-op-2',
  1420. project_slug: 'project_slug',
  1421. }),
  1422. makeTransaction({
  1423. span_id: '3',
  1424. event_id: '3',
  1425. transaction: 'transaction-name-3',
  1426. 'transaction.op': 'transaction-op-3',
  1427. project_slug: 'project_slug',
  1428. }),
  1429. ],
  1430. orphan_errors: [],
  1431. },
  1432. });
  1433. mockTraceMetaResponse({
  1434. body: {
  1435. errors: 0,
  1436. performance_issues: 0,
  1437. projects: 0,
  1438. transactions: 0,
  1439. transaction_child_count_map: [
  1440. {
  1441. 'transaction.id': '0',
  1442. count: 5,
  1443. },
  1444. {
  1445. 'transaction.id': '1',
  1446. count: 5,
  1447. },
  1448. {
  1449. 'transaction.id': '2',
  1450. count: 5,
  1451. },
  1452. {
  1453. 'transaction.id': '3',
  1454. count: 5,
  1455. },
  1456. ],
  1457. },
  1458. });
  1459. const spansRequest = mockSpansResponse(
  1460. '0',
  1461. {},
  1462. {
  1463. entries: [
  1464. {
  1465. type: EntryType.SPANS,
  1466. data: [
  1467. makeSpan({span_id: '0', description: 'span-description', op: 'op-0'}),
  1468. ],
  1469. },
  1470. ],
  1471. }
  1472. );
  1473. const {container} = render(<TraceView />, {router});
  1474. // Awaits for the placeholder rendering rows to be removed
  1475. await within(container).findByText(/transaction-op-0/i);
  1476. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1477. await userEvent.type(searchInput, 'op-0');
  1478. expect(searchInput).toHaveValue('op-0');
  1479. await searchToResolve();
  1480. await waitFor(() => {
  1481. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1482. '1/1'
  1483. );
  1484. });
  1485. const open = await screen.findAllByRole('button', {name: '+'});
  1486. await userEvent.click(open[0]!);
  1487. await waitFor(() => {
  1488. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1489. '1/1'
  1490. );
  1491. });
  1492. expect(await screen.findByText('span-description')).toBeInTheDocument();
  1493. expect(spansRequest).toHaveBeenCalled();
  1494. await waitFor(() => {
  1495. expect(screen.queryByTestId('trace-search-result-iterator')).toHaveTextContent(
  1496. '1/2'
  1497. );
  1498. });
  1499. });
  1500. it('during search, highlighting is persisted on the row', async () => {
  1501. const {container} = await searchTestSetup();
  1502. const searchInput = await screen.findByPlaceholderText('Search in trace');
  1503. await userEvent.type(searchInput, 'transaction-op');
  1504. expect(searchInput).toHaveValue('transaction-op');
  1505. await searchToResolve();
  1506. await assertHighlightedRowAtIndex(container, 1);
  1507. // User moves down the list using keyboard navigation
  1508. for (let i = 1; i < 6; i++) {
  1509. await userEvent.keyboard('{arrowDown}');
  1510. await assertHighlightedRowAtIndex(container, 1 + i);
  1511. }
  1512. // User clicks on an entry in the list, then proceeds to search
  1513. await waitFor(() => {
  1514. expect(screen.getByTestId('trace-search-result-iterator')).toHaveTextContent(
  1515. '6/11'
  1516. );
  1517. });
  1518. // And then continues the query - the highlighting is preserved as long as the
  1519. // row is part of the search results
  1520. await assertHighlightedRowAtIndex(container, 6);
  1521. await userEvent.type(searchInput, '-5');
  1522. expect(searchInput).toHaveValue('transaction-op-5');
  1523. await searchToResolve();
  1524. await assertHighlightedRowAtIndex(container, 6);
  1525. await userEvent.clear(searchInput);
  1526. await userEvent.type(searchInput, 'transaction-op-none');
  1527. await searchToResolve();
  1528. // eslint-disable-next-line testing-library/no-container
  1529. expect(container.querySelectorAll('.TraceRow.Highlight')).toHaveLength(0);
  1530. });
  1531. });
  1532. describe('tabbing', () => {
  1533. it('clicking on a node spawns a new tab when none is selected', async () => {
  1534. const {virtualizedContainer} = await simpleTestSetup();
  1535. const rows = getVirtualizedRows(virtualizedContainer);
  1536. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(1);
  1537. await userEvent.click(rows[5]!);
  1538. await waitFor(() => {
  1539. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1540. });
  1541. });
  1542. it('clicking on a node replaces the previously selected tab', async () => {
  1543. const {virtualizedContainer} = await simpleTestSetup();
  1544. const rows = getVirtualizedRows(virtualizedContainer);
  1545. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(1);
  1546. await userEvent.click(rows[5]!);
  1547. await waitFor(() => {
  1548. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1549. });
  1550. expect(
  1551. screen
  1552. .getAllByTestId(DRAWER_TABS_TEST_ID)[1]!
  1553. .textContent?.includes('transaction-op-4')
  1554. ).toBeTruthy();
  1555. await userEvent.click(rows[7]!);
  1556. await waitFor(() => {
  1557. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1558. });
  1559. await waitFor(() => {
  1560. expect(
  1561. screen
  1562. .getAllByTestId(DRAWER_TABS_TEST_ID)[1]!
  1563. .textContent?.includes('transaction-op-6')
  1564. ).toBeTruthy();
  1565. });
  1566. });
  1567. it('pinning a tab and clicking on a new node spawns a new tab', async () => {
  1568. const {virtualizedContainer} = await simpleTestSetup();
  1569. const rows = getVirtualizedRows(virtualizedContainer);
  1570. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(1);
  1571. await userEvent.click(rows[5]!);
  1572. await waitFor(() => {
  1573. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1574. });
  1575. await userEvent.click(await screen.findByTestId(DRAWER_TABS_PIN_BUTTON_TEST_ID));
  1576. await userEvent.click(rows[7]!);
  1577. await waitFor(() => {
  1578. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(3);
  1579. });
  1580. expect(
  1581. screen
  1582. .getAllByTestId(DRAWER_TABS_TEST_ID)[1]!
  1583. .textContent?.includes('transaction-op-4')
  1584. ).toBeTruthy();
  1585. expect(
  1586. screen
  1587. .getAllByTestId(DRAWER_TABS_TEST_ID)[2]!
  1588. .textContent?.includes('transaction-op-6')
  1589. ).toBeTruthy();
  1590. });
  1591. it('unpinning a tab removes it', async () => {
  1592. const {virtualizedContainer} = await simpleTestSetup();
  1593. const rows = getVirtualizedRows(virtualizedContainer);
  1594. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(1);
  1595. await userEvent.click(rows[5]!);
  1596. await waitFor(() => {
  1597. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1598. });
  1599. await userEvent.click(await screen.findByTestId(DRAWER_TABS_PIN_BUTTON_TEST_ID));
  1600. await userEvent.click(rows[7]!);
  1601. await waitFor(() => {
  1602. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(3);
  1603. });
  1604. const tabButtons = screen.queryAllByTestId(DRAWER_TABS_PIN_BUTTON_TEST_ID);
  1605. expect(tabButtons).toHaveLength(2);
  1606. await userEvent.click(tabButtons[0]!);
  1607. await waitFor(() => {
  1608. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1609. });
  1610. });
  1611. it('clicking a node that is already open in a tab switches to that tab and persists the previous node', async () => {
  1612. const {virtualizedContainer} = await simpleTestSetup();
  1613. const rows = getVirtualizedRows(virtualizedContainer);
  1614. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(1);
  1615. await userEvent.click(rows[5]!);
  1616. await waitFor(() => {
  1617. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(2);
  1618. });
  1619. await userEvent.click(await screen.findByTestId(DRAWER_TABS_PIN_BUTTON_TEST_ID));
  1620. await userEvent.click(rows[7]!);
  1621. await waitFor(() => {
  1622. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(3);
  1623. });
  1624. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)[2]).toHaveAttribute(
  1625. 'aria-selected',
  1626. 'true'
  1627. );
  1628. await userEvent.click(rows[5]!);
  1629. await waitFor(() => {
  1630. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)[1]).toHaveAttribute(
  1631. 'aria-selected',
  1632. 'true'
  1633. );
  1634. });
  1635. expect(screen.queryAllByTestId(DRAWER_TABS_TEST_ID)).toHaveLength(3);
  1636. });
  1637. });
  1638. });