[英]How can I unit test the result of a ui-bootsrap $uibModal instance?
I have a controller that calls $uibModal.open to pop a ui-bootstrap modal. 我有一个控制器调用$ uibModal.open来弹出一个ui-bootstrap模式。 The controller looks like this:
控制器看起来像这样:
(function () {
'use strict';
angular
.module('app')
.controller('MyCtrl', myCtrl);
function myCtrl($uibModal) {
var vm = this;
vm.showModal = showModal;
vm.results = [];
function showModal() {
var modalInstance = $uibModal.open({
templateUrl: '/mymodal.html',
controller: 'ModalCtrl ',
controllerAs: 'modalCtrl',
});
modalInstance.result.then(function (myResult) {
// I want to test that my code here gets called and behaves as expected. shortened for brevity
vm.results.push(myResult);
});
}
}
})();
I have successfully tested opening the modal with correct config options. 我已经成功测试了使用正确的配置选项打开模态。 But I would like to test the result of the modal as some logic is executed when this happens but I can't seem to figure out how I can hook into the result or get it to even execute in my tests
但是我想测试模态的结果,因为当发生这种情况时会执行一些逻辑,但我似乎无法弄清楚如何挂钩到结果中或者让它甚至在我的测试中执行
Tests with some setup code omitted 省略了一些设置代码的测试
(function() {
'use strict';
describe('myPageWithModal', function() {
//mock edit modal
var mockModalInstance = {
result: {
then: function(confirmCallback, cancelCallback) {
// Store the callbacks for later when the user clicks
// on the OK or Cancel button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
close: function( item ) {
// The user clicked OK on the modal dialog, call the stored
// confirm callback with the selected item
this.result.confirmCallBack( item );
},
dismiss: function( type ) {
// The user clicked cancel on the modal dialog, call
// the stored cancel callback
this.result.cancelCallback( type );
}
}
var mockModalOptions = {
controller: 'ModalCtrl ',
controllerAs: 'modalCtrl',
templateUrl: '/mymodal.html',
}
describe('myModal', function() {
var actualModalOptions;
beforeEach(function() {
ctrl = //omitted
//set up a spy to listen for when modal dialogs are opened and return our mock modal
spyOn($uibModal, 'open').and.callFake(function(options) {
actualModalOptions = options;
return mockModalInstance;
});
});
it('should open edit modal with options', function() {
ctrl.showModal();
expect($uibModal.open).toHaveBeenCalledWith(mockModalOptions);
});
it('should close modal and execute the modalInstance.result logic', function() {
});
});
});
})();
Considering that $uibModal.open
was mocked in current spec to return mockModalInstance
, mockModalInstance
should be freshly defined for each spec where it is used, and mockModalInstance.result
should be a promise: 考虑到
$uibModal.open
在当前规范嘲笑返回mockModalInstance
, mockModalInstance
应新鲜的地方每次使用规范中定义,并mockModalInstance.result
应该是一个承诺:
var modalResult = {};
var mockModalInstance = { result: $q.resolve(modalResult) };
spyOn(mockModalInstance.result, 'then').and.callThrough();
spyOn($uibModal, 'open').and.returnValue(mockModalInstance);
ctrl.showModal();
$rootScope.$digest();
expect(mockModalInstance.result.then).toHaveBeenCalledWith(jasmine.any(Function));
expect(ctrl.results.length).toBe(1);
expect(ctrl.results[0]).toBe(modalResult);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.