@@ -273,4 +273,87 @@ describe('TemplateRef', () => {
273273 } ) ;
274274 } ) ;
275275 } ) ;
276+
277+ describe ( 'context' , ( ) => {
278+ @Component ( {
279+ template : `
280+ <ng-template #templateRef let-name="name">{{name}}</ng-template>
281+ <ng-container #containerRef></ng-container>
282+ `
283+ } )
284+ class App {
285+ @ViewChild ( 'templateRef' ) templateRef ! : TemplateRef < any > ;
286+ @ViewChild ( 'containerRef' , { read : ViewContainerRef } ) containerRef ! : ViewContainerRef ;
287+ }
288+
289+ it ( 'should update if the context of a view ref is mutated' , ( ) => {
290+ TestBed . configureTestingModule ( { declarations : [ App ] } ) ;
291+ const fixture = TestBed . createComponent ( App ) ;
292+ fixture . detectChanges ( ) ;
293+ const context = { name : 'Frodo' } ;
294+ const viewRef = fixture . componentInstance . templateRef . createEmbeddedView ( context ) ;
295+ fixture . componentInstance . containerRef . insert ( viewRef ) ;
296+ fixture . detectChanges ( ) ;
297+
298+ expect ( fixture . nativeElement . textContent ) . toBe ( 'Frodo' ) ;
299+
300+ context . name = 'Bilbo' ;
301+ fixture . detectChanges ( ) ;
302+
303+ expect ( fixture . nativeElement . textContent ) . toBe ( 'Bilbo' ) ;
304+ } ) ;
305+
306+ it ( 'should update if the context of a view ref is replaced' , ( ) => {
307+ TestBed . configureTestingModule ( { declarations : [ App ] } ) ;
308+ const fixture = TestBed . createComponent ( App ) ;
309+ fixture . detectChanges ( ) ;
310+ const viewRef = fixture . componentInstance . templateRef . createEmbeddedView ( { name : 'Frodo' } ) ;
311+ fixture . componentInstance . containerRef . insert ( viewRef ) ;
312+ fixture . detectChanges ( ) ;
313+
314+ expect ( fixture . nativeElement . textContent ) . toBe ( 'Frodo' ) ;
315+
316+ viewRef . context = { name : 'Bilbo' } ;
317+ fixture . detectChanges ( ) ;
318+
319+ expect ( fixture . nativeElement . textContent ) . toBe ( 'Bilbo' ) ;
320+ } ) ;
321+
322+ it ( 'should use the latest context information inside template listeners' , ( ) => {
323+ const events : string [ ] = [ ] ;
324+
325+ @Component ( {
326+ template : `
327+ <ng-template #templateRef let-name="name">
328+ <button (click)="log(name)"></button>
329+ </ng-template>
330+ <ng-container #containerRef></ng-container>
331+ `
332+ } )
333+ class ListenerTest {
334+ @ViewChild ( 'templateRef' ) templateRef ! : TemplateRef < any > ;
335+ @ViewChild ( 'containerRef' , { read : ViewContainerRef } ) containerRef ! : ViewContainerRef ;
336+
337+ log ( name : string ) {
338+ events . push ( name ) ;
339+ }
340+ }
341+
342+ TestBed . configureTestingModule ( { declarations : [ ListenerTest ] } ) ;
343+ const fixture = TestBed . createComponent ( ListenerTest ) ;
344+ fixture . detectChanges ( ) ;
345+ const viewRef = fixture . componentInstance . templateRef . createEmbeddedView ( { name : 'Frodo' } ) ;
346+ fixture . componentInstance . containerRef . insert ( viewRef ) ;
347+ fixture . detectChanges ( ) ;
348+
349+ const button = fixture . nativeElement . querySelector ( 'button' ) ;
350+ button . click ( ) ;
351+ expect ( events ) . toEqual ( [ 'Frodo' ] ) ;
352+
353+ viewRef . context = { name : 'Bilbo' } ;
354+ fixture . detectChanges ( ) ;
355+ button . click ( ) ;
356+ expect ( events ) . toEqual ( [ 'Frodo' , 'Bilbo' ] ) ;
357+ } ) ;
358+ } ) ;
276359} ) ;
0 commit comments