Reputation: 145
I use service $uibModal to create modal window. And I need to ensure that template file exists.
(function () {
angular.module('some.module.name').factory('serviceName', function ($uibModal) {
function open() {
return $uibModal.open({
templateUrl: 'path/to/template.html',
controller: 'ControllerName',
resolve: {
context: function () {
return something;
}
}
});
}
return {
open: open
};
});
})();
I can check with static path like this way:
it('should pass a config object when opens the modal window', function () {
expectedOptions = {
templateUrl: 'path/to/template.html',
controller: 'ControllerName',
resolve: {
context: jasmine.any(Function)
}
};
serviceName.open();
expect($uibModal.open).toHaveBeenCalledWith(jasmine.objectContaining(expectedOptions));
});
But I will need to modify this test if the template path will be changed.
How can I check whether the modal window receives an existing template without test modification?
Thanks in advance!
Upvotes: 3
Views: 1062
Reputation: 1081
I think your test should check that the content from your template is loaded into modal window, instead of checking that the template file exists. To do that you have to use ngMock with Jasmine. You can find working example of similar test inside the source code of AngularUI Bootstrap Below is my solution (this is not real code, just example):
describe('Modal window', function() {
var $rootScope,
$uibModal,
$compile,
modal;
beforeEach(module('ui.bootstrap.modal'));
beforeEach(inject(function(_$rootScope_, _$compile_, _$uibModal_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$uibModal = _$uibModal_;
var modalOptions = {
templateUrl: 'path/to/template.html',
controller: 'ControllerName',
resolve: {
context: jasmine.any(Function)
}
};
var modal = $uibModal.open(modalOptions);
$rootScope.$digest();
}));
it('should load "Hello World" from template', function {
var contentToCompare = actual.find('body > div.modal > div.modal-dialog > div.modal-content');
var result = contentToCompare.html().indexOf('Hello World');
expect(result).toBeGreaterThan(0);
});
});
Upvotes: 1