我正在使用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);
});
答案 0 :(得分:0)
好的,所以可能有一个更好的方法,但这似乎有用,所以这就是我想出来的。
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);
});