Skip to content

Instantly share code, notes, and snippets.

@jholland918
Created November 3, 2016 16:14
Show Gist options
  • Save jholland918/38aa0151048e11c5dd99a54499cbcce3 to your computer and use it in GitHub Desktop.
Save jholland918/38aa0151048e11c5dd99a54499cbcce3 to your computer and use it in GitHub Desktop.
Testing Angular ui.bootstrap.modal
describe('user details controller',
function () {
var $controller, sut, helpers, $realUibModal, $spyUibModal, modalInstance, UserService, $routeParams, userStub, $location;
beforeEach(function () {
module('ui.bootstrap');
module('testHelpers');
module('app');
module(function ($provide) {
UserService = jasmine.createSpyObj('UserService', ['getUser', 'deleteUser']);
$provide.value('UserService', UserService);
$routeParams = {};
$provide.value('$routeParams', $routeParams);
$location = jasmine.createSpyObj('$location', ['path']);
$provide.value('$location', $location);
});
inject(function (_$controller_, _testHelpers_, _$uibModal_) {
$controller = _$controller_;
helpers = _testHelpers_;
$realUibModal = _$uibModal_;
});
userStub = { userType: "" };
UserService.getUser.and.callFake(helpers.promises.buildPromiseFake(userStub));
$spyUibModal = jasmine.createSpyObj('$uibModal', ['open']);
sut = $controller("UserDetailsController", { $routeParams: $routeParams, UserService: UserService, $uibModal: $spyUibModal, $location: $location });
UserService.deleteUser.and.callFake(helpers.promises.buildPromiseFake());
});
describe('deleteUserClick', function () {
it('should call service delete on prompt confirmation', function () {
configureAutoClosingModal();
var id = 1;
sut.deleteUserClick(id).resolve();
expect(UserService.deleteUser).toHaveBeenCalledWith(id);
});
});
function configureAutoClosingModal(closeResult) {
var controller = ['$uibModalInstance', modalController];
function modalController($uibModalInstance) {
$uibModalInstance.opened.then(function () {
$uibModalInstance.close(closeResult);
});
}
setModalInstance(controller);
}
function setModalInstance(controller) {
modalInstance = $realUibModal.open({
template: '<div />',
controller: controller
});
$spyUibModal.open.and.returnValue(modalInstance);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment