1- import { Component , ElementRef , NgModule } from '@angular/core' ;
1+ import { Component , ElementRef , NgModule , ViewChild , TemplateRef } from '@angular/core' ;
22import { TestBed } from '@angular/core/testing' ;
33import { NB_DOCUMENT } from '../../theme.options' ;
44import { NbThemeModule } from '../../theme.module' ;
@@ -14,9 +14,36 @@ const WINDOW_CONTENT = 'window content';
1414} )
1515class NbTestWindowComponent { }
1616
17+ @Component ( {
18+ selector : 'nb-test-window-with-template' ,
19+ template : `
20+ <ng-template #contentTemplate let-data>
21+ <p>Static text: {{ data.text }}</p>
22+ </ng-template>
23+ ` ,
24+ } )
25+ class NbTestWindowWithTemplateComponent {
26+ @ViewChild ( 'contentTemplate' ) contentTemplate : TemplateRef < any > ;
27+
28+ constructor ( private ws : NbWindowService ) { }
29+
30+ openWindow ( ) {
31+ return this . ws . open (
32+ this . contentTemplate ,
33+ { title : 'Window content from template' , context : { text : 'hello world' } } ,
34+ ) ;
35+ }
36+ }
37+
38+ @Component ( {
39+ selector : 'nb-test-window-with-component' ,
40+ template : `<p>window content {{ componentInput }}<p>` ,
41+ } )
42+ export class TestWindowComponent { }
43+
1744@NgModule ( {
18- declarations : [ NbTestWindowComponent ] ,
19- entryComponents : [ NbTestWindowComponent ] ,
45+ declarations : [ NbTestWindowComponent , NbTestWindowWithTemplateComponent , TestWindowComponent ] ,
46+ entryComponents : [ NbTestWindowComponent , NbTestWindowWithTemplateComponent , TestWindowComponent ] ,
2047} )
2148class NbTestWindowModule { }
2249
@@ -203,4 +230,32 @@ describe('window-service', () => {
203230 const windowElement : HTMLElement = windowRef . componentRef . location . nativeElement ;
204231 expect ( windowElement . querySelector ( 'nb-card-body' ) ) . not . toBeNull ( ) ;
205232 } ) ;
233+
234+ it ( `should render window content from template with context` , function ( ) {
235+ const fixture = TestBed . createComponent ( NbTestWindowWithTemplateComponent ) ;
236+ fixture . detectChanges ( ) ;
237+
238+ const windowRef = fixture . componentInstance . openWindow ( ) ;
239+ windowRef . componentRef . changeDetectorRef . detectChanges ( ) ;
240+ expect ( windowRef . componentRef ) . toBeDefined ( ) ;
241+
242+ const windowElement : ElementRef < HTMLElement > = windowRef . componentRef . injector . get ( ElementRef ) ;
243+ expect ( windowElement . nativeElement . innerText ) . toContain ( 'Static text: hello world' ) ;
244+ } ) ;
245+
246+ it ( `should render window content from component without context` , function ( ) {
247+ const windowRef = windowService . open ( TestWindowComponent ) ;
248+ windowRef . componentRef . changeDetectorRef . detectChanges ( ) ;
249+
250+ const windowElement : ElementRef < HTMLElement > = windowRef . componentRef . injector . get ( ElementRef ) ;
251+ expect ( windowElement . nativeElement . innerText ) . toEqual ( 'window content' ) ;
252+ } ) ;
253+
254+ it ( `should render window content from component with context` , function ( ) {
255+ const windowRef = windowService . open ( TestWindowComponent , { context : { componentInput : 'hello world' } } ) ;
256+ windowRef . componentRef . changeDetectorRef . detectChanges ( ) ;
257+
258+ const windowElement : ElementRef < HTMLElement > = windowRef . componentRef . injector . get ( ElementRef ) ;
259+ expect ( windowElement . nativeElement . innerText ) . toEqual ( 'window content hello world' ) ;
260+ } ) ;
206261} ) ;
0 commit comments