I have an Angular (using Angular 10) component with a HTML snippet like below:
<div *ngIf="someCondition()" id="myID">
<p>Line 1</p>
<p>Line 2</p>
<p>Line 3</p>
<p>Line 4</p>
<p>Line 5</p>
<p>Line 6</p>
<p>Line 7</p>
</div>
Now, while unit testing, I would like to figure out if *ngIf
satisfies, the corresponding <div>
will have 7 child elements.
In the spec file, I have the following :
it('should check for 7 elements', async () => {
spyOn(component, 'someCondition()').and.returnValue(true);
fixture.detectChanges();
await fixture.whenRenderingDone();
const elements = fixture.debugElement.queryAllNodes(By.css('#myID'));
// const elements = fixture.debugElement.queryAll(By.css('#myID'));
console.log('check elements.... ', elements);
})
With the above, I can not get access to the child <p>
tags or the number of the children! Also, it would be good, if I could test a specific <p>
tag with expected value.
How can I achieve this?