Skip to content

Commit c2270e1

Browse files
feat(ngMock.$componentController): add helper to instantiate controllers for components
Closes angular#13683
1 parent ca376fa commit c2270e1

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/ng/compile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10541054
*/
10551055
this.component = function registerComponent(name, options) {
10561056
var controller = options.controller || function() {};
1057-
this.$$componentControllers[name] = controller;
1057+
var ident = identifierForController(options.controller) || options.controllerAs || '$ctrl';
1058+
this.$$componentControllers[name] = { controller: controller, ident: ident};
10581059

10591060
function factory($injector) {
10601061
function makeInjectable(fn) {
@@ -1070,7 +1071,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10701071
var template = (!options.template && !options.templateUrl ? '' : options.template);
10711072
return {
10721073
controller: controller,
1073-
controllerAs: identifierForController(options.controller) || options.controllerAs || '$ctrl',
1074+
controllerAs: ident,
10741075
template: makeInjectable(template),
10751076
templateUrl: makeInjectable(options.templateUrl),
10761077
transclude: options.transclude,

src/ngMock/angular-mocks.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,18 +2166,24 @@ angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
21662166
* @name $componentController
21672167
* @description
21682168
* A service that can be used to create instances of component controllers.
2169+
* <div class="alert alert-info">
2170+
* Be aware that the controller will be instantiated and attached to the scope as specified in
2171+
* the component definition object. That means that you must always provide a `$scope` object
2172+
* in the `locals` param.
2173+
* </div>
21692174
* @param {string} componentName the name of the component whose controller we want to instantiate
21702175
* @param {Object} locals Injection locals for Controller.
21712176
* @param {Object=} bindings Properties to add to the controller before invoking the constructor. This is used
21722177
* to simulate the `bindToController` feature and simplify certain kinds of tests.
2178+
* @param {string=} ident Override the property name to use when attaching the controller to the scope.
21732179
* @return {Object} Instance of requested controller.
21742180
*/
21752181
angular.mock.$ComponentControllerProvider = ['$compileProvider', function($compileProvider) {
21762182
return {
21772183
$get: ['$controller', function($controller) {
21782184
return function $componentController(componentName, locals, bindings, ident) {
2179-
var controller = $compileProvider.$$componentControllers[componentName];
2180-
return $controller(controller, locals, bindings, ident);
2185+
var controllerInfo = $compileProvider.$$componentControllers[componentName];
2186+
return $controller(controllerInfo.controller, locals, bindings, ident || controllerInfo.ident);
21812187
};
21822188
}]
21832189
};

test/ngMock/angular-mocksSpec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,7 @@ describe('ngMock', function() {
18751875
var $scope = {};
18761876
var ctrl = $componentController('test', { $scope: $scope, a: 'A', b: 'B' }, { x: 'X', y: 'Y' });
18771877
expect(ctrl).toEqual({ $scope: $scope, a: 'A', b: 'B', x: 'X', y: 'Y' });
1878+
expect($scope.$ctrl).toBe(ctrl);
18781879
});
18791880
});
18801881

@@ -1894,6 +1895,7 @@ describe('ngMock', function() {
18941895
var $scope = {};
18951896
var ctrl = $componentController('test', { $scope: $scope, a: 'A', b: 'B' }, { x: 'X', y: 'Y' });
18961897
expect(ctrl).toEqual({ $scope: $scope, a: 'A', b: 'B', x: 'X', y: 'Y' });
1898+
expect($scope.$ctrl).toBe(ctrl);
18971899
});
18981900
});
18991901

@@ -1913,6 +1915,7 @@ describe('ngMock', function() {
19131915
var $scope = {};
19141916
var ctrl = $componentController('test', { $scope: $scope, a: 'A', b: 'B' }, { x: 'X', y: 'Y' });
19151917
expect(ctrl).toEqual({ $scope: $scope, a: 'A', b: 'B', x: 'X', y: 'Y' });
1918+
expect($scope.$ctrl).toBe(ctrl);
19161919
});
19171920
});
19181921

@@ -1932,9 +1935,10 @@ describe('ngMock', function() {
19321935
var $scope = {};
19331936
var ctrl = $componentController('test', { $scope: $scope, a: 'A', b: 'B' }, { x: 'X', y: 'Y' });
19341937
expect(ctrl).toEqual({ $scope: $scope, a: 'A', b: 'B', x: 'X', y: 'Y' });
1935-
expect($scope.testCtrl instanceof TestController).toBe(true);
1938+
expect($scope.testCtrl).toBe(ctrl);
19361939
});
1937-
}); });
1940+
});
1941+
});
19381942
});
19391943

19401944

0 commit comments

Comments
 (0)