I have no idea why in my Angular project, Jasmin alwasys return null when I'm trying to access a HTML's element!
it('should find the <p> with fixture.debugElement.query(By.css)', () => {
const bannerDe: DebugElement = fixture.debugElement;
const paragraphDe = bannerDe.query(By.css('p'));
const p: HTMLElement = paragraphDe.nativeElement;
expect(p.textContent).toEqual('banner works!');
});
<p class="title">banner works!</p>
I also have tried to call 'fixture.whenStable().then' but again it returns null!
it('should find the <p> with fixture.debugElement.query(By.css)', () => {
const fixture = TestBed.createComponent(MobileAppLinkComponent);
fixture.detectChanges();
fixture.whenStable().then(() => {
const bannerDe: DebugElement = fixture.debugElement;
const paragraphDe = bannerDe.query(By.css('p'));
const p: HTMLElement = paragraphDe.nativeElement;
expect(p.textContent).toEqual('banner works!');
})
Most likely the
ptag is hidden by an*ngIf. Can you try logging out thefixture.nativeElementto see if you see theptag?If the
ptag is not there, most likely it is being hidden by an*ngIfby a parent element. You will have to make this condition true, thenfixture.detectChanges()so the HTML updates and then attempt to grab theptag withfixture.debugElement.query(By.css('p')).