fix($route): update $route to reflect $location changes

* update $route to reflect new $location
* add some more unit tests to $route
* fix some other failing unit tests
* redirect overrides the url now

Breaks $route custom redirect fn has only 3 params now
This commit is contained in:
Vojta Jina
2011-07-12 02:18:17 +02:00
parent 5ba227c7cd
commit 22cb600280
6 changed files with 135 additions and 191 deletions

View File

@@ -32,17 +32,4 @@ describe("ScenarioSpec: Compilation", function(){
expect(jqLite(scope.$element).text()).toEqual('123');
});
});
describe("configuration", function(){
it("should take location object", function(){
var url = "http://server/#?book=moby";
scope = angular.compile("<div>{{$location}}</div>")();
var $location = scope.$service('$location');
var $browser = scope.$service('$browser');
expect($location.hashSearch.book).toBeUndefined();
$browser.url(url);
$browser.poll();
expect($location.hashSearch.book).toEqual('moby');
});
});
});

View File

@@ -10,11 +10,11 @@ describe('$routeParams', function(){
$route.when('/foo');
$route.when('/bar/:barId');
$location.hash = '/foo?a=b';
$location.path('/foo').search('a=b');
scope.$digest();
expect($routeParams).toEqual({a:'b'});
$location.hash = '/bar/123?x=abc';
$location.path('/bar/123').search('x=abc');
scope.$digest();
expect($routeParams).toEqual({barId:'123', x:'abc'});
});
@@ -30,11 +30,11 @@ describe('$routeParams', function(){
$route.when('/foo');
$route.when('/bar/:barId');
$location.hash = '/foo?a=b';
$location.path('/foo').search('a=b');
scope.$digest();
expect(scope.$service('$routeParams')).toBe(firstRouteParams);
$location.hash = '/bar/123?x=abc';
$location.path('/bar/123').search('x=abc');
scope.$digest();
expect(scope.$service('$routeParams')).toBe(firstRouteParams);
});

View File

