From 16597e8b52bdfe34b2239a5ab86a839fa8e980d6 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 2 Nov 2011 21:37:06 -0700 Subject: refactor($service): removed almost all references to scope.$service - still need to remove from factory --- test/BinderSpec.js | 4 +-- test/ResourceSpec.js | 5 ++-- test/directivesSpec.js | 4 +-- test/service/routeSpec.js | 26 ++++++++++-------- test/widget/inputSpec.js | 44 +++++++++++++++--------------- test/widgetsSpec.js | 68 ++++++++++++++++++++++++++++++----------------- 6 files changed, 87 insertions(+), 64 deletions(-) (limited to 'test') diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 6ab94401..01e61b57 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -189,7 +189,7 @@ describe('Binder', function() { assertTrue(html.indexOf('action="foo();"') > 0 ); }); - it('RepeaterAdd', inject(function($rootScope, $compile) { + it('RepeaterAdd', inject(function($rootScope, $compile, $browser) { var element = $compile('
')($rootScope); $rootScope.items = [{x:'a'}, {x:'b'}]; $rootScope.$apply(); @@ -200,7 +200,7 @@ describe('Binder', function() { first.val('ABC'); browserTrigger(first, 'keydown'); - $rootScope.$service('$browser').defer.flush(); + $browser.defer.flush(); expect($rootScope.items[0].x).toEqual('ABC'); })); diff --git a/test/ResourceSpec.js b/test/ResourceSpec.js index f648eb1b..2091a936 100644 --- a/test/ResourceSpec.js +++ b/test/ResourceSpec.js @@ -230,10 +230,9 @@ describe("resource", function() { expect(person.name).toEqual('misko'); })); - it('should return the same object when verifying the cache', inject(function($rootScope, $compile) { + it('should return the same object when verifying the cache', + inject(function($rootScope, $compile, $browser, $resource) { $compile('
')($rootScope); - var $browser = $rootScope.$service('$browser'); - var $resource = $rootScope.$service('$resource'); var Person = $resource('/Person/:id', null, {query: {method:'GET', isArray: true, verifyCache: true}}); $browser.xhr.expectGET('/Person/123').respond('[\n{\n"name":\n"misko"\n}\n]'); var person = Person.query({id:123}); diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 96d0993b..7acf87bc 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -512,12 +512,12 @@ describe("directive", function() { expect(element.text()).toEqual('hey dude!'); })); - it('should infer injection arguments', inject(function($rootScope, $compile) { + it('should infer injection arguments', inject(function($rootScope, $compile, $xhr) { temp.MyController = function($xhr){ this.$root.someService = $xhr; }; var element = $compile('
')($rootScope); - expect($rootScope.someService).toBe($rootScope.$service('$xhr')); + expect($rootScope.someService).toBe($xhr); })); }); diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index 26ae17e5..fed3faef 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -10,7 +10,8 @@ describe('$route', function() { log += ';'; } - $route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template: 'Chapter.html'}); + $route.when('/Book/:book/Chapter/:chapter', + {controller: BookChapter, template: 'Chapter.html'}); $route.when('/Blank'); $rootScope.$on('$beforeRouteChange', function(event, next, current) { log += 'before();'; @@ -50,7 +51,8 @@ describe('$route', function() { })); - it('should match a route that contains special chars in the path', inject(function($route, $location, $rootScope) { + it('should match a route that contains special chars in the path', + inject(function($route, $location, $rootScope) { $route.when('/$test.23/foo(bar)/:baz', {template: 'test.html'}); $location.path('/test'); @@ -71,7 +73,8 @@ describe('$route', function() { })); - it('should change route even when only search param changes', inject(function($route, $location, $rootScope) { + it('should change route even when only search param changes', + inject(function($route, $location, $rootScope) { var callback = jasmine.createSpy('onRouteChange'); $route.when('/test', {template: 'test.html'}); @@ -105,7 +108,8 @@ describe('$route', function() { })); - it('should handle unknown routes with "otherwise" route definition', inject(function($route, $location, $rootScope) { + it('should handle unknown routes with "otherwise" route definition', + inject(function($route, $location, $rootScope) { var onChangeSpy = jasmine.createSpy('onChange'); function NotFoundCtrl() {this.notFoundProp = 'not found!';} @@ -229,7 +233,8 @@ describe('$route', function() { })); - it('should allow custom redirectTo function to be used', inject(function($route, $location, $rootScope) { + it('should allow custom redirectTo function to be used', + inject(function($route, $location, $rootScope) { $route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'}); $route.when('/foo/:id', {redirectTo: customRedirectFn}); @@ -267,9 +272,8 @@ describe('$route', function() { describe('reloadOnSearch', function() { it('should reload a route when reloadOnSearch is enabled and .search() changes', - inject(function($route, $location, $rootScope) { - var $routeParams = $rootScope.$service('$routeParams'), - reloaded = jasmine.createSpy('route reload'); + inject(function($route, $location, $rootScope, $routeParams) { + var reloaded = jasmine.createSpy('route reload'); $route.when('/foo', {controller: FooCtrl}); $rootScope.$on('$beforeRouteChange', reloaded); @@ -399,9 +403,9 @@ describe('$route', function() { describe('reload', function() { - it('should reload even if reloadOnSearch is false', inject(function($route, $location, $rootScope) { - var $routeParams = $rootScope.$service('$routeParams'), - count = 0; + it('should reload even if reloadOnSearch is false', + inject(function($route, $location, $rootScope, $routeParams) { + var count = 0; $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); diff --git a/test/widget/inputSpec.js b/test/widget/inputSpec.js index cd70cea4..8a35407b 100644 --- a/test/widget/inputSpec.js +++ b/test/widget/inputSpec.js @@ -5,8 +5,9 @@ describe('widget: input', function() { var $compile = null; var doc = null; - beforeEach(inject(function($rootScope, $compile) { + beforeEach(inject(function($rootScope, $compile, $browser) { scope = $rootScope; + defer = $browser.defer; set$compile($compile); element = null; compile = function(html, parent) { @@ -18,7 +19,6 @@ describe('widget: input', function() { } $compile(element)(scope); scope.$apply(); - defer = scope.$service('$browser').defer; return scope; }; })); @@ -88,7 +88,7 @@ describe('widget: input', function() { }; inputElement.val(' a '); browserTrigger(inputElement); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.name).toEqual('a'); expect(log).toEqual('change();'); }); @@ -128,7 +128,7 @@ describe('widget: input', function() { element.val('Kai'); browserTrigger(element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.name).toEqual('Kai'); }); @@ -140,7 +140,7 @@ describe('widget: input', function() { expect(scope.name).toEqual("Misko"); expect(scope.count).toEqual(0); browserTrigger(element, 'keydown'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.name).toEqual("Misko"); expect(scope.count).toEqual(0); }); @@ -166,7 +166,7 @@ describe('widget: input', function() { element.val('1, 2, 3'); browserTrigger(element); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.list).toEqual(['1', '2', '3']); }); @@ -191,7 +191,7 @@ describe('widget: input', function() { } catch (e){} scope.$element.val('123X'); browserTrigger(scope.$element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.$element.val()).toEqual('123X'); expect(scope.age).toEqual(123); expect(scope.$element).toBeInvalid(); @@ -206,25 +206,25 @@ describe('widget: input', function() { scope.$element.val('a '); browserTrigger(scope.$element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.$element.val()).toEqual('a '); expect(scope.list).toEqual(['a']); scope.$element.val('a ,'); browserTrigger(scope.$element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.$element.val()).toEqual('a ,'); expect(scope.list).toEqual(['a']); scope.$element.val('a , '); browserTrigger(scope.$element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.$element.val()).toEqual('a , '); expect(scope.list).toEqual(['a']); scope.$element.val('a , b'); browserTrigger(scope.$element, 'change'); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(scope.$element.val()).toEqual('a , b'); expect(scope.list).toEqual(['a', 'b']); }); @@ -293,9 +293,9 @@ describe('widget: input', function() { }); - it("should process required", function() { + it("should process required", inject(function($formFactory) { compile('', jqLite(document.body)); - expect(scope.$service('$formFactory').rootForm.p.$required).toBe(true); + expect($formFactory.rootForm.p.$required).toBe(true); expect(element.hasClass('ng-invalid')).toBeTruthy(); scope.price = 'xxx'; @@ -304,9 +304,9 @@ describe('widget: input', function() { element.val(''); browserTrigger(element); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(element.hasClass('ng-invalid')).toBeTruthy(); - }); + })); it('should allow bindings on ng:required', function() { @@ -328,7 +328,7 @@ describe('widget: input', function() { element.val('abc'); browserTrigger(element); - scope.$service('$browser').defer.flush(); + defer.flush(); expect(element).toBeValid(); }); @@ -458,18 +458,18 @@ describe('widget: input', function() { describe('scope declaration', function() { - it('should read the declaration from scope', inject(function($rootScope, $compile) { - var input, $formFactory; + it('should read the declaration from scope', inject(function($rootScope, $compile, $formFactory) { + var input, formFactory; var element = angular.element(''); $rootScope.MyType = function($f, i) { input = i; - $formFactory = $f; + formFactory = $f; }; $rootScope.MyType.$inject = ['$formFactory', '$element']; $compile(element)($rootScope); - expect($formFactory).toBe($rootScope.$service('$formFactory')); + expect(formFactory).toBe($formFactory); expect(input[0]).toBe(element[0]); })); @@ -523,7 +523,7 @@ describe('widget: input', function() { if (value != undefined) { scope.$element.val(value); browserTrigger(element, 'keydown'); - scope.$service('$browser').defer.flush(); + defer.flush(); } scope.$digest(); } @@ -587,7 +587,7 @@ describe('widget: input', function() { el.val('xx'); browserTrigger(el, 'keydown'); - expect(function() { $rootScope.$service('$browser').defer.flush(); }). + expect(function() { defer.flush(); }). toThrow('Expected fooRegexp to be a RegExp but was undefined'); dealoc(el); diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index fc898f83..63182374 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -1,6 +1,10 @@ 'use strict'; describe("widget", function() { + beforeEach(inject(function($provide){ + $provide.factory('$xhrCache', ['$xhr.cache', identity]); + })); + describe('ng:switch', inject(function($rootScope, $compile) { it('should switch on value change', inject(function($rootScope, $compile) { var element = $compile( @@ -56,25 +60,26 @@ describe("widget", function() { describe('ng:include', inject(function($rootScope, $compile) { - it('should include on external file', inject(function($rootScope, $compile) { + it('should include on external file', inject(function($rootScope, $compile, $xhrCache) { var element = jqLite(''); var element = $compile(element)($rootScope); $rootScope.childScope = $rootScope.$new(); $rootScope.childScope.name = 'misko'; $rootScope.url = 'myUrl'; - $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; + $xhrCache.data.myUrl = {value:'{{name}}'}; $rootScope.$digest(); expect(element.text()).toEqual('misko'); })); - it('should remove previously included text if a falsy value is bound to src', inject(function($rootScope, $compile) { + it('should remove previously included text if a falsy value is bound to src', + inject(function($rootScope, $compile, $xhrCache) { var element = jqLite(''); var element = $compile(element)($rootScope); $rootScope.childScope = $rootScope.$new(); $rootScope.childScope.name = 'igor'; $rootScope.url = 'myUrl'; - $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; + $xhrCache.data.myUrl = {value:'{{name}}'}; $rootScope.$digest(); expect(element.text()).toEqual('igor'); @@ -86,11 +91,11 @@ describe("widget", function() { })); - it('should allow this for scope', inject(function($rootScope, $compile) { + it('should allow this for scope', inject(function($rootScope, $compile, $xhrCache) { var element = jqLite(''); var element = $compile(element)($rootScope); $rootScope.url = 'myUrl'; - $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{"abc"}}'}; + $xhrCache.data.myUrl = {value:'{{"abc"}}'}; $rootScope.$digest(); // TODO(misko): because we are using scope==this, the eval gets registered // during the flush phase and hence does not get called. @@ -102,28 +107,29 @@ describe("widget", function() { })); - it('should evaluate onload expression when a partial is loaded', inject(function($rootScope, $compile) { + it('should evaluate onload expression when a partial is loaded', + inject(function($rootScope, $compile, $xhrCache) { var element = jqLite(''); var element = $compile(element)($rootScope); expect($rootScope.loaded).not.toBeDefined(); $rootScope.url = 'myUrl'; - $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; + $xhrCache.data.myUrl = {value:'my partial'}; $rootScope.$digest(); expect(element.text()).toEqual('my partial'); expect($rootScope.loaded).toBe(true); })); - it('should destroy old scope', inject(function($rootScope, $compile) { + it('should destroy old scope', inject(function($rootScope, $compile, $xhrCache) { var element = jqLite(''); var element = $compile(element)($rootScope); expect($rootScope.$$childHead).toBeFalsy(); $rootScope.url = 'myUrl'; - $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; + $xhrCache.data.myUrl = {value:'my partial'}; $rootScope.$digest(); expect($rootScope.$$childHead).toBeTruthy(); @@ -135,7 +141,8 @@ describe("widget", function() { describe('a', inject(function($rootScope, $compile) { - it('should prevent default action to be executed when href is empty', inject(function($rootScope, $compile) { + it('should prevent default action to be executed when href is empty', + inject(function($rootScope, $compile) { var orgLocation = document.location.href, preventDefaultCalled = false, event; @@ -152,7 +159,8 @@ describe("widget", function() { } else { event = document.createEvent('MouseEvent'); - event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); + event.initMouseEvent( + 'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); event.preventDefaultOrg = event.preventDefault; event.preventDefault = function() { @@ -231,7 +239,8 @@ describe("widget", function() { $log.error.logs.shift(); })); - it('should expose iterator offset as $index when iterating over arrays', inject(function($rootScope, $compile) { + it('should expose iterator offset as $index when iterating over arrays', + inject(function($rootScope, $compile) { var element = $compile( '