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
modalService
isNgbModal
, it seems the logic you want to test is inside the modal content (ProductModal
), notNgbModal
itself.As you can see, after using
.open()
yourmodalRef.componentInstance
will be an instance ofProductModal
; so you can testProductModal
as any component with e.g. component fixtures and skipping themodalService
altogether:(Again, assuming
ProductModal
is a component with proper decorations.)