@@ -8,7 +8,7 @@ import { captureObservable, scheduler, setupNavigationWarnStub } from './util';
88@Component ( {
99 template : `<input type="text" [queryParam]="param" />` ,
1010} )
11- class BasicTestComponent {
11+ class StaticStandaloneExampleComponent {
1212
1313 public param : QueryParam < string > ;
1414
@@ -18,9 +18,23 @@ class BasicTestComponent {
1818
1919}
2020
21+ @Component ( {
22+ template : `<input type="text" [queryParam]="bound ? param : null" />` ,
23+ } )
24+ class ToggleStandaloneExampleComponent {
25+
26+ public param : QueryParam < string > ;
27+ public bound = false ;
28+
29+ constructor ( private qpb : QueryParamBuilder ) {
30+ this . param = qpb . stringParam ( 'q' ) ;
31+ }
32+
33+ }
34+
2135describe ( 'ngqp standalone parameters' , ( ) => {
22- let fixture : ComponentFixture < BasicTestComponent > ;
23- let component : BasicTestComponent ;
36+ let fixture : ComponentFixture < StaticStandaloneExampleComponent > ;
37+ let component : StaticStandaloneExampleComponent ;
2438 let input : HTMLInputElement ;
2539 let router : Router ;
2640
@@ -33,7 +47,7 @@ describe('ngqp standalone parameters', () => {
3347 QueryParamModule ,
3448 ] ,
3549 declarations : [
36- BasicTestComponent ,
50+ StaticStandaloneExampleComponent ,
3751 ] ,
3852 } ) ;
3953
@@ -43,7 +57,7 @@ describe('ngqp standalone parameters', () => {
4357 } ) ) ;
4458
4559 beforeEach ( ( ) => {
46- fixture = TestBed . createComponent ( BasicTestComponent ) ;
60+ fixture = TestBed . createComponent ( StaticStandaloneExampleComponent ) ;
4761 component = fixture . componentInstance ;
4862 fixture . detectChanges ( ) ;
4963 input = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'input' ) as HTMLInputElement ;
@@ -115,4 +129,78 @@ describe('ngqp standalone parameters', () => {
115129 expectObservable ( value$ ) . toBe ( '' ) ;
116130 } ) ;
117131 } ) ) ;
132+ } ) ;
133+
134+ describe ( 'Standalone parameters' , ( ) => {
135+ let fixture : ComponentFixture < ToggleStandaloneExampleComponent > ;
136+ let component : ToggleStandaloneExampleComponent ;
137+ let input : HTMLInputElement ;
138+ let router : Router ;
139+
140+ beforeEach ( ( ) => setupNavigationWarnStub ( ) ) ;
141+
142+ beforeEach ( async ( ( ) => {
143+ TestBed . configureTestingModule ( {
144+ imports : [
145+ RouterTestingModule . withRoutes ( [ ] ) ,
146+ QueryParamModule ,
147+ ] ,
148+ declarations : [
149+ ToggleStandaloneExampleComponent ,
150+ ] ,
151+ } ) ;
152+
153+ router = TestBed . get ( Router ) ;
154+ TestBed . compileComponents ( ) ;
155+ router . initialNavigation ( ) ;
156+ } ) ) ;
157+
158+ beforeEach ( ( ) => {
159+ fixture = TestBed . createComponent ( ToggleStandaloneExampleComponent ) ;
160+ component = fixture . componentInstance ;
161+ fixture . detectChanges ( ) ;
162+ input = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'input' ) as HTMLInputElement ;
163+ fixture . detectChanges ( ) ;
164+ } ) ;
165+
166+ it ( 'can be bound at runtime' , fakeAsync ( ( ) => {
167+ component . bound = true ;
168+ fixture . detectChanges ( ) ;
169+
170+ input . value = 'Test' ;
171+ input . dispatchEvent ( new Event ( 'input' ) ) ;
172+ tick ( ) ;
173+
174+ expect ( router . url ) . toBe ( `/?q=Test` ) ;
175+ } ) ) ;
176+
177+ it ( 'can be unbound at runtime' , fakeAsync ( ( ) => {
178+ component . bound = true ;
179+ fixture . detectChanges ( ) ;
180+
181+ router . navigateByUrl ( `/?q=One` ) ;
182+ tick ( ) ;
183+ expect ( input . value ) . toBe ( 'One' ) ;
184+
185+ component . bound = false ;
186+ fixture . detectChanges ( ) ;
187+
188+ router . navigateByUrl ( `/?q=Two` ) ;
189+ tick ( ) ;
190+ expect ( input . value ) . toBe ( 'One' ) ;
191+ } ) ) ;
192+
193+ it ( 'synchronizes with the URL upon being bound' , fakeAsync ( ( ) => {
194+ scheduler . run ( ( { expectObservable } ) => {
195+ router . navigateByUrl ( `/?q=One` ) ;
196+ tick ( ) ;
197+
198+ const paramValueChanges$ = captureObservable ( component . param . valueChanges ) ;
199+
200+ component . bound = true ;
201+ fixture . detectChanges ( ) ;
202+
203+ expectObservable ( paramValueChanges$ ) . toBe ( 'a' , { a : 'One' } ) ;
204+ } ) ;
205+ } ) ) ;
118206} ) ;
0 commit comments