diff options
| author | Igor Minar | 2011-08-15 08:34:11 -0700 | 
|---|---|---|
| committer | Igor Minar | 2011-08-19 12:05:52 -0700 | 
| commit | e004378d100ce767a1107180102790a9a360644e (patch) | |
| tree | 74302cc42b863864118526a7795a2252650438a9 /test/service/routeSpec.js | |
| parent | 4ec1d8ee86e3138fb91543ca0dca28463895c090 (diff) | |
| download | angular.js-e004378d100ce767a1107180102790a9a360644e.tar.bz2 | |
feat($route): add reloadOnSearch route param to avoid reloads
In order to avoid unnecesary route reloads when just hashSearch part
of the url changes, it is now possible to disable this behavior by
setting reloadOnSearch param of the route declaration to false.
Closes #354
Diffstat (limited to 'test/service/routeSpec.js')
| -rw-r--r-- | test/service/routeSpec.js | 136 | 
1 files changed, 136 insertions, 0 deletions
| diff --git a/test/service/routeSpec.js b/test/service/routeSpec.js index fc2c7f9d..4d24279c 100644 --- a/test/service/routeSpec.js +++ b/test/service/routeSpec.js @@ -227,4 +227,140 @@ describe('$route', function() {        }      });    }); + + +  describe('reloadOnSearch', function() { +    it('should reload a route when reloadOnSearch is enabled and hashSearch changes', function() { +      var scope = angular.scope(), +          $location = scope.$service('$location'), +          $route = scope.$service('$route'), +          reloaded = jasmine.createSpy('route reload'); + +      $route.when('/foo', {controller: FooCtrl}); +      $route.onChange(reloaded); + +      function FooCtrl() { +        reloaded(); +      } + +      $location.updateHash('/foo'); +      scope.$eval(); +      expect(reloaded).toHaveBeenCalled(); +      reloaded.reset(); + +      // trigger reload +      $location.hashSearch.foo = 'bar'; +      scope.$eval(); +      expect(reloaded).toHaveBeenCalled(); +    }); + + +    it('should not reload a route when reloadOnSearch is disabled and only hashSearch changes', +        function() { +      var scope = angular.scope(), +          $location = scope.$service('$location'), +          $route = scope.$service('$route'), +          reloaded = jasmine.createSpy('route reload'); + +      $route.when('/foo', {controller: FooCtrl, reloadOnSearch: false}); +      $route.onChange(reloaded); + +      function FooCtrl() { +        reloaded(); +      } + +      expect(reloaded).not.toHaveBeenCalled(); + +      $location.updateHash('/foo'); +      scope.$eval(); +      expect(reloaded).toHaveBeenCalled(); +      reloaded.reset(); + +      // don't trigger reload +      $location.hashSearch.foo = 'bar'; +      scope.$eval(); +      expect(reloaded).not.toHaveBeenCalled(); +    }); + + +    it('should reload reloadOnSearch route when url differs only in route path param', function() { +      var scope = angular.scope(), +          $location = scope.$service('$location'), +          $route = scope.$service('$route'), +          reloaded = jasmine.createSpy('routeReload'), +          onRouteChange = jasmine.createSpy('onRouteChange'); + +      $route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false}); +      $route.onChange(onRouteChange); + +      function FooCtrl() { +        reloaded(); +      } + +      expect(reloaded).not.toHaveBeenCalled(); +      expect(onRouteChange).not.toHaveBeenCalled(); + +      $location.updateHash('/foo/aaa'); +      scope.$eval(); +      expect(reloaded).toHaveBeenCalled(); +      expect(onRouteChange).toHaveBeenCalled(); +      reloaded.reset(); +      onRouteChange.reset(); + +      $location.updateHash('/foo/bbb'); +      scope.$eval(); +      expect(reloaded).toHaveBeenCalled(); +      expect(onRouteChange).toHaveBeenCalled(); +      reloaded.reset(); +      onRouteChange.reset(); + +      $location.hashSearch.foo = 'bar'; +      scope.$eval(); +      expect(reloaded).not.toHaveBeenCalled(); +      expect(onRouteChange).not.toHaveBeenCalled(); +    }); + + +    it('should update route params when reloadOnSearch is disabled and hashSearch', function() { +      var scope = angular.scope(), +          $location = scope.$service('$location'), +          $route = scope.$service('$route'), +          routeParams = jasmine.createSpy('routeParams'); + +      $route.when('/foo', {controller: FooCtrl}); +      $route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false}); + +      function FooCtrl() { +        this.$watch(function() { +          return $route.current.params; +        }, function(params) { +          routeParams(params); +        }); +      } + +      expect(routeParams).not.toHaveBeenCalled(); + +      $location.updateHash('/foo'); +      scope.$eval(); +      expect(routeParams).toHaveBeenCalledWith({}); +      routeParams.reset(); + +      // trigger reload +      $location.hashSearch.foo = 'bar'; +      scope.$eval(); +      expect(routeParams).toHaveBeenCalledWith({foo: 'bar'}); +      routeParams.reset(); + +      $location.updateHash('/bar/123'); +      scope.$eval(); +      expect(routeParams).toHaveBeenCalledWith({barId: '123'}); +      routeParams.reset(); + +      // don't trigger reload +      $location.hashSearch.foo = 'bar'; +      scope.$eval(); +      $route.current.scope.$eval(); // ng:view propagates evals so we have to do it by hand here +      expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'}); +    }); +  });  }); | 
