mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
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:
parent
5ba227c7cd
commit
22cb600280
6 changed files with 135 additions and 191 deletions
|
|
@ -11,7 +11,7 @@
|
|||
* @property {Array.<Object>} routes Array of all configured routes.
|
||||
*
|
||||
* @description
|
||||
* Watches `$location.hashPath` and tries to map the hash to an existing route
|
||||
* Watches `$location.url()` and tries to map the path to an existing route
|
||||
* definition. It is used for deep-linking URLs to controllers and views (HTML partials).
|
||||
*
|
||||
* The `$route` service is typically used in conjunction with {@link angular.widget.ng:view ng:view}
|
||||
|
|
@ -20,7 +20,6 @@
|
|||
* @example
|
||||
This example shows how changing the URL hash causes the <tt>$route</tt>
|
||||
to match a route against the URL, and the <tt>[[ng:include]]</tt> pulls in the partial.
|
||||
Try changing the URL in the input box to see changes.
|
||||
|
||||
<doc:example>
|
||||
<doc:source jsfiddle="false">
|
||||
|
|
@ -51,7 +50,7 @@
|
|||
<a href="#/Book/Moby/ch/1">Moby: Ch1</a> |
|
||||
<a href="#/Book/Gatsby">Gatsby</a> |
|
||||
<a href="#/Book/Gatsby/ch/4?key=value">Gatsby: Ch4</a><br/>
|
||||
$location.hashPath: <input type="text" name="$location.hashPath" size="80" />
|
||||
<pre>$location.path() = {{$location.path()}}</pre>
|
||||
<pre>$route.current.template = {{$route.current.template}}</pre>
|
||||
<pre>$route.current.params = {{$route.current.params}}</pre>
|
||||
<pre>$route.current.scope.name = {{$route.current.scope.name}}</pre>
|
||||
|
|
@ -159,21 +158,20 @@ angularServiceInject('$route', function($location, $routeParams) {
|
|||
* {@link angular.widget.ng:view ng:view} or
|
||||
* {@link angular.widget.ng:include ng:include} widgets.
|
||||
* - `redirectTo` – {(string|function())=} – value to update
|
||||
* {@link angular.service.$location $location} hash with and trigger route redirection.
|
||||
* {@link angular.service.$location $location} path with and trigger route redirection.
|
||||
*
|
||||
* If `redirectTo` is a function, it will be called with the following parameters:
|
||||
*
|
||||
* - `{Object.<string>}` - route parameters extracted from the current
|
||||
* `$location.hashPath` by applying the current route template.
|
||||
* - `{string}` - current `$location.hash`
|
||||
* - `{string}` - current `$location.hashPath`
|
||||
* - `{string}` - current `$location.hashSearch`
|
||||
* `$location.path()` by applying the current route template.
|
||||
* - `{string}` - current `$location.path()`
|
||||
* - `{Object}` - current `$location.search()`
|
||||
*
|
||||
* The custom `redirectTo` function is expected to return a string which will be used
|
||||
* to update `$location.hash`.
|
||||
* to update `$location.path()` and `$location.search()`.
|
||||
*
|
||||
* - `[reloadOnSearch=true]` - {boolean=} - reload route when $location.hashSearch
|
||||
* changes.
|
||||
* - `[reloadOnSearch=true]` - {boolean=} - reload route when only $location.search()
|
||||
* changes.
|
||||
*
|
||||
* If the option is set to false and url in the browser changes, then
|
||||
* $routeUpdate event is emited on the current route scope. You can use this event to
|
||||
|
|
@ -231,9 +229,7 @@ angularServiceInject('$route', function($location, $routeParams) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
this.$watch(function(){ return dirty + $location.hash; }, updateRoute);
|
||||
this.$watch(function() { return dirty + $location.url(); }, updateRoute);
|
||||
|
||||
return $route;
|
||||
|
||||
|
|
@ -278,10 +274,13 @@ angularServiceInject('$route', function($location, $routeParams) {
|
|||
$route.current = next;
|
||||
if (next) {
|
||||
if (next.redirectTo) {
|
||||
$location.update(isString(next.redirectTo)
|
||||
? {hashSearch: next.params, hashPath: interpolate(next.redirectTo, next.params)}
|
||||
: {hash: next.redirectTo(next.pathParams,
|
||||
$location.hash, $location.hashPath, $location.hashSearch)});
|
||||
if (isString(next.redirectTo)) {
|
||||
$location.path(interpolate(next.redirectTo, next.params)).search(next.params)
|
||||
.replace();
|
||||
} else {
|
||||
$location.url(next.redirectTo(next.pathParams, $location.path(), $location.search()))
|
||||
.replace();
|
||||
}
|
||||
} else {
|
||||
copy(next.params, $routeParams);
|
||||
next.scope = parentScope.$new(next.controller);
|
||||
|
|
@ -299,9 +298,9 @@ angularServiceInject('$route', function($location, $routeParams) {
|
|||
// Match a route
|
||||
var params, match;
|
||||
forEach(routes, function(route, path) {
|
||||
if (!match && (params = matcher($location.hashPath, path))) {
|
||||
if (!match && (params = matcher($location.path(), path))) {
|
||||
match = inherit(route, {
|
||||
params: extend({}, $location.hashSearch, params),
|
||||
params: extend({}, $location.search(), params),
|
||||
pathParams: params});
|
||||
match.$route = route;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,10 +8,10 @@
|
|||
*
|
||||
* @description
|
||||
* Current set of route parameters. The route parameters are a combination of the
|
||||
* {@link angular.service.$location $location} `hashSearch`, and `path`. The `path` parameters
|
||||
* {@link angular.service.$location $location} `search()`, and `path()`. The `path` parameters
|
||||
* are extracted when the {@link angular.service.$route $route} path is matched.
|
||||
*
|
||||
* In case of parameter name collision, `path` params take precedence over `hashSearch` params.
|
||||
* In case of parameter name collision, `path` params take precedence over `search` params.
|
||||
*
|
||||
* The service guarantees that the identity of the `$routeParams` object will remain unchanged
|
||||
* (but its properties will likely change) even when a route change occurs.
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue