I am doing unit testing with Jasmine, and Instanbul. I am testing a submit form. And I have already several unit tests for it. But now I want that if:
this.allowSubmitAgain = false; this method:
this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => {
not will be executed. Because on this line in instanbul:
if (this.allowSubmitAgain) {
it says E: else path not taken.
This is the whole method:
diplomaDataSubmitted() {
if (this.formModel && this.formModel.form.valid) {
this.formService.formStoreService.hideValidationSummary(this.formName);
Eif (this.allowSubmitAgain) {
this.allowSubmitAgain = false;
this.diplomaService.saveDiplomaMetaData(false, this.resources.opslaanSpinnerTekst).pipe(map(() => {
const documents: DocumentModel[] = this.formModel.getAngularFormControl(DiplomaFormKeysModel.keys.diplomasUpload).value.files;
this.diplomaService.saveDocumentForDiploma(documents, () => {
this.router.navigateByUrl(RoutesRegisterModel.pathnameMyRegisterRouteDiplomas).then(() => {
this.feedbackService.addSuccessMessageOnMainPortal(
{
key: this.resourceKeys.succesvolWijziging,
value: this.resources.succesvolWijziging,
}
);
});
}, (error: any) => {
this.allowSubmitAgain = true;
observableThrowError(error);
}, this.resources, this.resourceKeys);
})).subscribe(() => { }, (error: any) => {
this.allowSubmitAgain = true;
observableThrowError(error);
});
}
} else {
this.formService.formStoreService.showValidationSummary(this.formName);
}
}
And this is my unit test:
it('should not allow submit beaucse form has errors', fakeAsync(() => {
// Arrange
const spySaveMetaDataDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData');
diplomaServiceMock.returnErrorResponse();
// Act
component.diplomaDataSubmitted();
fixture.detectChanges();
// Assert
expect(spySaveMetaDataDiploma).not.toHaveBeenCalled();
console.log('testcase');
expect(component.allowSubmitAgain).toBe(false);
}));
But I get this error:
TypeError: Cannot read property 'subscribe' of undefined.
So how to fix that?
Thank you
I have it now like this:
it('should not allow submit beaucse form has errors', fakeAsync(() => {
// Arrange
const spySaveDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData').and.callThrough();
// Act
component.allowSubmitAgain = false;
// Assert
console.log('testcase');
expect(component.allowSubmitAgain).toBe(false);
expect(spySaveDiploma).not.toHaveBeenCalled();
}));
And I don't get errors.
But in the code coverage of Istanbul. The 'else path not taken' is still visible.
Eif (this.allowSubmitAgain) {
So on this line it still says else path not taken.
if I do it like this:
it('should not allow submit again beaucse form has errors', fakeAsync(() => {
const spySaveDiploma = spyOn(diplomaServiceMock, 'saveDiplomaMetaData').and.callThrough();
component.diplomaDataSubmitted();
component.allowSubmitAgain = false;
fixture.detectChanges();
console.log('testcase');
expect(component.allowSubmitAgain).toBe(false);
expect(spySaveDiploma).not.toHaveBeenCalled();
}));
I get this error:
Expected spy saveDiplomaMetaData not to have been called.
But what I have to do ?