1
1
import {
2
2
ChangeDetectorRef ,
3
3
Component ,
4
+ Directive ,
4
5
TemplateRef ,
5
6
ViewContainerRef ,
6
7
} from '@angular/core' ;
@@ -13,6 +14,7 @@ import {
13
14
waitForAsync ,
14
15
} from '@angular/core/testing' ;
15
16
import {
17
+ BehaviorSubject ,
16
18
EMPTY ,
17
19
interval ,
18
20
NEVER ,
@@ -60,6 +62,29 @@ class LetDirectiveTestCompleteComponent {
60
62
value$ : Observable < number > = of ( 42 ) ;
61
63
}
62
64
65
+ @Directive ( {
66
+ selector : '[recursiveDirective]' ,
67
+ } )
68
+ export class RecursiveDirective {
69
+ constructor ( private subject : BehaviorSubject < number > ) {
70
+ this . subject . next ( 1 ) ;
71
+ }
72
+ }
73
+
74
+ @Component ( {
75
+ template : `
76
+ <ng-container recursiveDirective *ngrxLet="subject as value">{{
77
+ value
78
+ }}</ng-container>
79
+ ` ,
80
+ } )
81
+ class LetDirectiveTestRecursionComponent {
82
+ constructor ( public subject : BehaviorSubject < number > ) { }
83
+ get value$ ( ) {
84
+ return this . subject ;
85
+ }
86
+ }
87
+
63
88
let fixtureLetDirectiveTestComponent : ComponentFixture < LetDirectiveTestComponent > ;
64
89
let letDirectiveTestComponent : {
65
90
value$ : ObservableInput < any > | undefined | null ;
@@ -117,6 +142,29 @@ const setupLetDirectiveTestComponentComplete = (): void => {
117
142
componentNativeElement = fixtureLetDirectiveTestComponent . nativeElement ;
118
143
} ;
119
144
145
+ const setupLetDirectiveTestRecursionComponent = ( ) : void => {
146
+ const subject = new BehaviorSubject ( 0 ) ;
147
+ TestBed . configureTestingModule ( {
148
+ declarations : [
149
+ LetDirectiveTestRecursionComponent ,
150
+ RecursiveDirective ,
151
+ LetDirective ,
152
+ ] ,
153
+ providers : [
154
+ { provide : ChangeDetectorRef , useClass : MockChangeDetectorRef } ,
155
+ TemplateRef ,
156
+ ViewContainerRef ,
157
+ { provide : BehaviorSubject , useValue : subject } ,
158
+ ] ,
159
+ } ) ;
160
+ fixtureLetDirectiveTestComponent = TestBed . createComponent (
161
+ LetDirectiveTestRecursionComponent
162
+ ) ;
163
+ letDirectiveTestComponent =
164
+ fixtureLetDirectiveTestComponent . componentInstance ;
165
+ componentNativeElement = fixtureLetDirectiveTestComponent . nativeElement ;
166
+ } ;
167
+
120
168
describe ( 'LetDirective' , ( ) => {
121
169
describe ( 'when nexting values' , ( ) => {
122
170
beforeEach ( waitForAsync ( setupLetDirectiveTestComponent ) ) ;
@@ -281,4 +329,14 @@ describe('LetDirective', () => {
281
329
expect ( componentNativeElement . textContent ) . toBe ( 'true' ) ;
282
330
} ) ;
283
331
} ) ;
332
+
333
+ describe ( 'when rendering recursively' , ( ) => {
334
+ beforeEach ( waitForAsync ( setupLetDirectiveTestRecursionComponent ) ) ;
335
+
336
+ it ( 'should render 2nd emitted value if the observable emits while the view is being rendered' , fakeAsync ( ( ) => {
337
+ fixtureLetDirectiveTestComponent . detectChanges ( ) ;
338
+ expect ( letDirectiveTestComponent ) . toBeDefined ( ) ;
339
+ expect ( componentNativeElement . textContent ) . toBe ( '1' ) ;
340
+ } ) ) ;
341
+ } ) ;
284
342
} ) ;
0 commit comments