Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of child components by tag name in angular2 unit testing

I have a parent component which will dynamically insert the child components based on the length of array. I want to test the number of components created, by using its tag name.

<div *ngFor = "let item of phoneList; let i = index">
    <phone-item [item]="item"> </phone-item>
</div>

I want to verify the number of <phone-item></phone-item> created within the html template in angular2 unit testing.

I have very little knowledge on debugElement and nativeElement. This is what I have tried to do.

let el = fixture.debugElement.query(By.css('phone-item'));

But I want the count of child components created.

like image 719
Amit Chigadani Avatar asked Apr 04 '17 10:04

Amit Chigadani


1 Answers

Use DebugElement#queryAll which returns an array. query only returns the first matching

like image 188
Paul Samsotha Avatar answered Sep 29 '22 04:09

Paul Samsotha