diff options
| author | Misko Hevery | 2012-05-22 16:45:56 -0700 | 
|---|---|---|
| committer | Misko Hevery | 2012-06-02 15:44:58 -0700 | 
| commit | 92a2e1807657c69e1372106b0727675a30f4cbd7 (patch) | |
| tree | f236f146b44c4a03cfc04e48e6dba878c74c6153 /test | |
| parent | 8aa18f0ad036fd4f2dc26f54d80754c70232b4f7 (diff) | |
| download | angular.js-92a2e1807657c69e1372106b0727675a30f4cbd7.tar.bz2 | |
feat($location): add $locatonChange[begin|completed] event
This allows location change cancelation
Diffstat (limited to 'test')
| -rw-r--r-- | test/ng/locationSpec.js | 157 | ||||
| -rw-r--r-- | test/ng/routeSpec.js | 29 | 
2 files changed, 171 insertions, 15 deletions
diff --git a/test/ng/locationSpec.js b/test/ng/locationSpec.js index 46079ea6..8b876a3c 100644 --- a/test/ng/locationSpec.js +++ b/test/ng/locationSpec.js @@ -791,19 +791,6 @@ describe('$location', function() {      }); -    it('should not rewrite when history disabled', function() { -      configureService('#new', false); -      inject( -        initBrowser(), -        initLocation(), -        function($browser) { -          browserTrigger(link, 'click'); -          expectNoRewrite($browser); -        } -      ); -    }); - -      it('should not rewrite full url links do different domain', function() {        configureService('http://www.dot.abc/a?b=c', true);        inject( @@ -982,4 +969,148 @@ describe('$location', function() {        });      }    }); + + +  describe('location cancellation', function() { +    it('should fire $before/afterLocationChange event', inject(function($location, $browser, $rootScope, $log) { +      expect($browser.url()).toEqual('http://server/'); + +      $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { +        $log.info('before', newUrl, oldUrl, $browser.url()); +      }); +      $rootScope.$on('$locationChangeSuccess', function(event, newUrl, oldUrl) { +        $log.info('after', newUrl, oldUrl, $browser.url()); +      }); + +      expect($location.url()).toEqual(''); +      $location.url('/somePath'); +      expect($location.url()).toEqual('/somePath'); +      expect($browser.url()).toEqual('http://server/'); +      expect($log.info.logs).toEqual([]); + +      $rootScope.$apply(); + +      expect($log.info.logs.shift()). +          toEqual(['before', 'http://server/#/somePath', 'http://server/', 'http://server/']); +      expect($log.info.logs.shift()). +          toEqual(['after', 'http://server/#/somePath', 'http://server/', 'http://server/#/somePath']); +      expect($location.url()).toEqual('/somePath'); +      expect($browser.url()).toEqual('http://server/#/somePath'); +    })); + + +    it('should allow $locationChangeStart event cancellation', inject(function($location, $browser, $rootScope, $log) { +      expect($browser.url()).toEqual('http://server/'); +      expect($location.url()).toEqual(''); + +      $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { +        $log.info('before', newUrl, oldUrl, $browser.url()); +        event.preventDefault(); +      }); +      $rootScope.$on('$locationChangeCompleted', function(event, newUrl, oldUrl) { +        throw Error('location should have been canceled'); +      }); + +      expect($location.url()).toEqual(''); +      $location.url('/somePath'); +      expect($location.url()).toEqual('/somePath'); +      expect($browser.url()).toEqual('http://server/'); +      expect($log.info.logs).toEqual([]); + +      $rootScope.$apply(); + +      expect($log.info.logs.shift()). +          toEqual(['before', 'http://server/#/somePath', 'http://server/', 'http://server/']); +      expect($log.info.logs[1]).toBeUndefined(); +      expect($location.url()).toEqual(''); +      expect($browser.url()).toEqual('http://server/'); +    })); + +    it ('should fire $locationChangeCompleted event when change from browser location bar', +      inject(function($log, $location, $browser, $rootScope) { +        $rootScope.$apply(); // clear initial $locationChangeStart + +        expect($browser.url()).toEqual('http://server/'); +        expect($location.url()).toEqual(''); + +        $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { +          throw Error('there is no before when user enters URL directly to browser'); +        }); +        $rootScope.$on('$locationChangeSuccess', function(event, newUrl, oldUrl) { +          $log.info('after', newUrl, oldUrl); +        }); + + +        $browser.url('http://server/#/somePath'); +        $browser.poll(); + +        expect($log.info.logs.shift()). +          toEqual(['after', 'http://server/#/somePath', 'http://server/']); +      }) +    ); + + +    it('should listen on click events on href and prevent browser default in hasbang mode', function() { +      module(function() { +        return function($rootElement, $compile, $rootScope) { +          $rootElement.html('<a href="http://server/#/somePath">link</a>'); +          $compile($rootElement)($rootScope); +          jqLite(document.body).append($rootElement); +        } +      }); + +      inject(function($location, $rootScope, $browser, $rootElement) { +        var log = '', +            link = $rootElement.find('a'); + + +        $rootScope.$on('$locationChangeStart', function(event) { +          event.preventDefault(); +          log += '$locationChangeStart'; +        }); +        $rootScope.$on('$locationChangeCompleted', function() { +          throw new Error('after cancellation in hashbang mode'); +        }); + +        browserTrigger(link, 'click'); + +        expect(log).toEqual('$locationChangeStart'); +        expect($browser.url()).toEqual('http://server/'); + +        dealoc($rootElement); +      }); +    }); + + +    it('should listen on click events on href and prevent browser default in html5 mode', function() { +      module(function($locationProvider) { +        $locationProvider.html5Mode(true); +        return function($rootElement, $compile, $rootScope) { +          $rootElement.html('<a href="http://server/somePath">link</a>'); +          $compile($rootElement)($rootScope); +          jqLite(document.body).append($rootElement); +        } +      }); + +      inject(function($location, $rootScope, $browser, $rootElement) { +        var log = '', +          link = $rootElement.find('a'); + +        $rootScope.$on('$locationChangeStart', function(event) { +          event.preventDefault(); +          log += '$locationChangeStart'; +        }); +        $rootScope.$on('$locationChangeCompleted', function() { +          throw new Error('after cancalation in html5 mode'); +        }); + +        browserTrigger(link, 'click'); + +        expect(log).toEqual('$locationChangeStart'); +        expect($browser.url()).toEqual('http://server/'); + +        dealoc($rootElement); +      }); +    }); +  });  }); diff --git a/test/ng/routeSpec.js b/test/ng/routeSpec.js index 8c5f93f9..31d932f7 100644 --- a/test/ng/routeSpec.js +++ b/test/ng/routeSpec.js @@ -60,6 +60,28 @@ describe('$route', function() {    }); +  it('should not change route when location is canceled', function() { +    module(function($routeProvider) { +      $routeProvider.when('/somePath', {template: 'some path'}); +    }); +    inject(function($route, $location, $rootScope, $log) { +      $rootScope.$on('$locationChangeStart', function(event) { +        $log.info('$locationChangeStart'); +        event.preventDefault(); +      }); + +      $rootScope.$on('$beforeRouteChange', function(event) { +        throw new Error('Should not get here'); +      }); + +      $location.path('/somePath'); +      $rootScope.$digest(); + +      expect($log.info.logs.shift()).toEqual(['$locationChangeStart']); +    }); +  }); + +    it('should match a route that contains special chars in the path', function() {      module(function($routeProvider) {        $routeProvider.when('/$test.23/foo(bar)/:baz', {templateUrl: 'test.html'}); @@ -540,8 +562,11 @@ describe('$route', function() {        });        inject(function($route, $location, $rootScope) {          var replace; -        $rootScope.$watch(function() { -          if (isUndefined(replace)) replace = $location.$$replace; + +        $rootScope.$on('$locationChangeStart', function(event, newUrl, oldUrl) { +          if (oldUrl == 'http://server/#/foo/id3/eId') { +            replace = $location.$$replace; +          }          });          $location.path('/foo/id3/eId');  | 
