By using the $uibModal
service to open a modal window, we need to inject the $uibModalInstance
in the modal controller to close or dismiss the modal window, this injection break down my unit tests.
script.js
angular.module('demo', ['ui.bootstrap'])
.controller('MainController', MainCtrl)
.controller('UIModalController', UIModalCtrl);
/** @ngInject */
function MainCtrl($log, $uibModal) {
var vm = this;
vm.openModal = function() {
$log.log('Opening Modal..');
$uibModal.open({
templateUrl: 'modal.html',
controller: 'UIModalController',
controllerAs: 'modal'
});
};
}
/** @ngInject */
function UIModalCtrl($log, $uibModalInstance) {
var vm = this;
vm.ok = function() {
$log.log('Ok clicked');
$uibModalInstance.close();
};
}
modal.html
<div class='modal-header'>
<h3>Hi there!</h3>
</div>
<div class='modal-body'>
Some content goes here
</div>
<div class='modal-footer'>
<button class='btn btn-primary' ng-click='modal.ok()'>Ok</button>
</div>
index.spec.js
describe('MainContrller', function() {
var vm;
beforeEach(module('demo'));
beforeEach(inject(function(_$controller_) {
vm = _$controller_('UIModalController', {});
}));
it('should define a ok function', function() {
expect(vm.ok).toBeDefined();
expect(angular.isFunction(vm.ok)).toBeTruthy();
});
it('should close the modal window if Ok button is pressed', inject(function($uibModalInstance) {
spyOn($uibModalInstance, 'close').and.callThrough();
vm.ok();
expect($uibModalInstance.close).toHaveBeenCalled();
}));
});
Here's a plunker to illustrate the issue: http://plnkr.co/edit/w4D2c25aa45OSXn3ZsJb?p=preview
Note: The problem I'm facing is not to close the modal (cause it works), the problem is that my unit tests work before $uibModalInstance
is injected.
The error thrown is:
Error: [$injector:unpr] Unknown provider: $uibModalInstanceProvider <- $uibModalInstance
Any suggestions are welcome, thanks for reading.