I am using NgbModal in my components and i am trying to write test case for that component.
My component function where i have opened modal look like this:
cancelLicense(activeMiningLicensesName: string, subscriptionId: string) {
const dialogueComponent: any = this.modalService.open(DialogueComponent, {
size: "md" as "lg",
centered: true,
backdrop: "static",
keyboard: false,
});
console.log(dialogueComponent, "===========================>");
dialogueComponent.componentInstance.message = `Are you sure you want to cancel your weekly subscription for ${activeMiningLicensesName}?`;
dialogueComponent.componentInstance.confirmText = "Yes";
dialogueComponent.componentInstance.cancelText = "No";
dialogueComponent.result.then((isConfirmClicked) => {
if (isConfirmClicked) {
this.confirmCancelLicense(activeMiningLicensesName, subscriptionId);
}
});
}
confirmCancelLicense(activeMiningLicensesName: string, subscriptionId: string) {
const dialogueComponent: any = this.modalService.open(DialogueComponent, {
size: "md" as "lg",
centered: true,
backdrop: "static",
keyboard: false,
});
console.log("----------------------->3", dialogueComponent);
dialogueComponent.componentInstance.message = `Please note that once you cancel, you can not get a refund unless the request for a refund falls within thirty (30) days of purchase. Also, refund only applies to the Yearly subscriptions; for more details, refer to the Terms and Conditions. Cancel anyway?`;
dialogueComponent.componentInstance.confirmText = "Yes";
dialogueComponent.componentInstance.cancelText = "No";
dialogueComponent.result.then((isConfirmClicked) => {
console.log("----------------------->2");
if (isConfirmClicked) {
this.subscriptionCancellation(activeMiningLicensesName, subscriptionId);
}
});
}
Here modelService is an injector to NgbModal service.
Now coming to test case, I have mocked modelService like below:
modalService = TestBed.inject(NgbModal);
component = fixture.componentInstance;
const ngBmodalref: Partial < NgbModalRef > = {
componentInstance: {},
result: new Promise((resolve, reject) => {
resolve({
status: true
});
}),
};
modelOpenSpy = spyOn(modalService, "open").and.returnValue(ngBmodalref as NgbModalRef);
Everything works fine when the model.open function is called in test case for the first time but when it is called second time rather than returning spied value it returns a NgBModalRefrence(see attached image)
If anyone can help me figure out why this is happening!
Note: I also tried spyOn().and.returnValues() and passing two different value, then also it is working the same way.