Reputation: 71
I havea a angular 8 application and I am using Jasmine Karma for unit testing. And I want to test a Formbuilder that is disabled in the NgOninit function:
I have it like this: component.ts
constructor(
private dossierService: DossierService,
private route: ActivatedRoute,
private sanitizer: DomSanitizer,
private dossierFileService: DossierFileService,
private errorProcessor: ErrorProcessor,
private dialog: MatDialog
) {
this.dossierItems = this.route.snapshot.data.dossierItems;
this.editDossierForm = this.formBuilder.group({});
this.editDossierForm.disable();
this.dossier = this.route.snapshot.data.dossier;
this.dossierItems = route.snapshot.data.dossierItems;
this.profileImagefile = this.route.snapshot.data.profileImage;
this.editDossierForm = this.formBuilder.group({
firstName: this.formBuilder.control(this.dossier.firstName, [Validators.required, Validators.maxLength(255)]),
lastName: this.formBuilder.control(this.dossier.lastName, [Validators.required, Validators.maxLength(255)]),
mobile: this.formBuilder.control(this.dossier.mobile, [Validators.maxLength(255)]),
company: this.formBuilder.control(this.dossier.company, [Validators.maxLength(255)]),
buddy: this.formBuilder.control(this.dossier.buddy, [Validators.maxLength(255)]),
supervisor: this.formBuilder.control(this.dossier.supervisor, [Validators.maxLength(255)]),
dateOfBirth: this.formBuilder.control(this.dossier.dateOfBirth)
});
}
ngOnInit(): void {
this.editDossierForm.disable();
}
}
and this is the spec file of it:
describe('DossierPersonalDataComponent', () => {
let component: DossierPersonalDataComponent;
let fixture: ComponentFixture<DossierPersonalDataComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, DossierModule, BrowserModule],
declarations: [DossierPersonalDataComponent],
providers: [
DossierFileService,
ErrorProcessor,
{
provide: ActivatedRoute,
useValue: {
snapshot: {
data: {
dossier: {
firstName: 'hello',
lastName: 'world',
mobile: '111-111-1111',
company: 'carapax',
buddy: 'bud',
supervisor: 'super',
dateOfBirth: '1900-01-01',
},
dossierItems: [], // mock
profileImage: '',
}
}
}
},
{
// DossierFileService, These have to be outside of the braces
// ErrorProcessor,
provide: DomSanitizer,
useValue: {
sanitize: () => 'safeString',
bypassSecurityTrustHtml: () => 'safeString'
}
}
]
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(DossierPersonalDataComponent);
component = fixture.componentInstance;
});
}));
it('should create', () => {
expect(component).toBeTruthy();
});
});
but in the instanbul coverage rapport this stays red:
}
ngOnInit(): void {
this.editDossierForm.disable();
}
So how to coverage this part?
Thank you
oke,
if I do it like this:
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
I get this error:
DossierPersonalDataComponent > should create
TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
and in my tesspec, I see this:
get profileImageUrl() {
return this.profileImagefile === null
? '/assets/placeholder.jpg'
: this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(this.profileImagefile));
}
and this line:
? '/assets/placeholder.jpg'
is in yellow. Branch not covered.
Upvotes: 1
Views: 1585
Reputation: 1659
You need to execute a fixture.detectChanges()
. Calling this the first time, it will trigger ngOnInit
. After that it will just trigger change detection.
So depending on your tests, you can either include a fixture.detectChanges()
inside the beforeEach
block.
I would only recommend this approach if you don't want to spy or setup different mocks for things happening inside ngOnInit
for each test case.
I usually add this inside every test case after I declared my spys. So your test would look something along those lines:
it('should create', () => {
// spy on anything you would like. e.g the disable call of your form
// then trigger onInit
fixture.detectChanges();
// then run your expectations
expect(component).toBeTruthy();
});
Upvotes: 1