diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/directivesSpec.js | 84 | ||||
| -rw-r--r-- | test/scenario/RunnerSpec.js | 3 | ||||
| -rw-r--r-- | test/scenario/SpecRunnerSpec.js | 8 | ||||
| -rw-r--r-- | test/service/formFactorySpec.js | 22 | ||||
| -rw-r--r-- | test/service/routeSpec.js | 21 | ||||
| -rw-r--r-- | test/service/scopeSpec.js | 31 | ||||
| -rw-r--r-- | test/widgetsSpec.js | 8 |
7 files changed, 77 insertions, 100 deletions
diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 1825dc49..7600a9c8 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -460,60 +460,66 @@ describe("directive", function() { }); describe('ng:controller', function() { + var element; - var temp; + beforeEach(inject(function($window) { + $window.Greeter = function($scope) { + // private stuff (not exported to scope) + this.prefix = 'Hello '; - beforeEach(function() { - temp = window.temp = {}; - temp.Greeter = function() { - this.$root.greeter = this; - this.greeting = 'hello'; - this.suffix = '!'; + // public stuff (exported to scope) + var ctrl = this; + $scope.name = 'Misko'; + $scope.greet = function(name) { + return ctrl.prefix + name + ctrl.suffix; + }; + + $scope.protoGreet = bind(this, this.protoGreet); }; - temp.Greeter.prototype = { - greet: function(name) { - return this.greeting + ' ' + name + this.suffix; + $window.Greeter.prototype = { + suffix: '!', + protoGreet: function(name) { + return this.prefix + name + this.suffix; } }; - }); + + $window.Child = function($scope) { + $scope.name = 'Adam'; + }; + })); afterEach(function() { - window.temp = undefined; + dealoc(element); }); - it('should bind', inject(function($rootScope, $compile) { - var element = $compile('<div ng:controller="temp.Greeter"></div>')($rootScope); - expect($rootScope.greeter.greeting).toEqual('hello'); - expect($rootScope.greeter.greet('misko')).toEqual('hello misko!'); + + it('should instantiate controller and bind methods', inject(function($compile, $rootScope) { + element = $compile('<div ng:controller="Greeter">{{greet(name)}}</div>')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('Hello Misko!'); })); - it('should support nested controllers', inject(function($rootScope, $compile) { - temp.ChildGreeter = function() { - this.greeting = 'hey'; - this.$root.childGreeter = this; - }; - temp.ChildGreeter.prototype = { - greet: function() { - return this.greeting + ' dude' + this.suffix; - } - }; - var element = $compile('<div ng:controller="temp.Greeter"><div ng:controller="temp.ChildGreeter">{{greet("misko")}}</div></div>')($rootScope); - expect($rootScope.greeting).not.toBeDefined(); - expect($rootScope.greeter.greeting).toEqual('hello'); - expect($rootScope.greeter.greet('misko')).toEqual('hello misko!'); - expect($rootScope.greeter.greeting).toEqual('hello'); - expect($rootScope.childGreeter.greeting).toEqual('hey'); - expect($rootScope.childGreeter.$parent.greeting).toEqual('hello'); + + it('should allow nested controllers', inject(function($compile, $rootScope) { + element = $compile('<div ng:controller="Greeter"><div ng:controller="Child">{{greet(name)}}</div></div>')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('Hello Adam!'); + dealoc(element); + + element = $compile('<div ng:controller="Greeter"><div ng:controller="Child">{{protoGreet(name)}}</div></div>')($rootScope); $rootScope.$digest(); - expect(element.text()).toEqual('hey dude!'); + expect(element.text()).toBe('Hello Adam!'); })); - it('should infer injection arguments', inject(function($rootScope, $compile, $http) { - temp.MyController = function($http) { - this.$root.someService = $http; + + it('should instantiate controller defined on scope', inject(function($compile, $rootScope) { + $rootScope.Greeter = function($scope) { + $scope.name = 'Vojta'; }; - var element = $compile('<div ng:controller="temp.MyController"></div>')($rootScope); - expect($rootScope.someService).toBe($http); + + element = $compile('<div ng:controller="Greeter">{{name}}</div>')($rootScope); + $rootScope.$digest(); + expect(element.text()).toBe('Vojta'); })); }); diff --git a/test/scenario/RunnerSpec.js b/test/scenario/RunnerSpec.js index 15bcc4b0..c4ad6f95 100644 --- a/test/scenario/RunnerSpec.js +++ b/test/scenario/RunnerSpec.js @@ -43,9 +43,6 @@ describe('angular.scenario.Runner', function() { location: {} }; runner = new angular.scenario.Runner($window); - runner.createSpecRunner_ = function(scope) { - return scope.$new(MockSpecRunner); - }; runner.on('SpecError', angular.mock.rethrow); runner.on('StepError', angular.mock.rethrow); }); diff --git a/test/scenario/SpecRunnerSpec.js b/test/scenario/SpecRunnerSpec.js index 4cffc63a..c104a9b7 100644 --- a/test/scenario/SpecRunnerSpec.js +++ b/test/scenario/SpecRunnerSpec.js @@ -40,7 +40,13 @@ describe('angular.scenario.SpecRunner', function() { }; $root.application = new ApplicationMock($window); $root.$window = $window; - runner = $root.$new(angular.scenario.SpecRunner); + runner = $root.$new(); + + var Cls = angular.scenario.SpecRunner; + for (var name in Cls.prototype) + runner[name] = angular.bind(runner, Cls.prototype[name]); + + Cls.call(runner); })); it('should bind futures to the spec', function() { diff --git a/test/service/formFactorySpec.js b/test/service/formFactorySpec.js index fbe601c6..1a23aa49 100644 --- a/test/service/formFactorySpec.js +++ b/test/service/formFactorySpec.js @@ -13,23 +13,24 @@ describe('$formFactory', function() { var scope; var log; - function WidgetCtrl($formFactory){ - this.$formFactory = $formFactory; + function WidgetCtrl($formFactory, $scope) { log += '<init>'; - this.$render = function() { + $scope.$render = function() { log += '$render();'; }; - this.$on('$validate', function(e){ + $scope.$on('$validate', function(e){ log += '$validate();'; }); + + this.$formFactory = $formFactory; } - WidgetCtrl.$inject = ['$formFactory']; + WidgetCtrl.$inject = ['$formFactory', '$scope']; WidgetCtrl.prototype = { - getFormFactory: function() { - return this.$formFactory; - } + getFormFactory: function() { + return this.$formFactory; + } }; beforeEach(inject(function($rootScope, $formFactory) { @@ -70,11 +71,6 @@ describe('$formFactory', function() { expect(widget.$modelValue).toEqual('xyz'); })); - - - it('should have controller prototype methods', inject(function($rootScope, $formFactory) { - expect(widget.getFormFactory()).toEqual($formFactory); - })); }); diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 95560d29..1bb1312f 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -112,7 +112,7 @@ describe('$route', function() { inject(function($route, $location, $rootScope) { var onChangeSpy = jasmine.createSpy('onChange'); - function NotFoundCtrl() {this.notFoundProp = 'not found!';} + function NotFoundCtrl($scope) {$scope.notFoundProp = 'not found!';} $route.when('/foo', {template: 'foo.html'}); $route.otherwise({template: '404.html', controller: NotFoundCtrl}); @@ -169,10 +169,11 @@ describe('$route', function() { it('should infer arguments in injection', inject(function($route, $location, $rootScope) { - $route.when('/test', {controller: function($route){ this.$route = $route; }}); + var injectedRoute; + $route.when('/test', {controller: function($route) {injectedRoute = $route;}}); $location.path('/test'); $rootScope.$digest(); - expect($route.current.scope.$route).toBe($route); + expect(injectedRoute).toBe($route); })); @@ -304,9 +305,9 @@ describe('$route', function() { $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); $rootScope.$on('$beforeRouteChange', reloaded); - function FooCtrl() { + function FooCtrl($scope) { reloaded(); - this.$on('$routeUpdate', routeUpdateEvent); + $scope.$on('$routeUpdate', routeUpdateEvent); } expect(reloaded).not.toHaveBeenCalled(); @@ -368,8 +369,8 @@ describe('$route', function() { $route.when('/foo', {controller: FooCtrl}); $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); - function FooCtrl() { - this.$watch(function() { + function FooCtrl($scope) { + $scope.$watch(function() { return $route.current.params; }, function(scope, value) { routeParams(value); @@ -414,10 +415,10 @@ describe('$route', function() { } function createController(name) { - return function() { + return function($scope) { log.push('init-' + name); - this.$on('$destroy', logger('destroy-' + name)); - this.$on('$routeUpdate', logger('route-update')); + $scope.$on('$destroy', logger('destroy-' + name)); + $scope.$on('$routeUpdate', logger('route-update')); }; } diff --git a/test/service/scopeSpec.js b/test/service/scopeSpec.js index 96271bc9..68ef2834 100644 --- a/test/service/scopeSpec.js +++ b/test/service/scopeSpec.js @@ -53,35 +53,6 @@ describe('Scope', function() { $rootScope.a = 123; expect(child.a).toEqual(123); })); - - - it('should instantiate controller and bind functions', inject(function($rootScope) { - function Cntl($browser, name) { - this.$browser = $browser; - this.callCount = 0; - this.name = name; - } - Cntl.$inject = ['$browser', 'name']; - - Cntl.prototype = { - myFn: function() { - expect(this).toEqual(cntl); - this.callCount++; - } - }; - - var cntl = $rootScope.$new(Cntl, {name:'misko'}); - - expect($rootScope.$browser).toBeUndefined(); - expect($rootScope.myFn).toBeUndefined(); - - expect(cntl.$browser).toBeDefined(); - expect(cntl.name).toEqual('misko'); - - cntl.myFn(); - cntl.$new().myFn(); - expect(cntl.callCount).toEqual(2); - })); }); @@ -341,7 +312,7 @@ describe('Scope', function() { $rootScope.$digest(); expect(isNaN(log.shift())).toBe(true); //jasmine's toBe and toEqual don't work well with NaNs expect(log).toEqual([undefined, '', false, {}, 23]); - log = [] + log = []; $rootScope.$digest(); expect(log).toEqual([]); })); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index f119174f..88d9e1b8 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -713,12 +713,12 @@ describe('widget', function() { $route.when('/foo', {controller: ParentCtrl, template: 'viewPartial.html'}); $rootScope.log = []; - function ParentCtrl() { - this.log.push('parent'); + function ParentCtrl($scope) { + $scope.log.push('parent'); } - $rootScope.ChildCtrl = function() { - this.log.push('child'); + $rootScope.ChildCtrl = function($scope) { + $scope.log.push('child'); }; $location.path('/foo'); |
