Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(location): fix parameter handling on search()
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Jul 31, 2013
1 parent 61906d3 commit 705c9d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/content/error/location/wpt.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ngdoc error
@name $location:wpt
@fullName Wrong parameter type
@description
29 changes: 18 additions & 11 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,17 +353,24 @@ LocationHashbangInHtml5Url.prototype =
* @return {string} search
*/
search: function(search, paramValue) {
if (isUndefined(search))
return this.$$search;

if (isDefined(paramValue)) {
if (paramValue === null) {
delete this.$$search[search];
} else {
this.$$search[search] = paramValue;
}
} else {
this.$$search = isString(search) ? parseKeyValue(search) : search;
switch (arguments.length) {
case 0:
return this.$$search;
case 1:
if (isString(search)) {
this.$$search = parseKeyValue(search);
} else if (isObject(search)) {
this.$$search = search;
} else {
throw $locationMinErr('wpt', 'First parameter of function must be string or an object.');
}
break;
default:
if (paramValue == undefined || paramValue == null) {
delete this.$$search[search];
} else {
this.$$search[search] = paramValue;
}
}

this.$$compose();
Expand Down
29 changes: 29 additions & 0 deletions test/ng/locationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ describe('$location', function() {
});


it('search() should handle multiple value', function() {
url.search('a&b');
expect(url.search()).toEqual({a: true, b: true});

url.search('a', null);

expect(url.search()).toEqual({b: true});

url.search('b', undefined);
expect(url.search()).toEqual({});
});


it('search() should handle single value', function() {
url.search('ignore');
expect(url.search()).toEqual({ignore: true});
});


it('search() should throw error an incorrect argument', function() {
expect(function() {
url.search(null);
}).toThrow('[$location:wpt] First parameter of function must be string or an object.');
expect(function() {
url.search(undefined);
}).toThrow('[$location:wpt] First parameter of function must be string or an object.');
});


it('hash() should change hash fragment', function() {
url.hash('new-hash');
expect(url.hash()).toBe('new-hash');
Expand Down

0 comments on commit 705c9d9

Please sign in to comment.