-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocationRouter.spec.js
157 lines (103 loc) · 4.51 KB
/
LocationRouter.spec.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Olives http://flams.github.com/olives
* The MIT License (MIT)
* Copyright (c) 2012-2013 Olivier Scherrer <pode.fr@gmail.com> - Olivier Wietrich <olivier.wietrich@gmail.com>
*/
require(["LocationRouter", "Router"], function (LocationRouter, Router) {
describe("LocationRouter initialisation", function () {
it("is a constructor function", function () {
expect(LocationRouter).toBeInstanceOf(Function);
});
it("inherits from Emily's router", function () {
var locationRouter = new LocationRouter();
expect(Object.getPrototypeOf(locationRouter)).toBeInstanceOf(Router);
});
});
describe("LocationRouter navigates to the route defined by the URL", function () {
var locationRouter = null;
beforeEach(function () {
locationRouter = new LocationRouter();
});
it("has a default function for parsing a hashtag", function () {
expect(locationRouter.parse).toBeInstanceOf(Function);
expect(locationRouter.parse("hello/im/the/router")).toEqual(["hello", "im", "the", "router"]);
expect(locationRouter.parse("#hello/im/the/router")).toEqual(["hello", "im", "the", "router"]);
});
it("parses the hashmark on start", function () {
window.location.hash = "hello/im/the/router";
expect(locationRouter.start).toBeInstanceOf(Function);
spyOn(locationRouter, "parse");
locationRouter.start();
expect(locationRouter.parse).toHaveBeenCalledWith("#hello/im/the/router");
});
it("navigate to the route parsed on start", function () {
window.location.hash = "hello/im/the/router";
spyOn(locationRouter, "navigate");
locationRouter.start();
expect(locationRouter.navigate).toHaveBeenCalledWith("hello", "im", "the", "router");
});
it("listens to hash change on start", function () {
spyOn(locationRouter, "bindOnHashChange");
locationRouter.start();
expect(locationRouter.bindOnHashChange).toHaveBeenCalled();
});
it("listens to hash change", function () {
spyOn(window, "addEventListener");
locationRouter.bindOnHashChange();
expect(window.addEventListener).toHaveBeenCalledWith("hashchange", locationRouter.boundOnHashChange, true);
});
it("parses the new hash and navigates to the corresponding route when the hash changes", function () {
var array = ["hello", "im", "the", "router"];
spyOn(locationRouter, "parse").andReturn(array);
spyOn(locationRouter, "navigate");
locationRouter.onHashChange({ newUrl: "url#hello/im/the/router"});
expect(locationRouter.parse).toHaveBeenCalledWith("hello/im/the/router");
expect(locationRouter.navigate).toHaveBeenCalledWith("hello", "im", "the", "router");
});
});
describe("LocationRouter updates the URL when navigating", function () {
var locationRouter = null;
beforeEach(function () {
locationRouter = new LocationRouter();
});
it("has a default function for serializing the arguments into a valid hashmark", function () {
expect(locationRouter.toUrl).toBeInstanceOf(Function);
expect(locationRouter.toUrl(["hello", "im", "the", "router"])).toBe("hello/im/the/router");
});
it("listens to route changes on start", function () {
spyOn(locationRouter, "bindOnRouteChange");
locationRouter.start();
expect(locationRouter.bindOnRouteChange).toHaveBeenCalled();
});
it("listens to route changes", function () {
spyOn(locationRouter, "watch");
locationRouter.bindOnRouteChange();
expect(locationRouter.watch).toHaveBeenCalledWith("route", locationRouter.onRouteChange, locationRouter);
});
it("updates the hash in the url on route change", function () {
spyOn(locationRouter, "toUrl").andReturn("hello/im/the/router");
locationRouter.onRouteChange("hello", "im", "the", "router");
expect(locationRouter.toUrl.mostRecentCall.args[0]).toEqual(["hello", "im", "the", "router"]);
expect(window.location.hash).toBe("#hello/im/the/router");
});
});
describe("LocationRouter can be destroyed", function () {
var locationRouter = null;
beforeEach(function() {
locationRouter = new LocationRouter();
});
it("removes the watch handler", function () {
spyOn(locationRouter, "watch").andReturn(1337);
spyOn(locationRouter, "unwatch");
locationRouter.start();
locationRouter.destroy();
expect(locationRouter.unwatch).toHaveBeenCalledWith(1337);
});
it("removes the hashchange event listener", function () {
spyOn(window, "removeEventListener");
locationRouter.start();
locationRouter.destroy();
expect(window.removeEventListener).toHaveBeenCalledWith("hashchange", locationRouter.boundOnHashChange, true);
});
});
});