I need to mock a NgbModal to do some unit tests in angular, but I have no idea how to do this. This is my function:
openModal(deviceID: string, productID: string){
const modalRef = this.modalService.open(ProductModal)
modalRef.componentInstance.content = {
deviceId: deviceID,
productId: productID
}
modalRef.componentInstance.toEmit.subscribe(($e) => {
if ($e === true) this.reloadList();
});
}
What am I supposed to do?
Assuming your
modalServiceisNgbModal, it seems the logic you want to test is inside the modal content (ProductModal), notNgbModalitself.As you can see, after using
.open()yourmodalRef.componentInstancewill be an instance ofProductModal; so you can testProductModalas any component with e.g. component fixtures and skipping themodalServicealtogether:(Again, assuming
ProductModalis a component with proper decorations.)