Skip to content

Commit

Permalink
fix(location): allow empty string param: Ng1LocationServices.url('')
Browse files Browse the repository at this point in the history
Closes #3563
  • Loading branch information
christopherthielen committed Dec 20, 2017
1 parent ab50f1b commit 01bbaf0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/locationServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @internalapi
* @module ng1
*/ /** */
import { LocationConfig, LocationServices, UIRouter, ParamType } from "@uirouter/core";
import { LocationConfig, LocationServices, UIRouter, ParamType, isDefined } from "@uirouter/core";
import { val, createProxyFunctions, removeFrom, isObject } from "@uirouter/core";
import { ILocationService, ILocationProvider } from "angular";

Expand Down Expand Up @@ -46,7 +46,7 @@ export class Ng1LocationServices implements LocationConfig, LocationServices {
}

url(newUrl?: string, replace = false, state?) {
if (newUrl) this.$location.url(newUrl);
if (isDefined(newUrl)) this.$location.url(newUrl);
if (replace) this.$location.replace();
if (state) this.$location.state(state);
return this.$location.url();
Expand Down
31 changes: 31 additions & 0 deletions test/urlRouterSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ describe("UrlRouter", function () {
expect(router.locationService.url).toHaveBeenCalledWith("/hello/", undefined);
}));

it('can push an empty url', inject(function($urlRouter, $location) {
spyOn(router.locationService, "url");
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
}));

// Angular 1.2 doesn't seem to support $location.url("")
if (angular.version.minor >= 3) {
// Test for https://github.com/angular-ui/ui-router/issues/3563
it('updates url after an empty url is pushed', inject(function($urlRouter, $location) {
$lp.html5Mode(false);
spyOn(router.locationService, "url").and.callThrough();
$urlRouter.push($umf.compile('/foobar'));
expect(router.locationService.url).toHaveBeenCalledWith("/foobar", undefined);
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
expect(router.locationService.url()).toBe('/');
}));

// Test #2 for https://github.com/angular-ui/ui-router/issues/3563
it('updates html5mode url after an empty url is pushed', inject(function($urlRouter, $location) {
$lp.html5Mode(true);
spyOn(router.locationService, "url").and.callThrough();
$urlRouter.push($umf.compile('/foobar'));
expect(router.locationService.url).toHaveBeenCalledWith("/foobar", undefined);
$urlRouter.push($umf.compile('/{id}', { params: { id: { squash: true, value: null } } }));
expect(router.locationService.url).toHaveBeenCalledWith("", undefined);
expect(router.locationService.url()).toBe('/');
}));
}

it('can push location changes that include a #fragment', inject(function($urlRouter, $location) {
// html5mode disabled
$lp.html5Mode(false);
Expand Down

0 comments on commit 01bbaf0

Please sign in to comment.