Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

test($compile): add test for optional require in directives with ^ operator #9392

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
"Controller '{0}', required by directive '{1}', can't be found!",
require, directiveName);
}
return value;
return value || null;
} else if (isArray(require)) {
value = [];
forEach(require, function(require) {
Expand Down
36 changes: 36 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4095,6 +4095,42 @@ describe('$compile', function() {
});


it("should pass null if required controller can't be found and is optional",function() {
module(function() {
directive('dep', function(log) {
return {
require: '?^main',
link: function(scope, element, attrs, controller) {
log('dep:' + controller);
}
};
});
});
inject(function(log, $compile, $rootScope) {
$compile('<div main><div dep></div></div>')($rootScope);
expect(log).toEqual('dep:null');
});
});


it("should pass null if required controller can't be found and is optional with the question mark on the right",function() {
module(function() {
directive('dep', function(log) {
return {
require: '^?main',
link: function(scope, element, attrs, controller) {
log('dep:' + controller);
}
};
});
});
inject(function(log, $compile, $rootScope) {
$compile('<div main><div dep></div></div>')($rootScope);
expect(log).toEqual('dep:null');
});
});


it('should have optional controller on current element', function() {
module(function() {
directive('dep', function(log) {
Expand Down