Skip to content

Commit

Permalink
fix(window): Fixed window.component.ts creating incorrect context (#1266
Browse files Browse the repository at this point in the history
)
  • Loading branch information
aefox authored and yggg committed Mar 17, 2019
1 parent 8db4b29 commit 30f4a5d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/framework/theme/components/window/window.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ export class NbWindowComponent implements OnInit, AfterViewChecked, OnDestroy {
}

protected attachTemplate() {
this.overlayContainer.attachTemplatePortal(new NbTemplatePortal(this.content as TemplateRef<any>, null, {
$implicit: this.context,
}));
this.overlayContainer
.attachTemplatePortal(new NbTemplatePortal(this.content as TemplateRef<any>, null, this.context));
}

protected attachComponent() {
Expand Down
61 changes: 58 additions & 3 deletions src/framework/theme/components/window/window.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ElementRef, NgModule } from '@angular/core';
import { Component, ElementRef, NgModule, ViewChild, TemplateRef } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { NB_DOCUMENT } from '../../theme.options';
import { NbThemeModule } from '../../theme.module';
Expand All @@ -14,9 +14,36 @@ const WINDOW_CONTENT = 'window content';
})
class NbTestWindowComponent {}

@Component({
selector: 'nb-test-window-with-template',
template: `
<ng-template #contentTemplate let-data>
<p>Static text: {{ data.text }}</p>
</ng-template>
`,
})
class NbTestWindowWithTemplateComponent {
@ViewChild('contentTemplate') contentTemplate: TemplateRef<any>;

constructor(private ws: NbWindowService) {}

openWindow() {
return this.ws.open(
this.contentTemplate,
{ title: 'Window content from template', context: { text: 'hello world' } },
);
}
}

@Component({
selector: 'nb-test-window-with-component',
template: `<p>window content {{ componentInput }}<p>`,
})
export class TestWindowComponent {}

@NgModule({
declarations: [NbTestWindowComponent],
entryComponents: [NbTestWindowComponent],
declarations: [NbTestWindowComponent, NbTestWindowWithTemplateComponent, TestWindowComponent],
entryComponents: [NbTestWindowComponent, NbTestWindowWithTemplateComponent, TestWindowComponent],
})
class NbTestWindowModule {}

Expand Down Expand Up @@ -203,4 +230,32 @@ describe('window-service', () => {
const windowElement: HTMLElement = windowRef.componentRef.location.nativeElement;
expect(windowElement.querySelector('nb-card-body')).not.toBeNull();
});

it(`should render window content from template with context`, function() {
const fixture = TestBed.createComponent(NbTestWindowWithTemplateComponent);
fixture.detectChanges();

const windowRef = fixture.componentInstance.openWindow();
windowRef.componentRef.changeDetectorRef.detectChanges();
expect(windowRef.componentRef).toBeDefined();

const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
expect(windowElement.nativeElement.innerText).toContain('Static text: hello world');
});

it(`should render window content from component without context`, function() {
const windowRef = windowService.open(TestWindowComponent);
windowRef.componentRef.changeDetectorRef.detectChanges();

const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
expect(windowElement.nativeElement.innerText).toEqual('window content');
});

it(`should render window content from component with context`, function() {
const windowRef = windowService.open(TestWindowComponent, { context: { componentInput: 'hello world' }});
windowRef.componentRef.changeDetectorRef.detectChanges();

const windowElement: ElementRef<HTMLElement> = windowRef.componentRef.injector.get(ElementRef);
expect(windowElement.nativeElement.innerText).toEqual('window content hello world');
});
});
2 changes: 1 addition & 1 deletion src/framework/theme/components/window/window.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class NbWindowService {
const ref = this.windowsContainerViewRef.createComponent(windowFactory, null, injector);
ref.instance.cfr = this.cfr;
ref.changeDetectorRef.detectChanges();
return ref
return ref;
}

protected subscribeToEvents(windowRef: NbWindowRef) {
Expand Down

0 comments on commit 30f4a5d

Please sign in to comment.