角度测试可以使用ngbootsrap模态来激活方法

时间:2018-06-13 09:07:28

标签: angular ng-bootstrap angular-test

我正在尝试测试我的canDeactivate方法,但我不知道如何开始。

/**
* Implementation of canDeactivate. This is invoked through a routing guard and is the special implementation only for this
* component. The routing guard defined in core/guards/can-deactivate.guard.ts invokes this.
* If the form is dirty (so any changes have been made) a modal dialog is raised to ask if the user really wants to leave
* without saving. Then we call a method to subscribe to the modals decision. We return a subject of the decision. The
* can-deactivate guard knows how to handle the subject.
*/
canDeactivate(): Observable<boolean> | boolean {
  if (this.userForm.dirty) {
    this.decisionModalRef = this.modalService.open(AlertComponent);
    this.listenForDecision();
    return this.decision;
  }
  return true;
}


/**
* This method subscribes to the buttonclick in the modal in order to pass the chosen value to the subject and close the
* modal.
*/
listenForDecision() {
   this.decisionModalRef.componentInstance.decision.subscribe(res => {
     this.decisionModalRef.close();
     this.decision.next(res);
   });
 }

canDeactivate()方法是canDeactivate Guard使用此组件的实现。

有没有人知道如何与ngbootstrap模态一起测试?

0 个答案:

没有答案