aboutsummaryrefslogtreecommitdiffstats
path: root/test/ng/locationSpec.js
diff options
context:
space:
mode:
authorMisko Hevery2012-05-22 16:45:56 -0700
committerMisko Hevery2012-06-02 15:44:58 -0700
commit92a2e1807657c69e1372106b0727675a30f4cbd7 (patch)
treef236f146b44c4a03cfc04e48e6dba878c74c6153 /test/ng/locationSpec.js
parent8aa18f0ad036fd4f2dc26f54d80754c70232b4f7 (diff)
downloadangular.js-92a2e1807657c69e1372106b0727675a30f4cbd7.tar.bz2
feat($location): add $locatonChange[begin|completed] event
This allows location change cancelation
Diffstat (limited to 'test/ng/locationSpec.js')
-rw-r--r--test/ng/locationSpec.js157
1 files changed, 144 insertions, 13 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);
+ });
+ });
+ });
});