1
- import { Component } from '@angular/core' ;
1
+ import { Component , numberAttribute } from '@angular/core' ;
2
2
import { TestBed } from '@angular/core/testing' ;
3
3
4
4
import { provideRouter } from '@angular/router' ;
@@ -30,6 +30,34 @@ describe(injectParams.name, () => {
30
30
expect ( instance . userId ( ) ) . toEqual ( 'test' ) ;
31
31
expect ( instance . paramKeysList ( ) ) . toEqual ( [ 'id' ] ) ;
32
32
} ) ;
33
+
34
+ it ( 'returns a signal everytime the route params change based on the param id and transform option' , async ( ) => {
35
+ TestBed . configureTestingModule ( {
36
+ providers : [
37
+ provideRouter ( [
38
+ { path : 'post/:id' , component : PostComponent } ,
39
+ { path : 'post' , component : PostComponent } ,
40
+ ] ) ,
41
+ ] ,
42
+ } ) ;
43
+
44
+ const harness = await RouterTestingHarness . create ( ) ;
45
+
46
+ const instanceNull = await harness . navigateByUrl ( '/post' , PostComponent ) ;
47
+
48
+ expect ( instanceNull . postId ( ) ) . toEqual ( null ) ;
49
+ expect ( instanceNull . postIdDefault ( ) ) . toEqual ( 69 ) ;
50
+
51
+ const instance = await harness . navigateByUrl ( '/post/420' , PostComponent ) ;
52
+
53
+ expect ( instance . postId ( ) ) . toEqual ( 420 ) ;
54
+ expect ( instance . postIdDefault ( ) ) . toEqual ( 420 ) ;
55
+
56
+ await harness . navigateByUrl ( '/post/test' , PostComponent ) ;
57
+
58
+ expect ( instance . postId ( ) ) . toEqual ( NaN ) ;
59
+ expect ( instance . postIdDefault ( ) ) . toEqual ( NaN ) ;
60
+ } ) ;
33
61
} ) ;
34
62
35
63
@Component ( {
@@ -41,3 +69,15 @@ export class UserProfileComponent {
41
69
userId = injectParams ( 'id' ) ;
42
70
paramKeysList = injectParams ( ( params ) => Object . keys ( params ) ) ;
43
71
}
72
+
73
+ @Component ( {
74
+ standalone : true ,
75
+ template : `` ,
76
+ } )
77
+ export class PostComponent {
78
+ postId = injectParams ( 'id' , { transform : numberAttribute } ) ;
79
+ postIdDefault = injectParams ( 'id' , {
80
+ transform : numberAttribute ,
81
+ defaultValue : 69 ,
82
+ } ) ;
83
+ }
0 commit comments