I am running an Angular Unit Test with Karma Jasmine. Its testing a button input. We are receiving the note below with two tests, one regular and one with async.
Does anyone know how to fix this unit test? Other tests in the component are running successfully.
HTML:
<div *ngIf="enableButtonFlag">
<button class="email-edit-button" mat-icon-button (click)="editSharedFormControl('emailAddress')">
<mat-icon>edit</mat-icon>
</button>
</div>
Test Attempt 1:
it('click emailAddress Edit button should allow form control enabled', ()=> {
fixture.whenStable().then(() => {
let button = fixture.debugElement.query(By.css('.email-edit-button')).nativeElement;
button.click();
tick();
fixture.detectChanges();
expect(component.editSharedForm.get('emailAddress').enabled).toBe(true);
});
});
Result: Test Did not Fail or Succeed:
SPEC HAS NO EXPECTATIONS click emailAddress Edit button should allow form control enabled
Test Attempt 2: with Async
it('click emailAddress Edit button should allow form control enabled', async (() => {
fixture.whenStable().then(() => {
let button = fixture.debugElement.query(By.css('.email-edit-button')).nativeElement;
button.click();
tick();
fixture.detectChanges();
expect(component.editSharedForm.get('emailAddress').enabled).toBe(true);
});
}));
Error: Failed: Uncaught (in promise): TypeError: Cannot read property 'nativeElement' of null TypeError: Cannot read property 'nativeElement' of null
Resources:
Spec has no expectations - Jasmine testing the callback function
Spec has no expectation console error although expect is present