@@ -4,14 +4,15 @@ import { Hub } from '@sentry/types';
44
55import { instrumentAngularRouting , TraceClassDecorator , TraceDirective , TraceMethodDecorator } from '../src' ;
66import { getParameterizedRouteFromSnapshot } from '../src/tracing' ;
7- import { AppComponent , TestEnv , TestViewContainerRef } from './utils/index' ;
7+ import { AppComponent , TestEnv } from './utils/index' ;
88
99let transaction : any ;
1010
1111const defaultStartTransaction = ( ctx : any ) => {
1212 transaction = {
1313 ...ctx ,
1414 setName : jest . fn ( name => ( transaction . name = name ) ) ,
15+ startChild : jest . fn ( ) ,
1516 } ;
1617
1718 return transaction ;
@@ -260,67 +261,76 @@ describe('Angular Tracing', () => {
260261 expect ( directive ) . toBeTruthy ( ) ;
261262 } ) ;
262263
263- it ( 'should create a child tracingSpan on init (WIP)' , async ( ) => {
264- const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
265-
266- @Component ( {
267- selector : 'app-component' ,
268- template : '<app-child trace></app-child>' ,
269- } )
270- class AppComponent {
271- public constructor ( ) { }
272- }
264+ it ( "should auto detect the component's selector." , async ( ) => {
265+ const directive = new TraceDirective ( {
266+ _lContainer : [ [ { localName : 'app-test' } ] ] ,
267+ } as unknown as any ) ;
273268
274- @Component ( {
275- selector : 'app-child' ,
276- template : '<p>Hi</p>' ,
277- } )
278- class ChildComponent {
279- public constructor ( ) { }
280- }
269+ const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
281270
282271 const env = await TestEnv . setup ( {
283- components : [ AppComponent , ChildComponent , TraceDirective ] ,
284- defaultComponent : AppComponent ,
272+ components : [ TraceDirective ] ,
285273 customStartTransaction,
286274 useTraceService : false ,
287275 } ) ;
288276
289277 transaction . startChild = jest . fn ( ) ;
290278
291- // directive.ngOnInit();
279+ directive . ngOnInit ( ) ;
292280
293281 expect ( transaction . startChild ) . toHaveBeenCalledWith ( {
294282 op : 'ui.angular.init' ,
295- description : '<unknown >' ,
283+ description : '<app-test >' ,
296284 } ) ;
297285
298286 env . destroy ( ) ;
299287 } ) ;
300288
301- it ( 'should create a child tracingSpan on init' , async ( ) => {
302- // const directive = new TraceDirective({} as unknown as any);
303- const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
289+ it . each ( [
290+ { } ,
291+ {
292+ _lContainer : [ ] ,
293+ } ,
294+ {
295+ _lContainer : [ [ ] ] ,
296+ } ,
297+ {
298+ _lContainer : [ [ { } ] ] ,
299+ } ,
300+ {
301+ _lContainer : [ [ { localName : undefined } ] ] ,
302+ } ,
303+ ] ) (
304+ "should fall back to the default component name if auto-detection doesn't work and no custom name is given" ,
305+ async ( containerViewRef : any ) => {
306+ const directive = new TraceDirective ( containerViewRef ) ;
307+ const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
304308
305- const env = await TestEnv . setup ( {
306- components : [ TraceDirective ] ,
307- customStartTransaction,
308- useTraceService : false ,
309- } ) ;
309+ const env = await TestEnv . setup ( {
310+ components : [ TraceDirective ] ,
311+ customStartTransaction,
312+ useTraceService : false ,
313+ } ) ;
310314
311- transaction . startChild = jest . fn ( ) ;
315+ const finishSpan = jest . fn ( ) ;
316+ transaction . startChild = jest . fn ( ) . mockReturnValue ( { finish : finishSpan } ) ;
317+ transaction . finish = jest . fn ( ) ;
312318
313- // directive.ngOnInit();
319+ directive . ngOnInit ( ) ;
314320
315- expect ( transaction . startChild ) . toHaveBeenCalledWith ( {
316- op : 'ui.angular.init' ,
317- description : '<unknown>' ,
318- } ) ;
321+ expect ( transaction . startChild ) . toHaveBeenCalledWith ( {
322+ op : 'ui.angular.init' ,
323+ description : '<unknown>' ,
324+ } ) ;
319325
320- env . destroy ( ) ;
321- } ) ;
326+ directive . ngAfterViewInit ( ) ;
327+ expect ( finishSpan ) . toHaveBeenCalledTimes ( 1 ) ;
322328
323- it ( 'should use component name as span description' , async ( ) => {
329+ env . destroy ( ) ;
330+ } ,
331+ ) ;
332+
333+ it ( 'should use the custom component name as span description if one is passed' , async ( ) => {
324334 const directive = new TraceDirective ( { } as unknown as any ) ;
325335 const finishMock = jest . fn ( ) ;
326336 const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
@@ -347,8 +357,7 @@ describe('Angular Tracing', () => {
347357 } ) ;
348358
349359 it ( 'should finish tracingSpan after view init' , async ( ) => {
350- // @ts -ignore - we don't need to pass a param here
351- const directive = new TraceDirective ( new TestViewContainerRef ( ) ) ;
360+ const directive = new TraceDirective ( { } as unknown as any ) ;
352361 const finishMock = jest . fn ( ) ;
353362 const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
354363
0 commit comments