Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 test ng-content

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?

like image 651
undefined Avatar asked May 14 '17 15:05

undefined


People also ask

How do you get Ng-content?

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.

What is Contentprojection?

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.

How can we get an instance of the component to be tested?

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.

What is TestBed in Angular?

TestBed is the primary api for writing unit tests for Angular applications and libraries.

What is the use of ng content in angular?

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

How to pass content from one component to another in angular?

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.

How to create a geek component in Angular 2?

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.

What is Content Projection in angular?

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.


1 Answers

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');
});
like image 132
user1929497 Avatar answered Sep 29 '22 20:09

user1929497