@@ -1,30 +1,24 @@
'use strict';
describe('$route', function() {
var scope;
var scope, $route, $location;
beforeEach(function(){
scope = angular.scope();
});
afterEach(function(){
dealoc(scope);
$location = scope.$service('$location');
$route = scope.$service('$route');
});
it('should route and fire change event', function(){
var log = '',
$location, $route,
lastRoute,
nextRoute;
function BookChapter() {
log += '<init>;';
}
scope = compile('<div></div>')();
$location = scope.$service('$location');
$route = scope.$service('$route');
$route.when('/Book/:book/Chapter/:chapter', {controller: BookChapter, template:'Chapter.html'});
$route.when('/Blank');
scope.$on('$beforeRouteChange', function(event, next, current){
@@ -40,21 +34,21 @@ describe('$route', function() {
expect(nextRoute).toBe(current);
});
$location.update('http://server#/Book/Moby/Chapter/Intro?p=123');
$location.path('/Book/Moby/Chapter/Intro').search('p=123');
scope.$digest();
expect(log).toEqual('before();<init>;after();');
expect($route.current.params).toEqual({book:'Moby', chapter:'Intro', p:'123'});
var lastId = $route.current.scope.$id;
log = '';
$location.update('http://server#/Blank?ignore');
$location.path('/Blank').search('ignore');
scope.$digest();
expect(log).toEqual('before();after();');
expect($route.current.params).toEqual({ignore:true});
expect($route.current.scope.$id).not.toEqual(lastId);
log = '';
$location.update('http://server#/NONE');
$location.path('/NONE');
scope.$digest();
expect(log).toEqual('before();after();');
expect($route.current).toEqual(null);
@@ -64,19 +58,31 @@ describe('$route', function() {
expect($route.current.template).toEqual('instant update');
});
it('should change route even when only search param changes', function() {
var callback = jasmine.createSpy('onRouteChange');
$route.when('/test', {template: 'test.html'});
scope.$on('$beforeRouteChange', callback);
$location.path('/test');
scope.$digest();
callback.reset();
$location.search({any: true});
scope.$digest();
expect(callback).toHaveBeenCalled();
});
it('should allow routes to be defined with just templates without controllers', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route'),
onChangeSpy = jasmine.createSpy('onChange');
var onChangeSpy = jasmine.createSpy('onChange');
$route.when('/foo', {template: 'foo.html'});
scope.$on('$beforeRouteChange', onChangeSpy);
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect($route.current.template).toEqual('foo.html');
@@ -86,10 +92,7 @@ describe('$route', function() {
it('should handle unknown routes with "otherwise" route definition', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route'),
onChangeSpy = jasmine.createSpy('onChange');
var onChangeSpy = jasmine.createSpy('onChange');
function NotFoundCtrl() {this.notFoundProp = 'not found!';}
@@ -99,7 +102,7 @@ describe('$route', function() {
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
$location.updateHash('/unknownRoute');
$location.path('/unknownRoute');
scope.$digest();
expect($route.current.template).toBe('404.html');
@@ -108,7 +111,7 @@ describe('$route', function() {
expect(onChangeSpy).toHaveBeenCalled();
onChangeSpy.reset();
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect($route.current.template).toEqual('foo.html');
@@ -118,33 +121,28 @@ describe('$route', function() {
});
it('should $destroy old routes', function(){
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route');
$route.when('/foo', {template: 'foo.html', controller: function(){ this.name = 'FOO';}});
$route.when('/bar', {template: 'bar.html', controller: function(){ this.name = 'BAR';}});
$route.when('/baz', {template: 'baz.html'});
expect(scope.$childHead).toEqual(null);
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect(scope.$$childHead).toBeTruthy();
expect(scope.$$childHead).toEqual(scope.$$childTail);
$location.updateHash('/bar');
scope.$digest();
expect(scope.$$childHead).toBeTruthy();
expect(scope.$$childHead).toEqual(scope.$$childTail);
return
$location.updateHash('/baz');
$location.path('/bar');
scope.$digest();
expect(scope.$$childHead).toBeTruthy();
expect(scope.$$childHead).toEqual(scope.$$childTail);
$location.updateHash('/');
$location.path('/baz');
scope.$digest();
expect(scope.$$childHead).toBeTruthy();
expect(scope.$$childHead).toEqual(scope.$$childTail);
$location.path('/');
scope.$digest();
expect(scope.$$childHead).toEqual(null);
expect(scope.$$childTail).toEqual(null);
@@ -152,15 +150,10 @@ describe('$route', function() {
describe('redirection', function() {
it('should support redirection via redirectTo property by updating $location', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$browser = scope.$service('$browser'),
$route = scope.$service('$route'),
onChangeSpy = jasmine.createSpy('onChange');
var onChangeSpy = jasmine.createSpy('onChange');
$route.when('', {redirectTo: '/foo'});
$route.when('/', {redirectTo: '/foo'});
$route.when('/foo', {template: 'foo.html'});
$route.when('/bar', {template: 'bar.html'});
$route.when('/baz', {redirectTo: '/bar'});
@@ -169,108 +162,85 @@ describe('$route', function() {
expect($route.current).toBeUndefined();
expect(onChangeSpy).not.toHaveBeenCalled();
scope.$digest(); //triggers initial route change - match the redirect route
$browser.defer.flush(); //triger route change - match the route we redirected to
expect($location.hash).toBe('/foo');
expect($route.current.template).toBe('foo.html');
expect(onChangeSpy.callCount).toBe(2);
onChangeSpy.reset();
$location.updateHash('');
scope.$digest(); //match the redirect route + update $browser
$browser.defer.flush(); //match the route we redirected to
expect($location.hash).toBe('/foo');
$location.path('/');
scope.$digest();
expect($location.path()).toBe('/foo');
expect($route.current.template).toBe('foo.html');
expect(onChangeSpy.callCount).toBe(2);
onChangeSpy.reset();
$location.updateHash('/baz');
scope.$digest(); //match the redirect route + update $browser
$browser.defer.flush(); //match the route we redirected to
expect($location.hash).toBe('/bar');
$location.path('/baz');
scope.$digest();
expect($location.path()).toBe('/bar');
expect($route.current.template).toBe('bar.html');
expect(onChangeSpy.callCount).toBe(2);
});
it('should interpolate route variables in the redirected hashPath from the original hashPath',
function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$browser = scope.$service('$browser'),
$route = scope.$service('$route');
it('should interpolate route vars in the redirected path from original path', function() {
$route.when('/foo/:id/foo/:subid/:extraId', {redirectTo: '/bar/:id/:subid/23'});
$route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
$location.path('/foo/id1/foo/subid3/gah');
scope.$digest();
$location.updateHash('/foo/id1/foo/subid3/gah');
scope.$digest(); //triggers initial route change - match the redirect route
$browser.defer.flush(); //triger route change - match the route we redirected to
expect($location.hash).toBe('/bar/id1/subid3/23?extraId=gah');
expect($route.current.template).toBe('bar.html');
expect($location.path()).toEqual('/bar/id1/subid3/23');
expect($location.search()).toEqual({extraId: 'gah'});
expect($route.current.template).toEqual('bar.html');
});
it('should interpolate route variables in the redirected hashPath from the original hashSearch',
function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$browser = scope.$service('$browser'),
$route = scope.$service('$route');
it('should interpolate route vars in the redirected path from original search', function() {
$route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
$route.when('/foo/:id/:extra', {redirectTo: '/bar/:id/:subid/99'});
$location.path('/foo/id3/eId').search('subid=sid1&appended=true');
scope.$digest();
$location.hash = '/foo/id3/eId?subid=sid1&appended=true';
scope.$digest(); //triggers initial route change - match the redirect route
$browser.defer.flush(); //triger route change - match the route we redirected to
expect($location.hash).toBe('/bar/id3/sid1/99?appended=true&extra=eId');
expect($route.current.template).toBe('bar.html');
expect($location.path()).toEqual('/bar/id3/sid1/99');
expect($location.search()).toEqual({appended: 'true', extra: 'eId'});
expect($route.current.template).toEqual('bar.html');
});
it('should allow custom redirectTo function to be used', function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$browser = scope.$service('$browser'),
$route = scope.$service('$route');
$route.when('/bar/:id/:subid/:subsubid', {template: 'bar.html'});
$route.when('/foo/:id',
{redirectTo: customRedirectFn});
$route.when('/foo/:id', {redirectTo: customRedirectFn});
$location.path('/foo/id3').search('subid=sid1&appended=true');
scope.$digest();
$location.hash = '/foo/id3?subid=sid1&appended=true';
scope.$digest(); //triggers initial route change - match the redirect route
$browser.defer.flush(); //triger route change - match the route we redirected to
expect($location.path()).toEqual('/custom');
expect($location.hash).toBe('custom');
function customRedirectFn(routePathParams, hash, hashPath, hashSearch) {
function customRedirectFn(routePathParams, path, search) {
expect(routePathParams).toEqual({id: 'id3'});
expect(hash).toEqual($location.hash);
expect(hashPath).toEqual($location.hashPath);
expect(hashSearch).toEqual($location.hashSearch);
return 'custom';
expect(path).toEqual($location.path());
expect(search).toEqual($location.search());
return '/custom';
}
});
it('should replace the url when redirecting', function() {
$route.when('/bar/:id', {template: 'bar.html'});
$route.when('/foo/:id/:extra', {redirectTo: '/bar/:id'});
var replace;
scope.$watch(function() {
if (isUndefined(replace)) replace = $location.$$replace;
});
$location.path('/foo/id3/eId');
scope.$digest();
expect($location.path()).toEqual('/bar/id3');
expect(replace).toBe(true);
});
});
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'),
$rouetParams = scope.$service('$routeParams'),
it('should reload a route when reloadOnSearch is enabled and .search() changes', function() {
var $rouetParams = scope.$service('$routeParams'),
reloaded = jasmine.createSpy('route reload');
$route.when('/foo', {controller: FooCtrl});
@@ -280,26 +250,23 @@ describe('$route', function() {
reloaded();
}
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect(reloaded).toHaveBeenCalled();
expect($rouetParams).toEqual({});
reloaded.reset();
// trigger reload
$location.hashSearch.foo = 'bar';
$location.search({foo: 'bar'});
scope.$digest();
expect(reloaded).toHaveBeenCalled();
expect($rouetParams).toEqual({foo:'bar'});
});
it('should not reload a route when reloadOnSearch is disabled and only hashSearch changes',
it('should not reload a route when reloadOnSearch is disabled and only .search() changes',
function() {
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route'),
reloaded = jasmine.createSpy('route reload'),
var reloaded = jasmine.createSpy('route reload'),
routeUpdateEvent = jasmine.createSpy('route reload');
$route.when('/foo', {controller: FooCtrl, reloadOnSearch: false});
@@ -312,14 +279,14 @@ describe('$route', function() {
expect(reloaded).not.toHaveBeenCalled();
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(routeUpdateEvent).not.toHaveBeenCalled();
reloaded.reset();
// don't trigger reload
$location.hashSearch.foo = 'bar';
$location.search({foo: 'bar'});
scope.$digest();
expect(reloaded).not.toHaveBeenCalled();
expect(routeUpdateEvent).toHaveBeenCalled();
@@ -327,10 +294,7 @@ describe('$route', function() {
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'),
var reloaded = jasmine.createSpy('routeReload'),
onRouteChange = jasmine.createSpy('onRouteChange');
$route.when('/foo/:fooId', {controller: FooCtrl, reloadOnSearch: false});
@@ -343,32 +307,29 @@ describe('$route', function() {
expect(reloaded).not.toHaveBeenCalled();
expect(onRouteChange).not.toHaveBeenCalled();
$location.updateHash('/foo/aaa');
$location.path('/foo/aaa');
scope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(onRouteChange).toHaveBeenCalled();
reloaded.reset();
onRouteChange.reset();
$location.updateHash('/foo/bbb');
$location.path('/foo/bbb');
scope.$digest();
expect(reloaded).toHaveBeenCalled();
expect(onRouteChange).toHaveBeenCalled();
reloaded.reset();
onRouteChange.reset();
$location.hashSearch.foo = 'bar';
$location.search({foo: 'bar'});
scope.$digest();
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');
it('should update params when reloadOnSearch is disabled and .search() changes', function() {
var routeParams = jasmine.createSpy('routeParams');
$route.when('/foo', {controller: FooCtrl});
$route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false});
@@ -383,24 +344,24 @@ describe('$route', function() {
expect(routeParams).not.toHaveBeenCalled();
$location.updateHash('/foo');
$location.path('/foo');
scope.$digest();
expect(routeParams).toHaveBeenCalledWith({});
routeParams.reset();
// trigger reload
$location.hashSearch.foo = 'bar';
$location.search({foo: 'bar'});
scope.$digest();
expect(routeParams).toHaveBeenCalledWith({foo: 'bar'});
routeParams.reset();
$location.updateHash('/bar/123');
$location.path('/bar/123').search({});
scope.$digest();
expect(routeParams).toHaveBeenCalledWith({barId: '123'});
routeParams.reset();
// don't trigger reload
$location.hashSearch.foo = 'bar';
$location.search({foo: 'bar'});
scope.$digest();
expect(routeParams).toHaveBeenCalledWith({barId: '123', foo: 'bar'});
});
@@ -409,22 +370,19 @@ describe('$route', function() {
describe('reload', function(){
it('should reload even if reloadOnSearch is false', function(){
var scope = angular.scope(),
$location = scope.$service('$location'),
$route = scope.$service('$route'),
$routeParams = scope.$service('$routeParams'),
var $routeParams = scope.$service('$routeParams'),
count = 0;
$route.when('/bar/:barId', {controller: FooCtrl, reloadOnSearch: false});
function FooCtrl() { count ++; }
$location.updateHash('/bar/123');
$location.path('/bar/123');
scope.$digest();
expect($routeParams).toEqual({barId:'123'});
expect(count).toEqual(1);
$location.hash = '/bar/123?a=b';
$location.path('/bar/123').search('a=b');
scope.$digest();
expect($routeParams).toEqual({barId:'123', a:'b'});
expect(count).toEqual(1);

View File

@@ -1117,7 +1117,7 @@ describe("widget", function(){
it('should do nothing when no routes are defined', function() {
$location.updateHash('/unknown');
$location.path('/unknown');
rootScope.$digest();
expect(rootScope.$element.text()).toEqual('');
});
@@ -1129,14 +1129,14 @@ describe("widget", function(){
expect(rootScope.$element.text()).toEqual('');
$location.updateHash('/foo');
$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');
$location.updateHash('/bar');
$location.path('/bar');
$browser.xhr.expectGET('myUrl2').respond('angular is da best');
rootScope.$digest();
rootScope.$digest();
@@ -1147,14 +1147,14 @@ describe("widget", function(){
it('should remove all content when location changes to an unknown route', function() {
$route.when('/foo', {controller: angular.noop, template: 'myUrl1'});
$location.updateHash('/foo');
$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');
$location.updateHash('/unknown');
$location.path('/unknown');
rootScope.$digest();
rootScope.$digest();
expect(rootScope.$element.text()).toEqual('');
@@ -1164,7 +1164,7 @@ describe("widget", function(){
$route.when('/foo', {controller: angular.noop, template: 'myUrl1'});
rootScope.parentVar = 'parent';
$location.updateHash('/foo');
$location.path('/foo');
$browser.xhr.expectGET('myUrl1').respond('<div>{{parentVar}}</div>');
rootScope.$digest();
rootScope.$digest();
@@ -1183,7 +1183,7 @@ describe("widget", function(){
var myApp = angular.scope();
var $browser = myApp.$service('$browser');
$browser.xhr.expectGET('includePartial.html').respond('view: <ng:view></ng:view>');
$browser.url('http://server/#/foo');
myApp.$service('$location').path('/foo');
var $route = myApp.$service('$route');
$route.when('/foo', {controller: angular.noop, template: 'viewPartial.html'});
@@ -1220,7 +1220,7 @@ describe("widget", function(){
this.log.push('child');
};
$location.updateHash('/foo');
$location.path('/foo');
$browser.xhr.expectGET('viewPartial.html').
respond('<div ng:init="log.push(\'init\')">' +
'<div ng:controller="ChildCtrl"></div>' +
@@ -1230,12 +1230,12 @@ describe("widget", function(){
expect(rootScope.log).toEqual(['parent', 'init', 'child']);
$location.updateHash('');
$location.path('/');
rootScope.$apply();
expect(rootScope.log).toEqual(['parent', 'init', 'child']);
rootScope.log = [];
$location.updateHash('/foo');
$location.path('/foo');
rootScope.$apply();
$browser.defer.flush();