1+ import { Component } from '@angular/core' ;
2+ import { Router } from '@angular/router' ;
3+ import { async , ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
4+ import { RouterTestingModule } from '@angular/router/testing' ;
5+ import { QueryParamBuilder , QueryParam , QueryParamGroup , QueryParamModule } from '../public_api' ;
6+ import { captureObservable , scheduler , setupNavigationWarnStub } from './util' ;
7+
8+ @Component ( {
9+ template : `
10+ <div [queryParamGroup]="paramGroup">
11+ <input *ngIf="showInput" type="text" queryParamName="param" />
12+ </div>
13+ ` ,
14+ } )
15+ class TestComponent {
16+
17+ public paramGroup : QueryParamGroup ;
18+ public param : QueryParam < string > ;
19+ public showInput = true ;
20+
21+ constructor ( private qpb : QueryParamBuilder ) {
22+ this . param = qpb . stringParam ( 'q' ) ;
23+ this . paramGroup = qpb . group ( {
24+ param : this . param ,
25+ } ) ;
26+ }
27+
28+ }
29+
30+ describe ( 'QueryParamGroup#remove' , ( ) => {
31+ let paramGroup : QueryParamGroup ;
32+ let fixture : ComponentFixture < TestComponent > ;
33+ let component : TestComponent ;
34+ let router : Router ;
35+ let qpb : QueryParamBuilder ;
36+
37+ beforeEach ( ( ) => setupNavigationWarnStub ( ) ) ;
38+
39+ beforeEach ( async ( ( ) => {
40+ TestBed . configureTestingModule ( {
41+ imports : [
42+ RouterTestingModule . withRoutes ( [ ] ) ,
43+ QueryParamModule . withConfig ( ) ,
44+ ] ,
45+ declarations : [
46+ TestComponent ,
47+ ] ,
48+ } ) ;
49+
50+ router = TestBed . get ( Router ) ;
51+ qpb = TestBed . get ( QueryParamBuilder ) ;
52+ TestBed . compileComponents ( ) ;
53+ router . initialNavigation ( ) ;
54+ } ) ) ;
55+
56+ beforeEach ( ( ) => {
57+ fixture = TestBed . createComponent ( TestComponent ) ;
58+ component = fixture . componentInstance ;
59+ fixture . detectChanges ( ) ;
60+
61+ paramGroup = component . paramGroup ;
62+ } ) ;
63+
64+ it ( 'removes a parameter from value changes' , fakeAsync ( ( ) => {
65+ scheduler . run ( ( { expectObservable } ) => {
66+ paramGroup . remove ( 'param' ) ;
67+ fixture . detectChanges ( ) ;
68+
69+ const groupValue$ = captureObservable ( paramGroup . valueChanges ) ;
70+ const value$ = captureObservable ( component . param . valueChanges ) ;
71+
72+ router . navigateByUrl ( '/?q=Test' ) ;
73+ tick ( ) ;
74+
75+ expectObservable ( groupValue$ ) . toBe ( 'a' , {
76+ a : { } ,
77+ } ) ;
78+ expectObservable ( value$ ) . toBe ( '' ) ;
79+ } ) ;
80+ } ) ) ;
81+
82+ it ( 'detaches the value accessor' , fakeAsync ( ( ) => {
83+ paramGroup . remove ( 'param' ) ;
84+ fixture . detectChanges ( ) ;
85+
86+ const input = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'input' ) as HTMLInputElement ;
87+ input . value = 'Test' ;
88+ input . dispatchEvent ( new Event ( 'input' ) ) ;
89+ tick ( ) ;
90+
91+ expect ( router . url ) . toBe ( '/' ) ;
92+ } ) ) ;
93+
94+ it ( 're-attaches the value accessor when the parameter is added back' , fakeAsync ( ( ) => {
95+ paramGroup . remove ( 'param' ) ;
96+ fixture . detectChanges ( ) ;
97+
98+ paramGroup . add ( 'param' , component . param ) ;
99+ fixture . detectChanges ( ) ;
100+
101+ const input = ( fixture . nativeElement as HTMLElement ) . querySelector ( 'input' ) as HTMLInputElement ;
102+ input . value = 'Test' ;
103+ input . dispatchEvent ( new Event ( 'input' ) ) ;
104+ tick ( ) ;
105+
106+ expect ( router . url ) . toBe ( '/?q=Test' ) ;
107+ } ) ) ;
108+ } ) ;
0 commit comments