I'm wondering if there is a way to test ng-content
without creating a host element?
For example, if I have alert component -
@Component({
selector: 'app-alert',
template: `
<div>
<ng-content></ng-content>
</div>
`,
})
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AlertComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AlertComponent);
component = fixture.componentInstance;
});
it('should display the ng content', () => {
});
How can I set ng-content
without creating a host element wrapper?
Once you project content into App Component using ng-content. Now, using the template reference variable “#contentParagraph” you can access ng-content using @ContentChild within Angular component (ItemElementComponent) class as shown below.
Content projection is a pattern in which you insert, or project, the content you want to use inside another component. For example, you could have a Card component that accepts content provided by another component.
You should configure a testing module before each test, using the TestBed . Here you can declare you component to test, and any providers it needs. Then you create the component using the TestBed also.
TestBed is the primary api for writing unit tests for Angular applications and libraries.
What is ng-content The ng-content tag acts as a placeholder for inserting external or dynamic content. The Parent component passes the external content to the child component. When Angular parses the template, it inserts the external content where ng-content
Using ng-content we can pass content inside the component selector and when angular parses that content that appears at the place of ng-content. Create an angular app to be used. Create a component “geek” using command “ng g c geek”. Then we use this component inside the app component and provide the ng-content inside “geek” component.
Create an angular app to be used. Create a component “geek” using command “ng g c geek”. Then we use this component inside the app component and provide the ng-content inside “geek” component. Using ng-content we are passing two things in geek component first is person position and second is year of experience.
When Angular parses the template, it inserts the external content where ng-content appears in the child component’s template We can use content projection to create a reusable component. The components that have similar logic & layout and can be used in many places in the application.
You have to create another dummy test component that contains this testing component, ie. app-alert
@Component({
template: `<app-alert>Hello World</app-alert>`,
})
class TestHostComponent {}
Make TestHostComponent part of your test bed module
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppAlert, TestHostComponent],
}).compileComponents();
}));
And then instantiate this testing component, check if that contains the ng-content part, ie. "hello world" text
it('should show ng content content', () => {
const testFixture = TestBed.createComponent(TestHostComponent);
const de: DebugElement = testFixture.debugElement.query(
By.css('div')
);
const el: Element = de.nativeElement;
expect(el.textContent).toEqual('Hello World');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With