Skip to content

Commit f0ce297

Browse files
committed
Initial test spec for $urlRouter.
1 parent 500299b commit f0ce297

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/urlRouter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function $UrlRouterProvider( $urlMatcherFactory) {
5454
handler = ['$match', function ($match) { return redirect.format($match); }];
5555
}
5656
else if (!isFunction(handler) && !isArray(handler))
57-
throw new Error("invalid 'handler' in when()");
57+
throw new Error("invalid 'handler' in when()");
5858

5959
rule = function ($injector, $location) {
6060
return handleIfMatch($injector, handler, what.exec($location.path(), $location.search()));
@@ -67,18 +67,18 @@ function $UrlRouterProvider( $urlMatcherFactory) {
6767
handler = ['$match', function ($match) { return interpolate(redirect, $match); }];
6868
}
6969
else if (!isFunction(handler) && !isArray(handler))
70-
throw new Error("invalid 'handler' in when()");
70+
throw new Error("invalid 'handler' in when()");
7171

7272
if (what.global || what.sticky)
73-
throw new Error("when() RegExp must not be global or sticky");
73+
throw new Error("when() RegExp must not be global or sticky");
7474

7575
rule = function ($injector, $location) {
7676
return handleIfMatch($injector, handler, what.exec($location.path()));
7777
};
7878
rule.prefix = regExpPrefix(what);
7979
}
8080
else
81-
throw new Error("invalid 'what' in when()");
81+
throw new Error("invalid 'what' in when()");
8282

8383
return this.rule(rule);
8484
};

test/urlRouterSpec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
describe("UrlRouter", function () {
2+
3+
var $urp, $ur, location, match, scope;
4+
5+
beforeEach(function() {
6+
angular.module('ui.router.test', function() {}).config(function ($urlRouterProvider) {
7+
$urp = $urlRouterProvider;
8+
9+
$urp.rule(function ($injector, $location) {
10+
return $location.path().replace('baz', 'b4z');
11+
}).when('/foo/:param', function($match) {
12+
match = ['/foo/:param', $match];
13+
}).when('/bar', function($match) {
14+
match = ['/bar', $match];
15+
}).when('/:param', function($match) {
16+
match = ['/:param', $match];
17+
});
18+
});
19+
20+
module('ui.router', 'ui.router.test');
21+
22+
inject(function($rootScope, $location, $injector) {
23+
scope = $rootScope.$new();
24+
location = $location;
25+
$ur = $injector.invoke($urp.$get);
26+
});
27+
});
28+
29+
describe("provider", function () {
30+
31+
it("should throw on non-function rules", function () {
32+
expect(function() { $urp.rule(null); }).toThrow("'rule' must be a function")
33+
expect(function() { $urp.otherwise(null); }).toThrow("'rule' must be a function")
34+
});
35+
36+
});
37+
38+
describe("service", function() {
39+
it("should execute rewrite rules", function () {
40+
location.path("/foo");
41+
scope.$emit("$locationChangeSuccess");
42+
expect(location.path()).toBe("/foo");
43+
44+
location.path("/baz");
45+
scope.$emit("$locationChangeSuccess");
46+
expect(location.path()).toBe("/b4z");
47+
});
48+
});
49+
50+
});

0 commit comments

Comments
 (0)