Skip to content

Commit 34207b1

Browse files
committed
chore($urlRouter): add tests for location syncing
1 parent 3a03042 commit 34207b1

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/urlRouter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function $UrlRouterProvider( $locationProvider, $urlMatcherFactory) {
287287
},
288288

289289
push: function(urlMatcher, params, options) {
290-
$location.url(urlMatcher.format(params));
290+
$location.url(urlMatcher.format(params || {}));
291291
if (options && options.replace) $location.replace();
292292
},
293293

test/urlRouterSpec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,46 @@ describe("UrlRouter", function () {
106106
expect(called).toBeTruthy();
107107
expect(location.path()).toBe("/b4z");
108108
}));
109+
110+
describe("location updates", function() {
111+
it('can push location changes', inject(function($urlRouter, $location) {
112+
spyOn($location, "url");
113+
spyOn($location, "replace");
114+
$urlRouter.push(new UrlMatcher("/hello/:name"), { name: "world" });
115+
116+
expect($location.url).toHaveBeenCalledWith("/hello/world");
117+
expect($location.replace).not.toHaveBeenCalled();
118+
}));
119+
120+
it('can push a replacement location', inject(function($urlRouter, $location) {
121+
spyOn($location, "url");
122+
spyOn($location, "replace");
123+
$urlRouter.push(new UrlMatcher("/hello/:name"), { name: "world" }, { replace: true });
124+
125+
expect($location.url).toHaveBeenCalledWith("/hello/world");
126+
expect($location.replace).toHaveBeenCalled();
127+
}));
128+
129+
it('can push location changes with no parameters', inject(function($urlRouter, $location) {
130+
spyOn($location, "url");
131+
$urlRouter.push(new UrlMatcher("/hello/:name"));
132+
133+
expect($location.url).toHaveBeenCalledWith("/hello/");
134+
}));
135+
136+
it('can read and sync a copy of location URL', inject(function($urlRouter, $location) {
137+
$location.url('/old');
138+
139+
spyOn($location, 'url').andCallThrough();
140+
$urlRouter.update(true);
141+
expect($location.url).toHaveBeenCalled();
142+
143+
$location.url('/new');
144+
$urlRouter.update();
145+
146+
expect($location.url()).toBe('/old');
147+
}));
148+
});
109149
});
110150

111151
});

0 commit comments

Comments
 (0)