Skip to content

Instantly share code, notes, and snippets.

@kaplan81
Last active June 18, 2019 10:31
Show Gist options
  • Save kaplan81/e49334069e7245a7bb33c40786b234f8 to your computer and use it in GitHub Desktop.
Save kaplan81/e49334069e7245a7bb33c40786b234f8 to your computer and use it in GitHub Desktop.
import { ComponentSuite, ComponentSuiteElements } from '@my-test-kit';
import { PortalModule, TemplatePortal } from '@angular/cdk/portal';
import {
AfterContentInit,
Component,
TemplateRef,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NestedComponent } from './nested.component';
@Component({
template: `
<ng-template>External Content</ng-template>
<nested [content]="content"></nested>
`
})
class HostComponent implements AfterContentInit {
@ViewChild(TemplateRef)
templateRef: TemplateRef<any>;
content: TemplatePortal;
constructor(private viewContainerRef: ViewContainerRef) {}
ngAfterContentInit() {
this.content = new TemplatePortal(this.templateRef, this.viewContainerRef);
}
}
describe('NestedComponent', () => {
let fixture: ComponentFixture<HostComponent>;
let els: ComponentSuiteElements<HostComponent, NestedComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [PortalModule],
declarations: [HostComponent, NestedComponent]
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(HostComponent);
els = new ComponentSuite<HostComponent, NestedComponent>(
fixture,
'nested'
).elements;
});
}));
describe('basic case', () => {
it('should match snapshot', () => {
fixture.detectChanges();
expect(fixture).toMatchSnapshot();
});
it('should render content from external template', () => {
fixture.detectChanges();
const externalContent: TemplatePortal = els.host.component.content;
const internalContent: TemplatePortal = els.nested.component.content;
expect(externalContent).toBe(internalContent);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment