From d8d68f6e93f9822b56b7828e9a9179d916ce9a3c Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 13 Apr 2016 14:33:15 +0100 Subject: [PATCH 1/2] feat($componentController): provide isolated scope if none is passed --- src/ngMock/angular-mocks.js | 13 ++++++++++--- test/ngMock/angular-mocksSpec.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index cb50624541c2..47f0bb769d28 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -2196,8 +2196,8 @@ angular.mock.$ControllerDecorator = ['$delegate', function($delegate) { * A service that can be used to create instances of component controllers. *
* Be aware that the controller will be instantiated and attached to the scope as specified in - * the component definition object. That means that you must always provide a `$scope` object - * in the `locals` param. + * the component definition object. If you do not provide a `$scope` object in the `locals` param + * then the helper will create a new isolated scope as a child of `$rootScope`. *
* @param {string} componentName the name of the component whose controller we want to instantiate * @param {Object} locals Injection locals for Controller. @@ -2207,7 +2207,7 @@ angular.mock.$ControllerDecorator = ['$delegate', function($delegate) { * @return {Object} Instance of requested controller. */ angular.mock.$ComponentControllerProvider = ['$compileProvider', function($compileProvider) { - this.$get = ['$controller','$injector', function($controller,$injector) { + this.$get = ['$controller','$injector', '$rootScope', function($controller, $injector, $rootScope) { return function $componentController(componentName, locals, bindings, ident) { // get all directives associated to the component name var directives = $injector.get(componentName + 'Directive'); @@ -2225,6 +2225,13 @@ angular.mock.$ComponentControllerProvider = ['$compileProvider', function($compi } // get the info of the component var directiveInfo = candidateDirectives[0]; + // create a scope if needed + if (!locals) { + locals = {}; + } + if (!locals.$scope) { + locals.$scope = locals.$scope || $rootScope.$new(true); + } return $controller(directiveInfo.controller, locals, bindings, ident || directiveInfo.controllerAs); }; }]; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 943407b9c3b5..f848419e0734 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -2089,6 +2089,26 @@ describe('ngMock', function() { }).toThrowError('Too many components found'); }); }); + + it('should create an isolated child of $rootScope, if no `$scope` local is provided', function() { + function TestController($scope) { + this.$scope = $scope; + } + module(function($compileProvider) { + $compileProvider.component('test', { + controller: TestController + }); + }); + inject(function($componentController, $rootScope) { + var $ctrl = $componentController('test'); + expect($ctrl.$scope).toBeDefined(); + expect($ctrl.$scope.$parent).toBe($rootScope); + // check it is isolated + $rootScope.a = 17; + $ctrl.$scope.a = 42; + expect($rootScope.a).toEqual(17); + }); + }); }); }); From 9d033a4f2e49fcd149601e1fe31fa9e230a80686 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Wed, 13 Apr 2016 17:59:32 +0100 Subject: [PATCH 2/2] feat($componentController): provide isolated scope if none is passed --- src/ngMock/angular-mocks.js | 8 ++------ test/ngMock/angular-mocksSpec.js | 1 + 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 47f0bb769d28..99e80f40d2ea 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -2226,12 +2226,8 @@ angular.mock.$ComponentControllerProvider = ['$compileProvider', function($compi // get the info of the component var directiveInfo = candidateDirectives[0]; // create a scope if needed - if (!locals) { - locals = {}; - } - if (!locals.$scope) { - locals.$scope = locals.$scope || $rootScope.$new(true); - } + locals = locals || {}; + locals.$scope = locals.$scope || $rootScope.$new(true); return $controller(directiveInfo.controller, locals, bindings, ident || directiveInfo.controllerAs); }; }]; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index f848419e0734..985eabc375bc 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -2105,6 +2105,7 @@ describe('ngMock', function() { expect($ctrl.$scope.$parent).toBe($rootScope); // check it is isolated $rootScope.a = 17; + expect($ctrl.$scope.a).toBeUndefined(); $ctrl.$scope.a = 42; expect($rootScope.a).toEqual(17); });