trace.spec.tsx 57 KB

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