1
1
$ ( document ) . ready ( function ( ) {
2
2
3
3
var router = null ;
4
+ var location = null ;
4
5
var lastRoute = null ;
5
6
var lastArgs = [ ] ;
6
7
@@ -9,10 +10,33 @@ $(document).ready(function() {
9
10
lastArgs = args ;
10
11
}
11
12
13
+ var Location = function ( href ) {
14
+ this . replace ( href ) ;
15
+ } ;
16
+
17
+ _ . extend ( Location . prototype , {
18
+
19
+ replace : function ( href ) {
20
+ _ . extend ( this , _ . pick ( $ ( '<a></a>' , { href : href } ) [ 0 ] ,
21
+ 'href' ,
22
+ 'hash' ,
23
+ 'search' ,
24
+ 'fragment' ,
25
+ 'pathname'
26
+ ) ) ;
27
+ } ,
28
+
29
+ toString : function ( ) {
30
+ return this . href ;
31
+ }
32
+
33
+ } ) ;
34
+
12
35
module ( "Backbone.Router" , {
13
36
14
37
setup : function ( ) {
15
- Backbone . history = null ;
38
+ location = new Location ( 'http://example.com' ) ;
39
+ Backbone . history = new Backbone . History ( { location : location } ) ;
16
40
router = new Router ( { testing : 101 } ) ;
17
41
Backbone . history . interval = 9 ;
18
42
Backbone . history . start ( { pushState : false } ) ;
@@ -101,24 +125,20 @@ $(document).ready(function() {
101
125
equal ( router . testing , 101 ) ;
102
126
} ) ;
103
127
104
- asyncTest ( "Router: routes (simple)" , 4 , function ( ) {
105
- window . location . hash = 'search/news' ;
106
- setTimeout ( function ( ) {
107
- equal ( router . query , 'news' ) ;
108
- equal ( router . page , undefined ) ;
109
- equal ( lastRoute , 'search' ) ;
110
- equal ( lastArgs [ 0 ] , 'news' ) ;
111
- start ( ) ;
112
- } , 10 ) ;
128
+ test ( "Router: routes (simple)" , 4 , function ( ) {
129
+ location . replace ( 'http://example.com#search/news' ) ;
130
+ Backbone . history . checkUrl ( ) ;
131
+ equal ( router . query , 'news' ) ;
132
+ equal ( router . page , undefined ) ;
133
+ equal ( lastRoute , 'search' ) ;
134
+ equal ( lastArgs [ 0 ] , 'news' ) ;
113
135
} ) ;
114
136
115
- asyncTest ( "Router: routes (two part)" , 2 , function ( ) {
116
- window . location . hash = 'search/nyc/p10' ;
117
- setTimeout ( function ( ) {
118
- equal ( router . query , 'nyc' ) ;
119
- equal ( router . page , '10' ) ;
120
- start ( ) ;
121
- } , 10 ) ;
137
+ test ( "Router: routes (two part)" , 2 , function ( ) {
138
+ location . replace ( 'http://example.com#search/nyc/p10' ) ;
139
+ Backbone . history . checkUrl ( ) ;
140
+ equal ( router . query , 'nyc' ) ;
141
+ equal ( router . page , '10' ) ;
122
142
} ) ;
123
143
124
144
test ( "Router: routes via navigate" , 2 , function ( ) {
@@ -145,22 +165,12 @@ $(document).ready(function() {
145
165
} ) ;
146
166
} ) ;
147
167
148
- test ( "Router: doesn't fire routes to the same place twice" , 6 , function ( ) {
149
- equal ( router . count , 0 ) ;
150
- router . navigate ( 'counter' , { trigger : true } ) ;
151
- equal ( router . count , 1 ) ;
152
- router . navigate ( '/counter' , { trigger : true } ) ;
153
- router . navigate ( '/counter' , { trigger : true } ) ;
154
- equal ( router . count , 1 ) ;
155
- router . navigate ( 'search/counter' , { trigger : true } ) ;
156
- router . navigate ( 'counter' , { trigger : true } ) ;
157
- equal ( router . count , 2 ) ;
158
- Backbone . history . stop ( ) ;
159
- router . navigate ( 'search/counter' , { trigger : true } ) ;
160
- router . navigate ( 'counter' , { trigger : true } ) ;
161
- equal ( router . count , 2 ) ;
162
- Backbone . history . start ( ) ;
163
- equal ( router . count , 3 ) ;
168
+ test ( "loadUrl is not called for identical routes." , 0 , function ( ) {
169
+ Backbone . history . loadUrl = function ( ) { ok ( false ) ; } ;
170
+ location . replace ( 'http://example.com#route' ) ;
171
+ Backbone . history . navigate ( 'route' ) ;
172
+ Backbone . history . navigate ( '/route' ) ;
173
+ Backbone . history . navigate ( '/route' ) ;
164
174
} ) ;
165
175
166
176
test ( "Router: use implicit callback if none provided" , 1 , function ( ) {
@@ -169,73 +179,49 @@ $(document).ready(function() {
169
179
equal ( router . count , 1 ) ;
170
180
} ) ;
171
181
172
- asyncTest ( "Router: routes via navigate with {replace: true}" , 2 , function ( ) {
173
- var historyLength = window . history . length ;
174
- router . navigate ( 'search/manhattan/start_here' ) ;
175
- router . navigate ( 'search/manhattan/then_here' ) ;
176
- router . navigate ( 'search/manhattan/finally_here' , { replace : true } ) ;
177
-
178
- equal ( window . location . hash , "#search/manhattan/finally_here" ) ;
179
- window . history . go ( - 1 ) ;
180
- setTimeout ( function ( ) {
181
- equal ( window . location . hash , "#search/manhattan/start_here" ) ;
182
- start ( ) ;
183
- } , 500 ) ;
182
+ test ( "Router: routes via navigate with {replace: true}" , 1 , function ( ) {
183
+ location . replace ( 'http://example.com#start_here' ) ;
184
+ Backbone . history . checkUrl ( ) ;
185
+ location . replace = function ( href ) {
186
+ strictEqual ( href , new Location ( 'http://example.com#end_here' ) . href ) ;
187
+ } ;
188
+ Backbone . history . navigate ( 'end_here' , { replace : true } ) ;
184
189
} ) ;
185
190
186
- asyncTest ( "Router: routes (splats)" , 1 , function ( ) {
187
- window . location . hash = 'splat/long-list/of/splatted_99args/end' ;
188
- setTimeout ( function ( ) {
189
- equal ( router . args , 'long-list/of/splatted_99args' ) ;
190
- start ( ) ;
191
- } , 10 ) ;
191
+ test ( "Router: routes (splats)" , 1 , function ( ) {
192
+ location . replace ( 'http://example.com#splat/long-list/of/splatted_99args/end' ) ;
193
+ Backbone . history . checkUrl ( ) ;
194
+ equal ( router . args , 'long-list/of/splatted_99args' ) ;
192
195
} ) ;
193
196
194
- asyncTest ( "Router: routes (complex)" , 3 , function ( ) {
195
- window . location . hash = 'one/two/three/complex-part/four/five/six/seven' ;
196
- setTimeout ( function ( ) {
197
- equal ( router . first , 'one/two/three' ) ;
198
- equal ( router . part , 'part' ) ;
199
- equal ( router . rest , 'four/five/six/seven' ) ;
200
- start ( ) ;
201
- } , 10 ) ;
197
+ test ( "Router: routes (complex)" , 3 , function ( ) {
198
+ location . replace ( 'http://example.com#one/two/three/complex-part/four/five/six/seven' ) ;
199
+ Backbone . history . checkUrl ( ) ;
200
+ equal ( router . first , 'one/two/three' ) ;
201
+ equal ( router . part , 'part' ) ;
202
+ equal ( router . rest , 'four/five/six/seven' ) ;
202
203
} ) ;
203
204
204
- asyncTest ( "Router: routes (query)" , 5 , function ( ) {
205
- window . location . hash = 'mandel?a=b&c=d' ;
206
- setTimeout ( function ( ) {
207
- equal ( router . entity , 'mandel' ) ;
208
- equal ( router . queryArgs , 'a=b&c=d' ) ;
209
- equal ( lastRoute , 'query' ) ;
210
- equal ( lastArgs [ 0 ] , 'mandel' ) ;
211
- equal ( lastArgs [ 1 ] , 'a=b&c=d' ) ;
212
- start ( ) ;
213
- } , 10 ) ;
205
+ test ( "Router: routes (query)" , 5 , function ( ) {
206
+ location . replace ( 'http://example.com#mandel?a=b&c=d' ) ;
207
+ Backbone . history . checkUrl ( ) ;
208
+ equal ( router . entity , 'mandel' ) ;
209
+ equal ( router . queryArgs , 'a=b&c=d' ) ;
210
+ equal ( lastRoute , 'query' ) ;
211
+ equal ( lastArgs [ 0 ] , 'mandel' ) ;
212
+ equal ( lastArgs [ 1 ] , 'a=b&c=d' ) ;
214
213
} ) ;
215
214
216
- asyncTest ( "Router: routes (anything)" , 1 , function ( ) {
217
- window . location . hash = 'doesnt-match-a-route' ;
218
- setTimeout ( function ( ) {
219
- equal ( router . anything , 'doesnt-match-a-route' ) ;
220
- start ( ) ;
221
- window . location . hash = '' ;
222
- } , 10 ) ;
215
+ test ( "Router: routes (anything)" , 1 , function ( ) {
216
+ location . replace ( 'http://example.com#doesnt-match-a-route' ) ;
217
+ Backbone . history . checkUrl ( ) ;
218
+ equal ( router . anything , 'doesnt-match-a-route' ) ;
223
219
} ) ;
224
220
225
- asyncTest ( "Router: fires event when router doesn't have callback on it" , 1 , function ( ) {
226
- try {
227
- var callbackFired = false ;
228
- var myCallback = function ( ) { callbackFired = true ; } ;
229
- router . on ( "route:noCallback" , myCallback ) ;
230
- window . location . hash = "noCallback" ;
231
- setTimeout ( function ( ) {
232
- equal ( callbackFired , true ) ;
233
- start ( ) ;
234
- window . location . hash = '' ;
235
- } , 10 ) ;
236
- } catch ( err ) {
237
- ok ( false , "an exception was thrown trying to fire the router event with no router handler callback" ) ;
238
- }
221
+ test ( "Router: fires event when router doesn't have callback on it" , 1 , function ( ) {
222
+ router . on ( "route:noCallback" , function ( ) { ok ( true ) ; } ) ;
223
+ location . replace ( 'http://example.com#noCallback' ) ;
224
+ Backbone . history . checkUrl ( ) ;
239
225
} ) ;
240
226
241
227
test ( "#933, #908 - leading slash" , 2 , function ( ) {
@@ -265,17 +251,14 @@ $(document).ready(function() {
265
251
equal ( router . rest , 'has%20space' ) ;
266
252
} ) ;
267
253
268
- asyncTest ( "Router: correctly handles URLs with % (#868)" , 3 , function ( ) {
269
- window . location . hash = 'search/fat%3A1.5%25' ;
270
- setTimeout ( function ( ) {
271
- window . location . hash = 'search/fat' ;
272
- setTimeout ( function ( ) {
273
- equal ( router . query , 'fat' ) ;
274
- equal ( router . page , undefined ) ;
275
- equal ( lastRoute , 'search' ) ;
276
- start ( ) ;
277
- } , 50 ) ;
278
- } , 50 ) ;
254
+ test ( "Router: correctly handles URLs with % (#868)" , 3 , function ( ) {
255
+ location . replace ( 'http://example.com#search/fat%3A1.5%25' ) ;
256
+ Backbone . history . checkUrl ( ) ;
257
+ location . replace ( 'http://example.com#search/fat' ) ;
258
+ Backbone . history . checkUrl ( ) ;
259
+ equal ( router . query , 'fat' ) ;
260
+ equal ( router . page , undefined ) ;
261
+ equal ( lastRoute , 'search' ) ;
279
262
} ) ;
280
263
281
264
} ) ;
0 commit comments