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

Commit d0f1285

Browse files
committed
test(compile): additional tests for controllers with explicit return values.
Includes tests for controllers that: - explicitly return primitives - are attached to scope using `controllerAs` - transclude contents
1 parent 57fe701 commit d0f1285

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

src/ng/compile.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1974,8 +1974,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
19741974
// Remove and re-install bindToController bindings
19751975
thisLinkFn.$$destroyBindings();
19761976
thisLinkFn.$$destroyBindings =
1977-
initializeDirectiveBindings(scope, attrs, controllerResult,
1978-
bindings, scopeDirective);
1977+
initializeDirectiveBindings(scope, attrs, controllerResult, bindings, scopeDirective);
19791978
}
19801979
}
19811980
}

test/ng/compileSpec.js

+103
Original file line numberDiff line numberDiff line change
@@ -4219,6 +4219,28 @@ describe('$compile', function() {
42194219
});
42204220

42214221

4222+
it('primitive controller return values are ignored', function() {
4223+
module(function() {
4224+
directive('logControllerProp', function(log) {
4225+
return {
4226+
controller: function($scope) {
4227+
this.foo = 'baz'; // value *will* be used.
4228+
return 'bar';
4229+
},
4230+
link: function(scope, element, attrs, controller) {
4231+
log(controller.foo);
4232+
}
4233+
};
4234+
});
4235+
});
4236+
inject(function(log, $compile, $rootScope) {
4237+
element = $compile('<log-controller-prop></log-controller-prop>')($rootScope);
4238+
expect(log).toEqual('baz');
4239+
expect(element.data('$logControllerPropController').foo).toEqual('baz');
4240+
});
4241+
});
4242+
4243+
42224244
it('should get required parent controller', function() {
42234245
module(function() {
42244246
directive('nested', function(log) {
@@ -4238,6 +4260,28 @@ describe('$compile', function() {
42384260
});
42394261

42404262

4263+
it('should get explicit return value of required parent controller', function() {
4264+
module(function() {
4265+
directive('nested', function(log) {
4266+
return {
4267+
require: '^^?nested',
4268+
controller: function() {
4269+
return {foo: 'bar'};
4270+
},
4271+
link: function(scope, element, attrs, controller) {
4272+
log(!!controller && controller.foo);
4273+
}
4274+
};
4275+
});
4276+
});
4277+
inject(function(log, $compile, $rootScope) {
4278+
element = $compile('<div nested><div nested></div></div>')($rootScope);
4279+
4280+
expect(log).toEqual('bar; false');
4281+
});
4282+
});
4283+
4284+
42414285
it('should get required parent controller when the question mark precedes the ^^', function() {
42424286
module(function() {
42434287
directive('nested', function(log) {
@@ -4546,6 +4590,30 @@ describe('$compile', function() {
45464590
});
45474591

45484592

4593+
it('should respect explicit controller return value when using controllerAs', function() {
4594+
module(function() {
4595+
directive('main', function() {
4596+
return {
4597+
templateUrl: 'main.html',
4598+
transclude: true,
4599+
scope: {},
4600+
controller: function() {
4601+
this.name = 'lucas';
4602+
return {name: 'george'};
4603+
},
4604+
controllerAs: 'mainCtrl'
4605+
};
4606+
});
4607+
});
4608+
inject(function($templateCache, $compile, $rootScope) {
4609+
$templateCache.put('main.html', '<span>template:{{mainCtrl.name}} <div ng-transclude></div></span>');
4610+
element = $compile('<div main>transclude:{{mainCtrl.name}}</div>')($rootScope);
4611+
$rootScope.$apply();
4612+
expect(element.text()).toBe('template:george transclude:');
4613+
});
4614+
});
4615+
4616+
45494617
it('should support controller alias', function() {
45504618
module(function($controllerProvider) {
45514619
$controllerProvider.register('MainCtrl', function() {
@@ -5547,6 +5615,41 @@ describe('$compile', function() {
55475615
});
55485616
});
55495617

5618+
it('should copy the explicit return value to all clones', function() {
5619+
module(function() {
5620+
directive('makeThree', valueFn({
5621+
transclude: 'content',
5622+
controller: function($transclude) {
5623+
this.foo = 'baz';
5624+
return {transclude:$transclude, foo: 'bar'};
5625+
},
5626+
link: function(scope, el, attr, ctrl) {
5627+
ctrl.transclude(cloneAttach);
5628+
ctrl.transclude(cloneAttach);
5629+
ctrl.transclude(cloneAttach);
5630+
5631+
function cloneAttach(clone) {
5632+
el.append(clone);
5633+
}
5634+
}
5635+
}));
5636+
5637+
directive('nested', function(log) {
5638+
return {
5639+
require: '^^makeThree',
5640+
link: function(scope, element, attrs, controller) {
5641+
log(controller.foo);
5642+
}
5643+
};
5644+
});
5645+
});
5646+
inject(function(log, $compile) {
5647+
element = $compile('<div make-three><div nested></div></div>')($rootScope);
5648+
$rootScope.$apply();
5649+
expect(log).toEqual('bar; bar; bar');
5650+
});
5651+
});
5652+
55505653
it('should provide the $transclude controller local as 5th argument to the pre and post-link function', function() {
55515654
var ctrlTransclude, preLinkTransclude, postLinkTransclude;
55525655
module(function() {

0 commit comments

Comments
 (0)