@@ -7,14 +7,17 @@ import {
7
7
ChangeDetectionStrategy ,
8
8
Component ,
9
9
DoCheck ,
10
+ Input ,
10
11
OnChanges ,
11
12
OnDestroy ,
12
13
OnInit ,
14
+ SimpleChange ,
15
+ SimpleChanges ,
13
16
} from '@angular/core' ;
14
17
import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ;
15
18
import { By } from '@angular/platform-browser' ;
16
19
import { getObservableLifecycle } from 'ngx-observable-lifecycle' ;
17
- import { mapTo } from 'rxjs/operators' ;
20
+ import { map } from 'rxjs/operators' ;
18
21
19
22
describe ( 'integration' , ( ) => {
20
23
type ObserverSpy = {
@@ -59,6 +62,8 @@ describe('integration', () => {
59
62
AfterContentChecked ,
60
63
AfterContentInit
61
64
{
65
+ @Input ( ) input : any ;
66
+
62
67
public componentInstanceId = componentInstanceId ++ ;
63
68
64
69
public ngAfterContentChecked ( ) : void {
@@ -81,8 +86,8 @@ describe('integration', () => {
81
86
ngDoCheckSpy ( ) ;
82
87
}
83
88
84
- public ngOnChanges ( ) : void {
85
- ngOnChangesSpy ( ) ;
89
+ public ngOnChanges ( simpleChanges : SimpleChanges ) : void {
90
+ ngOnChangesSpy ( simpleChanges ) ;
86
91
}
87
92
88
93
public ngOnDestroy ( ) : void {
@@ -105,14 +110,16 @@ describe('integration', () => {
105
110
ngOnDestroy,
106
111
} = getObservableLifecycle ( this ) ;
107
112
108
- ngOnChanges . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( onChanges$Spy ) ;
109
- ngOnInit . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( onInit$Spy ) ;
110
- ngDoCheck . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( doCheck$Spy ) ;
111
- ngAfterContentInit . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( afterContentInit$Spy ) ;
112
- ngAfterContentChecked . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( afterContentChecked$Spy ) ;
113
- ngAfterViewInit . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( afterViewInit$Spy ) ;
114
- ngAfterViewChecked . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( afterViewChecked$Spy ) ;
115
- ngOnDestroy . pipe ( mapTo ( this . componentInstanceId ) ) . subscribe ( onDestroy$Spy ) ;
113
+ const instanceId = this . componentInstanceId ;
114
+
115
+ ngOnChanges . pipe ( map ( value => ( { instanceId, value } ) ) ) . subscribe ( onChanges$Spy ) ;
116
+ ngOnInit . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( onInit$Spy ) ;
117
+ ngDoCheck . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( doCheck$Spy ) ;
118
+ ngAfterContentInit . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( afterContentInit$Spy ) ;
119
+ ngAfterContentChecked . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( afterContentChecked$Spy ) ;
120
+ ngAfterViewInit . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( afterViewInit$Spy ) ;
121
+ ngAfterViewChecked . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( afterViewChecked$Spy ) ;
122
+ ngOnDestroy . pipe ( map ( ( ) => ( { instanceId } ) ) ) . subscribe ( onDestroy$Spy ) ;
116
123
}
117
124
}
118
125
@@ -131,14 +138,37 @@ describe('integration', () => {
131
138
}
132
139
}
133
140
141
+ @Component ( {
142
+ selector : 'lib-host-with-input-component' ,
143
+ template : `
144
+ <h1>Host with input Component</h1>
145
+ <lib-test-component *ngIf="testComponentVisible" [input]="inputValue"></lib-test-component>
146
+ ` ,
147
+ } )
148
+ class HostWithInputComponent {
149
+ public testComponentVisible = false ;
150
+
151
+ public inputValue = undefined ;
152
+
153
+ public setTestComponentVisible ( visible : boolean ) {
154
+ this . testComponentVisible = visible ;
155
+ }
156
+
157
+ public setInputValue ( value : any ) {
158
+ this . inputValue = value ;
159
+ }
160
+ }
161
+
134
162
let component : HostComponent ;
163
+ let componentWithInput : HostWithInputComponent ;
135
164
let fixture : ComponentFixture < HostComponent > ;
165
+ let fixtureWithInput : ComponentFixture < HostWithInputComponent > ;
136
166
137
167
beforeEach (
138
168
waitForAsync ( ( ) => {
139
169
TestBed . configureTestingModule ( {
140
170
imports : [ CommonModule ] ,
141
- declarations : [ HostComponent , TestComponent ] ,
171
+ declarations : [ HostComponent , HostWithInputComponent , TestComponent ] ,
142
172
} ) . compileComponents ( ) ;
143
173
} ) ,
144
174
) ;
@@ -167,6 +197,10 @@ describe('integration', () => {
167
197
fixture = TestBed . createComponent ( HostComponent ) ;
168
198
component = fixture . componentInstance ;
169
199
fixture . detectChanges ( ) ;
200
+
201
+ fixtureWithInput = TestBed . createComponent ( HostWithInputComponent ) ;
202
+ componentWithInput = fixtureWithInput . componentInstance ;
203
+ fixtureWithInput . detectChanges ( ) ;
170
204
} ) ;
171
205
172
206
it ( 'should be created' , ( ) => {
@@ -239,10 +273,42 @@ describe('integration', () => {
239
273
240
274
newInstance . ngOnDestroy ( ) ;
241
275
242
- expect ( onDestroy$Spy . next ) . toHaveBeenCalledWith ( newInstance . componentInstanceId ) ;
276
+ expect ( onDestroy$Spy . next ) . toHaveBeenCalledWith ( { instanceId : newInstance . componentInstanceId } ) ;
243
277
244
278
const componentUnderTest = fixture . debugElement . query ( By . directive ( TestComponent ) ) . componentInstance ;
245
279
246
- expect ( onDestroy$Spy . next ) . not . toHaveBeenCalledWith ( componentUnderTest . componentInstanceId ) ;
280
+ expect ( onDestroy$Spy . next ) . not . toHaveBeenCalledWith ( { instanceId : componentUnderTest . componentInstanceId } ) ;
281
+ } ) ;
282
+
283
+ it ( 'should still receive the SimpleChanges object in the ngOnChanges original hook and provide the SimpleChanges into the stream as well' , ( ) => {
284
+ expect ( onChanges$Spy . next ) . not . toHaveBeenCalled ( ) ;
285
+ expect ( ngOnChangesSpy ) . not . toHaveBeenCalled ( ) ;
286
+ componentWithInput . setTestComponentVisible ( true ) ;
287
+ fixtureWithInput . detectChanges ( ) ;
288
+
289
+ expect ( onChanges$Spy . next ) . toHaveBeenCalledOnceWith ( {
290
+ instanceId : jasmine . anything ( ) ,
291
+ value : {
292
+ input : new SimpleChange ( undefined , undefined , true ) ,
293
+ } ,
294
+ } ) ;
295
+ expect ( ngOnChangesSpy ) . toHaveBeenCalledOnceWith ( {
296
+ input : new SimpleChange ( undefined , undefined , true ) ,
297
+ } ) ;
298
+
299
+ componentWithInput . setInputValue ( 'New value' ) ;
300
+ fixtureWithInput . detectChanges ( ) ;
301
+
302
+ expect ( onChanges$Spy . next ) . toHaveBeenCalledTimes ( 2 ) ;
303
+ expect ( onChanges$Spy . next ) . toHaveBeenCalledWith ( {
304
+ instanceId : jasmine . anything ( ) ,
305
+ value : {
306
+ input : new SimpleChange ( undefined , 'New value' , false ) ,
307
+ } ,
308
+ } ) ;
309
+ expect ( ngOnChangesSpy ) . toHaveBeenCalledTimes ( 2 ) ;
310
+ expect ( ngOnChangesSpy ) . toHaveBeenCalledWith ( {
311
+ input : new SimpleChange ( undefined , 'New value' , false ) ,
312
+ } ) ;
247
313
} ) ;
248
314
} ) ;
0 commit comments