@@ -4,14 +4,15 @@ import { Hub } from '@sentry/types';
4
4
5
5
import { instrumentAngularRouting , TraceClassDecorator , TraceDirective , TraceMethodDecorator } from '../src' ;
6
6
import { getParameterizedRouteFromSnapshot } from '../src/tracing' ;
7
- import { AppComponent , TestEnv , TestViewContainerRef } from './utils/index' ;
7
+ import { AppComponent , TestEnv } from './utils/index' ;
8
8
9
9
let transaction : any ;
10
10
11
11
const defaultStartTransaction = ( ctx : any ) => {
12
12
transaction = {
13
13
...ctx ,
14
14
setName : jest . fn ( name => ( transaction . name = name ) ) ,
15
+ startChild : jest . fn ( ) ,
15
16
} ;
16
17
17
18
return transaction ;
@@ -260,67 +261,76 @@ describe('Angular Tracing', () => {
260
261
expect ( directive ) . toBeTruthy ( ) ;
261
262
} ) ;
262
263
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 ) ;
273
268
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 ) ;
281
270
282
271
const env = await TestEnv . setup ( {
283
- components : [ AppComponent , ChildComponent , TraceDirective ] ,
284
- defaultComponent : AppComponent ,
272
+ components : [ TraceDirective ] ,
285
273
customStartTransaction,
286
274
useTraceService : false ,
287
275
} ) ;
288
276
289
277
transaction . startChild = jest . fn ( ) ;
290
278
291
- // directive.ngOnInit();
279
+ directive . ngOnInit ( ) ;
292
280
293
281
expect ( transaction . startChild ) . toHaveBeenCalledWith ( {
294
282
op : 'ui.angular.init' ,
295
- description : '<unknown >' ,
283
+ description : '<app-test >' ,
296
284
} ) ;
297
285
298
286
env . destroy ( ) ;
299
287
} ) ;
300
288
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 ) ;
304
308
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
+ } ) ;
310
314
311
- transaction . startChild = jest . fn ( ) ;
315
+ const finishSpan = jest . fn ( ) ;
316
+ transaction . startChild = jest . fn ( ) . mockReturnValue ( { finish : finishSpan } ) ;
317
+ transaction . finish = jest . fn ( ) ;
312
318
313
- // directive.ngOnInit();
319
+ directive . ngOnInit ( ) ;
314
320
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
+ } ) ;
319
325
320
- env . destroy ( ) ;
321
- } ) ;
326
+ directive . ngAfterViewInit ( ) ;
327
+ expect ( finishSpan ) . toHaveBeenCalledTimes ( 1 ) ;
322
328
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 ( ) => {
324
334
const directive = new TraceDirective ( { } as unknown as any ) ;
325
335
const finishMock = jest . fn ( ) ;
326
336
const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
@@ -347,8 +357,7 @@ describe('Angular Tracing', () => {
347
357
} ) ;
348
358
349
359
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 ) ;
352
361
const finishMock = jest . fn ( ) ;
353
362
const customStartTransaction = jest . fn ( defaultStartTransaction ) ;
354
363
0 commit comments