Skip to content

Commit 01bbaf0

Browse files
fix(location): allow empty string param: Ng1LocationServices.url('')
Closes #3563
1 parent ab50f1b commit 01bbaf0

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/locationServices.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @internalapi
33
* @module ng1
44
*/ /** */
5-
import { LocationConfig, LocationServices, UIRouter, ParamType } from "@uirouter/core";
5+
import { LocationConfig, LocationServices, UIRouter, ParamType, isDefined } from "@uirouter/core";
66
import { val, createProxyFunctions, removeFrom, isObject } from "@uirouter/core";
77
import { ILocationService, ILocationProvider } from "angular";
88

@@ -46,7 +46,7 @@ export class Ng1LocationServices implements LocationConfig, LocationServices {
4646
}
4747

4848
url(newUrl?: string, replace = false, state?) {
49-
if (newUrl) this.$location.url(newUrl);
49+
if (isDefined(newUrl)) this.$location.url(newUrl);
5050
if (replace) this.$location.replace();
5151
if (state) this.$location.state(state);
5252
return this.$location.url();

test/urlRouterSpec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,37 @@ describe("UrlRouter", function () {
208208
expect(router.locationService.url).toHaveBeenCalledWith("/hello/", undefined);
209209
}));
210210

211+
it('can push an empty url', inject(function($urlRouter, $location) {
212+
spyOn(router.locationService, "url");
213+
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
214+
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
215+
}));
216+
217+
// Angular 1.2 doesn't seem to support $location.url("")
218+
if (angular.version.minor >= 3) {
219+
// Test for https://github.com/angular-ui/ui-router/issues/3563
220+
it('updates url after an empty url is pushed', inject(function($urlRouter, $location) {
221+
$lp.html5Mode(false);
222+
spyOn(router.locationService, "url").and.callThrough();
223+
$urlRouter.push($umf.compile('/foobar'));
224+
expect(router.locationService.url).toHaveBeenCalledWith("/foobar", undefined);
225+
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
226+
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
227+
expect(router.locationService.url()).toBe('/');
228+
}));
229+
230+
// Test #2 for https://github.com/angular-ui/ui-router/issues/3563
231+
it('updates html5mode url after an empty url is pushed', inject(function($urlRouter, $location) {
232+
$lp.html5Mode(true);
233+
spyOn(router.locationService, "url").and.callThrough();
234+
$urlRouter.push($umf.compile('/foobar'));
235+
expect(router.locationService.url).toHaveBeenCalledWith("/foobar", undefined);
236+
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
237+
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
238+
expect(router.locationService.url()).toBe('/');
239+
}));
240+
}
241+
211242
it('can push location changes that include a #fragment', inject(function($urlRouter, $location) {
212243
// html5mode disabled
213244
$lp.html5Mode(false);

0 commit comments

Comments
 (0)