forked from angular-ui/ui-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlRouterSpec.js
111 lines (90 loc) · 3.33 KB
/
urlRouterSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
describe("UrlRouter", function () {
var $urp, $ur, location, match, scope;
beforeEach(function() {
angular.module('ui.router.router.test', function() {}).config(function ($urlRouterProvider) {
$urp = $urlRouterProvider;
$urp.rule(function ($injector, $location) {
var path = $location.path();
if (!/baz/.test(path)) return false;
return path.replace('baz', 'b4z');
}).when('/foo/:param', function($match) {
match = ['/foo/:param', $match];
}).when('/bar', function($match) {
match = ['/bar', $match];
});
});
module('ui.router.router', 'ui.router.router.test');
inject(function($rootScope, $location, $injector) {
scope = $rootScope.$new();
location = $location;
$ur = $injector.invoke($urp.$get);
});
});
describe("provider", function () {
it("should throw on non-function rules", function () {
expect(function() { $urp.rule(null); }).toThrow("'rule' must be a function")
expect(function() { $urp.otherwise(null); }).toThrow("'rule' must be a function")
});
});
describe("service", function() {
it("should execute rewrite rules", function () {
location.path("/foo");
scope.$emit("$locationChangeSuccess");
expect(location.path()).toBe("/foo");
location.path("/baz");
scope.$emit("$locationChangeSuccess");
expect(location.path()).toBe("/b4z");
});
it("should keep otherwise last", function () {
$urp.otherwise('/otherwise');
location.path("/lastrule");
scope.$emit("$locationChangeSuccess");
expect(location.path()).toBe("/otherwise");
$urp.when('/lastrule', function($match) {
match = ['/lastrule', $match];
});
location.path("/lastrule");
scope.$emit("$locationChangeSuccess");
expect(location.path()).toBe("/lastrule");
});
it("should allow custom URL matchers", function () {
var custom = {
url: { exec: function() {}, format: function() {}, concat: function() {} },
handler: function() {}
};
spyOn(custom.url, "exec").andReturn({});
spyOn(custom.url, "format").andReturn("/foo-bar");
spyOn(custom, "handler").andReturn(true);
$urp.when(custom.url, custom.handler);
scope.$broadcast("$locationChangeSuccess");
scope.$apply();
expect(custom.url.exec).toHaveBeenCalled();
expect(custom.url.format).not.toHaveBeenCalled();
expect(custom.handler).toHaveBeenCalled();
});
it('can be cancelled by preventDefault() in $locationChangeSuccess', inject(function () {
var called;
location.path("/baz");
scope.$on('$locationChangeSuccess', function (ev) {
ev.preventDefault();
called = true;
});
scope.$emit("$locationChangeSuccess");
expect(called).toBeTruthy();
expect(location.path()).toBe("/baz");
}));
it('can be deferred and updated in $locationChangeSuccess', inject(function ($urlRouter, $timeout) {
var called;
location.path("/baz");
scope.$on('$locationChangeSuccess', function (ev) {
ev.preventDefault();
called = true;
$timeout($urlRouter.sync, 2000);
});
scope.$emit("$locationChangeSuccess");
$timeout.flush();
expect(called).toBeTruthy();
expect(location.path()).toBe("/b4z");
}));
});
});