I have a below canDeactivate function in the component.
canDeactivate() {
return new Promise(resolve => {
if(this.close){
this.confirmationService.confirm({
message: 'Please close',
header: 'Warning',
icon: 'pi pi-excalmation-triangle'
accept: () => {
resolve(true);
},
reject: () => {
resolve(false);
}
});
} else {
resolve(true);
}
});
}
I have the below spec file for the same.
it('test canDeactivate', () => {
component.close = true;
const confirmationService: ConfirmationService = TestBed.get(ConfirmationService);
fixture.detectChanges();
spyOn<any>(confirmationService, 'confirm').and.callFake((param: any) => {
params.accept();
console.log('test spy');
expect(Promise.resolve()).toBeTruthy();
});
component.canDeactivate();
});
When I ran the spec with the above code, the test is passing but the statements line of confirmation service was not covered and even the console inside the spy is not logged. Please let me know how to test the confirmation service here in the right way.