diff --git a/src/locationServices.ts b/src/locationServices.ts index 6d2e7e4f8..ca856fe15 100644 --- a/src/locationServices.ts +++ b/src/locationServices.ts @@ -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"; @@ -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(); diff --git a/test/urlRouterSpec.ts b/test/urlRouterSpec.ts index 3c2dd2dbd..8ffe6dee6 100644 --- a/test/urlRouterSpec.ts +++ b/test/urlRouterSpec.ts @@ -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);