diff options
| author | Misko Hevery | 2011-10-17 16:56:56 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2011-11-14 16:39:31 -0800 | 
| commit | 48697a2b86dbb12ea8de64cc5fece7caf68b321e (patch) | |
| tree | 1fa50659f0bb5de2640dea2a2e5bb5628f2bb14a /test/widgetsSpec.js | |
| parent | 93b777c916ccff243c5a6080bf5f39860ac7bf39 (diff) | |
| download | angular.js-48697a2b86dbb12ea8de64cc5fece7caf68b321e.tar.bz2 | |
refactor(injector): turn scope into a service
- turn scope into a $rootScope service.
- injector is now a starting point for creating angular application.
- added inject() method which wraps jasmine its/beforeEach/afterEach,
  and which allows configuration and injection of services.
- refactor tests to use inject() where possible
BREAK:
- removed angular.scope() method
Diffstat (limited to 'test/widgetsSpec.js')
| -rw-r--r-- | test/widgetsSpec.js | 750 | 
1 files changed, 371 insertions, 379 deletions
diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index e75b2592..1a4c5e6c 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -1,165 +1,145 @@  'use strict';  describe("widget", function() { -  var compile = null, element = null, scope = null; - -  beforeEach(function() { -    scope = null; -    element = null; -    compile = function(html, parent) { -      if (parent) { -        parent.html(html); -        element = parent.children(); -      } else { -        element = jqLite(html); -      } -      scope = angular.compile(element)(); -      scope.$apply(); -      return scope; -    }; -  }); - -  afterEach(function() { -    dealoc(element); -  }); - - -  describe('ng:switch', function() { -    it('should switch on value change', function() { -      compile('<ng:switch on="select">' + +  describe('ng:switch', inject(function($rootScope) { +    it('should switch on value change', inject(function($rootScope) { +      var element = angular.compile('<ng:switch on="select">' +            '<div ng:switch-when="1">first:{{name}}</div>' +            '<div ng:switch-when="2">second:{{name}}</div>' +            '<div ng:switch-when="true">true:{{name}}</div>' + -        '</ng:switch>'); +        '</ng:switch>')($rootScope);        expect(element.html()).toEqual(''); -      scope.select = 1; -      scope.$apply(); +      $rootScope.select = 1; +      $rootScope.$apply();        expect(element.text()).toEqual('first:'); -      scope.name="shyam"; -      scope.$apply(); +      $rootScope.name="shyam"; +      $rootScope.$apply();        expect(element.text()).toEqual('first:shyam'); -      scope.select = 2; -      scope.$apply(); +      $rootScope.select = 2; +      $rootScope.$apply();        expect(element.text()).toEqual('second:shyam'); -      scope.name = 'misko'; -      scope.$apply(); +      $rootScope.name = 'misko'; +      $rootScope.$apply();        expect(element.text()).toEqual('second:misko'); -      scope.select = true; -      scope.$apply(); +      $rootScope.select = true; +      $rootScope.$apply();        expect(element.text()).toEqual('true:misko'); -    }); - -    it('should switch on switch-when-default', function() { -      compile('<ng:switch on="select">' + -                '<div ng:switch-when="1">one</div>' + -                '<div ng:switch-default>other</div>' + -              '</ng:switch>'); -      scope.$apply(); +    })); +     + +    it('should switch on switch-when-default', inject(function($rootScope) { +      var element = angular.compile( +        '<ng:switch on="select">' + +          '<div ng:switch-when="1">one</div>' + +          '<div ng:switch-default>other</div>' + +        '</ng:switch>')($rootScope); +      $rootScope.$apply();        expect(element.text()).toEqual('other'); -      scope.select = 1; -      scope.$apply(); +      $rootScope.select = 1; +      $rootScope.$apply();        expect(element.text()).toEqual('one'); -    }); - -    it('should call change on switch', function() { -      var scope = angular.compile('<ng:switch on="url" change="name=\'works\'"><div ng:switch-when="a">{{name}}</div></ng:switch>')(); -      scope.url = 'a'; -      scope.$apply(); -      expect(scope.name).toEqual(undefined); -      expect(scope.$element.text()).toEqual('works'); -      dealoc(scope); -    }); - -  }); - - -  describe('ng:include', function() { -    it('should include on external file', function() { +    })); + +     +    it('should call change on switch', inject(function($rootScope) { +      var element = angular.compile( +        '<ng:switch on="url" change="name=\'works\'">' + +          '<div ng:switch-when="a">{{name}}</div>' + +        '</ng:switch>')($rootScope); +      $rootScope.url = 'a'; +      $rootScope.$apply(); +      expect($rootScope.name).toEqual(undefined); +      expect(element.text()).toEqual('works'); +    })); +  })); + + +  describe('ng:include', inject(function($rootScope) { +    it('should include on external file', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.childScope = scope.$new(); -      scope.childScope.name = 'misko'; -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.childScope = $rootScope.$new(); +      $rootScope.childScope.name = 'misko'; +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; +      $rootScope.$digest();        expect(element.text()).toEqual('misko'); -      dealoc(scope); -    }); +    })); -    it('should remove previously included text if a falsy value is bound to src', function() { +     +    it('should remove previously included text if a falsy value is bound to src', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="childScope"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.childScope = scope.$new(); -      scope.childScope.name = 'igor'; -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.childScope = $rootScope.$new(); +      $rootScope.childScope.name = 'igor'; +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'{{name}}'}; +      $rootScope.$digest();        expect(element.text()).toEqual('igor'); -      scope.url = undefined; -      scope.$digest(); +      $rootScope.url = undefined; +      $rootScope.$digest();        expect(element.text()).toEqual(''); -      dealoc(scope); -    }); +    })); -    it('should allow this for scope', function() { +     +    it('should allow this for scope', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" scope="this"></ng:include>'); -      var scope = angular.compile(element)(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'{{"abc"}}'}; -      scope.$digest(); +      var element = angular.compile(element)($rootScope); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').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.        // I don't think passing 'this' makes sense. Does having scope on ng:include makes sense? -      // should we make scope="this" ilegal? -      scope.$digest(); +      // should we make scope="this" illegal? +      $rootScope.$digest();        expect(element.text()).toEqual('abc'); -      dealoc(element); -    }); +    })); -    it('should evaluate onload expression when a partial is loaded', function() { +     +    it('should evaluate onload expression when a partial is loaded', inject(function($rootScope) {        var element = jqLite('<ng:include src="url" onload="loaded = true"></ng:include>'); -      var scope = angular.compile(element)(); +      var element = angular.compile(element)($rootScope); -      expect(scope.loaded).not.toBeDefined(); +      expect($rootScope.loaded).not.toBeDefined(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; -      scope.$digest(); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; +      $rootScope.$digest();        expect(element.text()).toEqual('my partial'); -      expect(scope.loaded).toBe(true); -      dealoc(element); -    }); +      expect($rootScope.loaded).toBe(true); +    })); -    it('should destroy old scope', function() { +     +    it('should destroy old scope', inject(function($rootScope) {        var element = jqLite('<ng:include src="url"></ng:include>'); -      var scope = angular.compile(element)(); +      var element = angular.compile(element)($rootScope); -      expect(scope.$$childHead).toBeFalsy(); +      expect($rootScope.$$childHead).toBeFalsy(); -      scope.url = 'myUrl'; -      scope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; -      scope.$digest(); -      expect(scope.$$childHead).toBeTruthy(); +      $rootScope.url = 'myUrl'; +      $rootScope.$service('$xhr.cache').data.myUrl = {value:'my partial'}; +      $rootScope.$digest(); +      expect($rootScope.$$childHead).toBeTruthy(); -      scope.url = null; -      scope.$digest(); -      expect(scope.$$childHead).toBeFalsy(); -      dealoc(element); -    }); -  }); +      $rootScope.url = null; +      $rootScope.$digest(); +      expect($rootScope.$$childHead).toBeFalsy(); +    })); +  })); -  describe('a', function() { -    it('should prevent default action to be executed when href is empty', function() { +  describe('a', inject(function($rootScope) { +    it('should prevent default action to be executed when href is empty', inject(function($rootScope) {        var orgLocation = document.location.href,            preventDefaultCalled = false,            event; -      compile('<a href="">empty link</a>'); +      var element = angular.compile('<a href="">empty link</a>')($rootScope);        if (msie < 9) { @@ -185,185 +165,201 @@ describe("widget", function() {        }        expect(document.location.href).toEqual(orgLocation); -    }); -  }); +    })); +  })); -  describe('@ng:repeat', function() { -    it('should ng:repeat over array', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li></ul>'); +  describe('@ng:repeat', inject(function($rootScope) { +    it('should ng:repeat over array', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:init="suffix = \';\'" ng:bind="item + suffix"></li>' + +        '</ul>')($rootScope);        Array.prototype.extraProperty = "should be ignored";        // INIT -      scope.items = ['misko', 'shyam']; -      scope.$digest(); +      $rootScope.items = ['misko', 'shyam']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(2);        expect(element.text()).toEqual('misko;shyam;');        delete Array.prototype.extraProperty;        // GROW -      scope.items = ['adam', 'kai', 'brad']; -      scope.$digest(); +      $rootScope.items = ['adam', 'kai', 'brad']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(3);        expect(element.text()).toEqual('adam;kai;brad;');        // SHRINK -      scope.items = ['brad']; -      scope.$digest(); +      $rootScope.items = ['brad']; +      $rootScope.$digest();        expect(element.find('li').length).toEqual(1);        expect(element.text()).toEqual('brad;'); -    }); +    })); -    it('should ng:repeat over object', function() { -      var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>'); -      scope.items = {misko:'swe', shyam:'set'}; -      scope.$digest(); +    it('should ng:repeat over object', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li>' + +        '</ul>')($rootScope); +      $rootScope.items = {misko:'swe', shyam:'set'}; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:swe;shyam:set;'); -    }); +    })); -    it('should not ng:repeat over parent properties', function() { +    it('should not ng:repeat over parent properties', inject(function($rootScope) {        var Class = function() {};        Class.prototype.abc = function() {};        Class.prototype.value = 'abc'; -      var scope = compile('<ul><li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li></ul>'); -      scope.items = new Class(); -      scope.items.name = 'value'; -      scope.$digest(); +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, value) in items" ng:bind="key + \':\' + value + \';\' "></li>' + +        '</ul>')($rootScope); +      $rootScope.items = new Class(); +      $rootScope.items.name = 'value'; +      $rootScope.$digest();        expect(element.text()).toEqual('name:value;'); -    }); +    })); -    it('should error on wrong parsing of ng:repeat', function() { +    it('should error on wrong parsing of ng:repeat', inject(function($rootScope) {        expect(function() { -        compile('<ul><li ng:repeat="i dont parse"></li></ul>'); +        var element = angular.compile('<ul><li ng:repeat="i dont parse"></li></ul>')($rootScope);        }).toThrow("Expected ng:repeat in form of '_item_ in _collection_' but got 'i dont parse'.");        $logMock.error.logs.shift(); -    }); +    })); -    it('should expose iterator offset as $index when iterating over arrays', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ' + -                                  'ng:bind="item + $index + \'|\'"></li></ul>'); -      scope.items = ['misko', 'shyam', 'frodo']; -      scope.$digest(); +    it('should expose iterator offset as $index when iterating over arrays', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:bind="item + $index + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = ['misko', 'shyam', 'frodo']; +      $rootScope.$digest();        expect(element.text()).toEqual('misko0|shyam1|frodo2|'); -    }); +    })); -    it('should expose iterator offset as $index when iterating over objects', function() { -      var scope = compile('<ul><li ng:repeat="(key, val) in items" ' + -                                  'ng:bind="key + \':\' + val + $index + \'|\'"></li></ul>'); -      scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; -      scope.$digest(); +    it('should expose iterator offset as $index when iterating over objects', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="(key, val) in items" ng:bind="key + \':\' + val + $index + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; +      $rootScope.$digest();        expect(element.text()).toEqual('frodo:f0|misko:m1|shyam:s2|'); -    }); +    })); -    it('should expose iterator position as $position when iterating over arrays', function() { -      var scope = compile('<ul><li ng:repeat="item in items" ' + -                                  'ng:bind="item + \':\' + $position + \'|\'"></li></ul>'); -      scope.items = ['misko', 'shyam', 'doug']; -      scope.$digest(); +    it('should expose iterator position as $position when iterating over arrays', +        inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="item in items" ng:bind="item + \':\' + $position + \'|\'"></li>' + +        '</ul>')($rootScope); +      $rootScope.items = ['misko', 'shyam', 'doug']; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:middle|doug:last|'); -      scope.items.push('frodo'); -      scope.$digest(); +      $rootScope.items.push('frodo'); +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:middle|doug:middle|frodo:last|'); -      scope.items.pop(); -      scope.items.pop(); -      scope.$digest(); +      $rootScope.items.pop(); +      $rootScope.items.pop(); +      $rootScope.$digest();        expect(element.text()).toEqual('misko:first|shyam:last|'); -    }); +    })); -    it('should expose iterator position as $position when iterating over objects', function() { -      var scope = compile( +    it('should expose iterator position as $position when iterating over objects', inject(function($rootScope) { +      var element = angular.compile(          '<ul>' +            '<li ng:repeat="(key, val) in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'">' +            '</li>' + -        '</ul>'); -      scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; -      scope.$digest(); +        '</ul>')($rootScope); +      $rootScope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; +      $rootScope.$digest();        expect(element.text()).toEqual('doug:d:first|frodo:f:middle|misko:m:middle|shyam:s:last|'); -      delete scope.items.doug; -      delete scope.items.frodo; -      scope.$digest(); +      delete $rootScope.items.doug; +      delete $rootScope.items.frodo; +      $rootScope.$digest();        expect(element.text()).toEqual('misko:m:first|shyam:s:last|'); -    }); +    })); -    it('should ignore $ and $$ properties', function() { -      var scope = compile('<ul><li ng:repeat="i in items">{{i}}|</li></ul>'); -      scope.items = ['a', 'b', 'c']; -      scope.items.$$hashkey = 'xxx'; -      scope.items.$root = 'yyy'; -      scope.$digest(); +    it('should ignore $ and $$ properties', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="i in items">{{i}}|</li></ul>')($rootScope); +      $rootScope.items = ['a', 'b', 'c']; +      $rootScope.items.$$hashkey = 'xxx'; +      $rootScope.items.$root = 'yyy'; +      $rootScope.$digest();        expect(element.text()).toEqual('a|b|c|'); -    }); +    })); -    it('should repeat over nested arrays', function() { -      var scope = compile('<ul>' + -                            '<li ng:repeat="subgroup in groups">' + -                              '<div ng:repeat="group in subgroup">{{group}}|</div>X' + -                            '</li>' + -                           '</ul>'); -      scope.groups = [['a', 'b'], ['c','d']]; -      scope.$digest(); +    it('should repeat over nested arrays', inject(function($rootScope) { +      var element = angular.compile( +        '<ul>' + +          '<li ng:repeat="subgroup in groups">' + +            '<div ng:repeat="group in subgroup">{{group}}|</div>X' + +          '</li>' + +        '</ul>')($rootScope); +      $rootScope.groups = [['a', 'b'], ['c','d']]; +      $rootScope.$digest();        expect(element.text()).toEqual('a|b|Xc|d|X'); -    }); +    })); -    it('should ignore non-array element properties when iterating over an array', function() { -      var scope = compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>'); -      scope.array = ['a', 'b', 'c']; -      scope.array.foo = '23'; -      scope.array.bar = function() {}; -      scope.$digest(); +    it('should ignore non-array element properties when iterating over an array', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope); +      $rootScope.array = ['a', 'b', 'c']; +      $rootScope.array.foo = '23'; +      $rootScope.array.bar = function() {}; +      $rootScope.$digest();        expect(element.text()).toBe('a|b|c|'); -    }); +    })); -    it('should iterate over non-existent elements of a sparse array', function() { -      var scope = compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>'); -      scope.array = ['a', 'b']; -      scope.array[4] = 'c'; -      scope.array[6] = 'd'; -      scope.$digest(); +    it('should iterate over non-existent elements of a sparse array', inject(function($rootScope) { +      var element = angular.compile('<ul><li ng:repeat="item in array">{{item}}|</li></ul>')($rootScope); +      $rootScope.array = ['a', 'b']; +      $rootScope.array[4] = 'c'; +      $rootScope.array[6] = 'd'; +      $rootScope.$digest();        expect(element.text()).toBe('a|b|||c||d|'); -    }); +    }));      describe('stability', function() { -      var a, b, c, d, scope, lis; +      var a, b, c, d, lis, element; -      beforeEach(function() { -        scope = compile( +      beforeEach(inject(function($rootScope) { +        element = angular.compile(            '<ul>' + -            '<li ng:repeat="item in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'">' + -            '</li>' + -          '</ul>'); +            '<li ng:repeat="item in items" ng:bind="key + \':\' + val + \':\' + $position + \'|\'"></li>' + +          '</ul>')($rootScope);          a = {};          b = {};          c = {};          d = {}; -        scope.items = [a, b, c]; -        scope.$digest(); +        $rootScope.items = [a, b, c]; +        $rootScope.$digest();          lis = element.find('li'); -      }); +      })); -      it('should preserve the order of elements', function() { -        scope.items = [a, c, d]; -        scope.$digest(); +      it('should preserve the order of elements', inject(function($rootScope) { +        $rootScope.items = [a, c, d]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[2]);          expect(newElements[2]).not.toEqual(lis[1]); -      }); +      })); -      it('should support duplicates', function() { -        scope.items = [a, a, b, c]; -        scope.$digest(); +      it('should support duplicates', inject(function($rootScope) { +        $rootScope.items = [a, a, b, c]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).not.toEqual(lis[0]); @@ -371,170 +367,163 @@ describe("widget", function() {          expect(newElements[3]).toEqual(lis[2]);          lis = newElements; -        scope.$digest(); +        $rootScope.$digest();          newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[2]);          expect(newElements[3]).toEqual(lis[3]); -        scope.$digest(); +        $rootScope.$digest();          newElements = element.find('li');          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[2]);          expect(newElements[3]).toEqual(lis[3]); -      }); +      })); -      it('should remove last item when one duplicate instance is removed', function() { -        scope.items = [a, a, a]; -        scope.$digest(); +      it('should remove last item when one duplicate instance is removed', inject(function($rootScope) { +        $rootScope.items = [a, a, a]; +        $rootScope.$digest();          lis = element.find('li'); -        scope.items = [a, a]; -        scope.$digest(); +        $rootScope.items = [a, a]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements.length).toEqual(2);          expect(newElements[0]).toEqual(lis[0]);          expect(newElements[1]).toEqual(lis[1]); -      }); +      })); -      it('should reverse items when the collection is reversed', function() { -        scope.items = [a, b, c]; -        scope.$digest(); +      it('should reverse items when the collection is reversed', inject(function($rootScope) { +        $rootScope.items = [a, b, c]; +        $rootScope.$digest();          lis = element.find('li'); -        scope.items = [c, b, a]; -        scope.$digest(); +        $rootScope.items = [c, b, a]; +        $rootScope.$digest();          var newElements = element.find('li');          expect(newElements.length).toEqual(3);          expect(newElements[0]).toEqual(lis[2]);          expect(newElements[1]).toEqual(lis[1]);          expect(newElements[2]).toEqual(lis[0]); -      }); +      }));      }); -  }); +  }));    describe('@ng:non-bindable', function() { -    it('should prevent compilation of the owning element and its children', function() { -      var scope = compile('<div ng:non-bindable><span ng:bind="name"></span></div>'); -      scope.name =  'misko'; -      scope.$digest(); +    it('should prevent compilation of the owning element and its children', inject(function($rootScope) { +      var element = angular.compile('<div ng:non-bindable><span ng:bind="name"></span></div>')($rootScope); +      $rootScope.name =  'misko'; +      $rootScope.$digest();        expect(element.text()).toEqual(''); -    }); +    }));    });    describe('ng:view', function() { -    var rootScope, $route, $location, $browser; - -    beforeEach(function() { -      rootScope = angular.compile('<ng:view></ng:view>')(); -      $route = rootScope.$service('$route'); -      $location = rootScope.$service('$location'); -      $browser = rootScope.$service('$browser'); -    }); - -    afterEach(function() { -      dealoc(rootScope); -    }); +    var element; +    beforeEach(inject(function($rootScope) { +      element = angular.compile('<ng:view></ng:view>')($rootScope); +    })); -    it('should do nothing when no routes are defined', function() { +    it('should do nothing when no routes are defined', inject(function($rootScope, $location) {        $location.path('/unknown'); -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual(''); -    }); +      $rootScope.$digest(); +      expect(element.text()).toEqual(''); +    })); -    it('should load content via xhr when route changes', function() { +    it('should load content via xhr when route changes', inject(function($rootScope, $browser, $location, $route) {        $route.when('/foo', {template: 'myUrl1'});        $route.when('/bar', {template: 'myUrl2'}); -      expect(rootScope.$element.text()).toEqual(''); +      expect(element.text()).toEqual('');        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('4'); +      expect(element.text()).toEqual('4');        $location.path('/bar');        $browser.xhr.expectGET('myUrl2').respond('angular is da best'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('angular is da best'); -    }); +      expect(element.text()).toEqual('angular is da best'); +    })); -    it('should remove all content when location changes to an unknown route', function() { +    it('should remove all content when location changes to an unknown route', +        inject(function($rootScope, $location, $browser, $route) {        $route.when('/foo', {template: 'myUrl1'});        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('4'); +      expect(element.text()).toEqual('4');        $location.path('/unknown'); -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual(''); -    }); +      $rootScope.$digest(); +      expect($rootScope.$element.text()).toEqual(''); +    })); -    it('should chain scopes and propagate evals to the child scope', function() { +    it('should chain scopes and propagate evals to the child scope', +        inject(function($rootScope, $location, $browser, $route) {        $route.when('/foo', {template: 'myUrl1'}); -      rootScope.parentVar = 'parent'; +      $rootScope.parentVar = 'parent';        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{parentVar}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('parent'); - -      rootScope.parentVar = 'new parent'; -      rootScope.$digest(); -      expect(rootScope.$element.text()).toEqual('new parent'); -    }); +      expect(element.text()).toEqual('parent'); -    it('should be possible to nest ng:view in ng:include', function() { -      dealoc(rootScope); // we are about to override it. +      $rootScope.parentVar = 'new parent'; +      $rootScope.$digest(); +      expect($rootScope.$element.text()).toEqual('new parent'); +    })); -      var myApp = angular.scope(); +    it('should be possible to nest ng:view in ng:include', inject(function() { +      var injector = createInjector(); +      var myApp = injector('$rootScope');        var $browser = myApp.$service('$browser');        $browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>'); -      myApp.$service('$location').path('/foo'); +      injector('$location').path('/foo'); -      var $route = myApp.$service('$route'); +      var $route = injector('$route');        $route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'}); -      rootScope = angular.compile( +      var element = angular.compile(            '<div>' + -            'include: <ng:include src="\'includePartial.html\'">' + -          '</ng:include></div>')(myApp); -      rootScope.$apply(); +            'include: <ng:include src="\'includePartial.html\'"> </ng:include>' + +          '</div>')(myApp); +      myApp.$apply();        $browser.xhr.expectGET('viewPartial.html').respond('content'); -      rootScope.$digest(); +      myApp.$digest();        $browser.xhr.flush(); -      expect(rootScope.$element.text()).toEqual('include: view: content'); +      expect(myApp.$element.text()).toEqual('include: view: content');        expect($route.current.template).toEqual('viewPartial.html'); -      dealoc($route.current.scope); -    }); +      dealoc(myApp); +    }));      it('should initialize view template after the view controller was initialized even when ' + -       'templates were cached', function() { +       'templates were cached', inject(function($rootScope, $location, $browser, $route) {        // this is a test for a regression that was introduced by making the ng:view cache sync        $route.when('/foo', {controller: ParentCtrl, template: 'viewPartial.html'}); -      rootScope.log = []; +      $rootScope.log = [];        function ParentCtrl() {          this.log.push('parent');        } -      rootScope.ChildCtrl = function() { +      $rootScope.ChildCtrl = function() {          this.log.push('child');        }; @@ -543,42 +532,42 @@ describe("widget", function() {            respond('<div ng:init="log.push(\'init\')">' +                      '<div ng:controller="ChildCtrl"></div>' +                    '</div>'); -      rootScope.$apply(); +      $rootScope.$apply();        $browser.xhr.flush(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']);        $location.path('/'); -      rootScope.$apply(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); +      $rootScope.$apply(); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']); -      rootScope.log = []; +      $rootScope.log = [];        $location.path('/foo'); -      rootScope.$apply(); +      $rootScope.$apply();        $browser.defer.flush(); -      expect(rootScope.log).toEqual(['parent', 'init', 'child']); -    }); +      expect($rootScope.log).toEqual(['parent', 'init', 'child']); +    }));      it('should discard pending xhr callbacks if a new route is requested before the current ' + -        'finished loading', function() { +        'finished loading', inject(function($route, $rootScope, $location, $browser) {        // this is a test for a bad race condition that affected feedback        $route.when('/foo', {template: 'myUrl1'});        $route.when('/bar', {template: 'myUrl2'}); -      expect(rootScope.$element.text()).toEqual(''); +      expect($rootScope.$element.text()).toEqual('');        $location.path('/foo');        $browser.xhr.expectGET('myUrl1').respond('<div>{{1+3}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $location.path('/bar');        $browser.xhr.expectGET('myUrl2').respond('<div>{{1+1}}</div>'); -      rootScope.$digest(); +      $rootScope.$digest();        $browser.xhr.flush(); // now that we have to requests pending, flush! -      expect(rootScope.$element.text()).toEqual('2'); -    }); +      expect($rootScope.$element.text()).toEqual('2'); +    }));    }); @@ -586,125 +575,128 @@ describe("widget", function() {      describe('deal with pluralized strings without offset', function() { -       beforeEach(function() { -          compile('<ng:pluralize count="email"' + -                                 "when=\"{'0': 'You have no new email'," + -                                         "'one': 'You have one new email'," + -                                         "'other': 'You have {} new emails'}\">" + -                  '</ng:pluralize>'); -        }); - -        it('should show single/plural strings', function() { -          scope.email = 0; -          scope.$digest(); +       var element; +       beforeEach(inject(function($rootScope) { +          element = angular.compile( +            '<ng:pluralize count="email"' + +                           "when=\"{'0': 'You have no new email'," + +                                   "'one': 'You have one new email'," + +                                   "'other': 'You have {} new emails'}\">" + +            '</ng:pluralize>')($rootScope); +        })); + +        it('should show single/plural strings', inject(function($rootScope) { +          $rootScope.email = 0; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = '0'; -          scope.$digest(); +          $rootScope.email = '0'; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = 1; -          scope.$digest(); +          $rootScope.email = 1; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -          scope.email = 0.01; -          scope.$digest(); +          $rootScope.email = 0.01; +          $rootScope.$digest();            expect(element.text()).toBe('You have 0.01 new emails'); -          scope.email = '0.1'; -          scope.$digest(); +          $rootScope.email = '0.1'; +          $rootScope.$digest();            expect(element.text()).toBe('You have 0.1 new emails'); -          scope.email = 2; -          scope.$digest(); +          $rootScope.email = 2; +          $rootScope.$digest();            expect(element.text()).toBe('You have 2 new emails'); -          scope.email = -0.1; -          scope.$digest(); +          $rootScope.email = -0.1; +          $rootScope.$digest();            expect(element.text()).toBe('You have -0.1 new emails'); -          scope.email = '-0.01'; -          scope.$digest(); +          $rootScope.email = '-0.01'; +          $rootScope.$digest();            expect(element.text()).toBe('You have -0.01 new emails'); -          scope.email = -2; -          scope.$digest(); +          $rootScope.email = -2; +          $rootScope.$digest();            expect(element.text()).toBe('You have -2 new emails'); -        }); +        })); -        it('should show single/plural strings with mal-formed inputs', function() { -          scope.email = ''; -          scope.$digest(); +        it('should show single/plural strings with mal-formed inputs', inject(function($rootScope) { +          $rootScope.email = ''; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = null; -          scope.$digest(); +          $rootScope.email = null; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = undefined; -          scope.$digest(); +          $rootScope.email = undefined; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = 'a3'; -          scope.$digest(); +          $rootScope.email = 'a3'; +          $rootScope.$digest();            expect(element.text()).toBe(''); -          scope.email = '011'; -          scope.$digest(); +          $rootScope.email = '011'; +          $rootScope.$digest();            expect(element.text()).toBe('You have 11 new emails'); -          scope.email = '-011'; -          scope.$digest(); +          $rootScope.email = '-011'; +          $rootScope.$digest();            expect(element.text()).toBe('You have -11 new emails'); -          scope.email = '1fff'; -          scope.$digest(); +          $rootScope.email = '1fff'; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -          scope.email = '0aa22'; -          scope.$digest(); +          $rootScope.email = '0aa22'; +          $rootScope.$digest();            expect(element.text()).toBe('You have no new email'); -          scope.email = '000001'; -          scope.$digest(); +          $rootScope.email = '000001'; +          $rootScope.$digest();            expect(element.text()).toBe('You have one new email'); -        }); +        }));      });      describe('deal with pluralized strings with offset', function() { -      it('should show single/plural strings with offset', function() { -        compile("<ng:pluralize count=\"viewCount\"  offset=2 " + -                    "when=\"{'0': 'Nobody is viewing.'," + -                            "'1': '{{p1}} is viewing.'," + -                            "'2': '{{p1}} and {{p2}} are viewing.'," + -                            "'one': '{{p1}}, {{p2}} and one other person are viewing.'," + -                            "'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" + -                "</ng:pluralize>"); -        scope.p1 = 'Igor'; -        scope.p2 = 'Misko'; - -        scope.viewCount = 0; -        scope.$digest(); +      it('should show single/plural strings with offset', inject(function($rootScope) { +        var element = angular.compile( +          "<ng:pluralize count=\"viewCount\"  offset=2 " + +              "when=\"{'0': 'Nobody is viewing.'," + +                      "'1': '{{p1}} is viewing.'," + +                      "'2': '{{p1}} and {{p2}} are viewing.'," + +                      "'one': '{{p1}}, {{p2}} and one other person are viewing.'," + +                      "'other': '{{p1}}, {{p2}} and {} other people are viewing.'}\">" + +          "</ng:pluralize>")($rootScope); +        $rootScope.p1 = 'Igor'; +        $rootScope.p2 = 'Misko'; + +        $rootScope.viewCount = 0; +        $rootScope.$digest();          expect(element.text()).toBe('Nobody is viewing.'); -        scope.viewCount = 1; -        scope.$digest(); +        $rootScope.viewCount = 1; +        $rootScope.$digest();          expect(element.text()).toBe('Igor is viewing.'); -        scope.viewCount = 2; -        scope.$digest(); +        $rootScope.viewCount = 2; +        $rootScope.$digest();          expect(element.text()).toBe('Igor and Misko are viewing.'); -        scope.viewCount = 3; -        scope.$digest(); +        $rootScope.viewCount = 3; +        $rootScope.$digest();          expect(element.text()).toBe('Igor, Misko and one other person are viewing.'); -        scope.viewCount = 4; -        scope.$digest(); +        $rootScope.viewCount = 4; +        $rootScope.$digest();          expect(element.text()).toBe('Igor, Misko and 2 other people are viewing.'); -      }); +      }));      });    });  });  | 
