【问题标题】:Mocking $uibModal's opened and closed promises模拟 $uibModal 的打开和关闭承诺
【发布时间】:2017-08-09 18:12:14
【问题描述】:

我正在使用 Angular-UI 的 $uibModal 在我的代码中打开一个模式。调用 open 方法后,我定义了在 opens.then() 和 closed.then() 承诺中运行的代码。所有这一切都很好,但是在尝试测试它时(在 Jasmine 中),我不知道如何解决打开和关闭的承诺。

这是我用来打开模式的代码(在我的控制器中):

function backButtonClick() {
  var warningModal = $uibModal.open({
    animation: true,
    ariaLabelledBy: 'modal-warning-header',
    ariaDescribedBy: 'modal-alert-body',
    backdrop: 'static',
    templateUrl: 'app/components/directives/modals/alertModal/alertModal.html',
    controller: 'AlertModalController',
    controllerAs: 'vm',
    size: 'sm',
    resolve: {
      options: function() { 
        return {
          title: stringsService.getString('WorkNotSavedTitle'),
          message: stringsService.getString('WorkNotSavedMessage'),
          modalHeaderClass: 'modal-warning-header',
          modalHeaderIconClass: 'fa-warning modal-warning-alert-icon',
          modalHeaderTitleClass: 'modal-warning-alert-title',
          modalContentClass: 'modal-warning-content',
          modalButtonsClass: 'modal-centered-buttons',
          showModalHeader: true,
          showPrimaryButton: true,
          showSecondaryButton: false,
          showTertiaryButton: true,
          primaryButtonText: stringsService.getString('RemainInActivityButton'),
          primaryButtonClick: function() { warningModal.dismiss(); },
          tertiaryButtonText: stringsService.getString('LeaveActivityButton'),
          tertiaryButtonClick: function() { warningModal.dismiss(); leaveActivity(); }
        }; 
      }
    }
  });
  warningModal.opened.then(function() { vm.isWarningModalOpen = true; });
  warningModal.closed.then(function() { vm.isWarningModalOpen = false; });
}

以及我目前的测试:

it('should show the Warning modal if the back button is clicked', function() {
    var modalServiceMock = {
      open: function(options) {}
    };      
    sinon.stub(modalServiceMock, 'open').returns({
      dismiss: function() { return; },
      opened: {
        then: function(callback) { return callback(); }
      },
      closed: {
        then: function(callback) { return callback(); }
      }
    });        
    var ctlr = $controller('BayServiceController', { $scope: this.$scope, $uibModal: modalServiceMock});
    ctlr.backButtonClick();

    //this line passes
    expect(modalServiceMock.open).toHaveBeenCalled();
    //this line fails
    expect(ctlr.isWarningModalOpen).toBe(true); 
});

【问题讨论】:

    标签: angularjs jasmine angular-ui-bootstrap sinon


    【解决方案1】:

    好的,所以可能有更好的方法,但这似乎可行,所以这就是我想出的。

    it('should show the Warning modal if the back button is clicked', function() {
      modalServiceMock = {
        open: function(modalOptions) {
          var closedCallback;
          return {
            dismiss: function() { closedCallback(); },
            opened: { 
              then: function(callback) { callback(); }
            },
            closed: {
              then: function(callback) { closedCallback = callback; }
            },
            resolver: modalOptions.resolve
          };
        }
      };        
      var ctlr = $controller('BayServiceController', { $scope: this.$scope, $uibModal: modalServiceMock});
      ctlr.backButtonClick();
    
      this.rootScope.$apply();
      expect(ctlr.isWarningModalOpen).toBe(true); //this now passes
    
      //to test closing the modal, I access the resolver property of the mock and run the given method for dismissing the modal
      ctlr.warningModal.resolver.options().primaryButtonClick();
    
      expect(ctlr.isWarningModalOpen).toBe(false); 
    
    });
    

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2014-02-04
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2022-06-29
      • 2019-06-01
      相关资源
      最近更新 更多