@@ -2,60 +2,64 @@ var assert = require('assert');
2
2
var React = require ( 'react' ) ;
3
3
var Router = require ( './index' ) ;
4
4
5
- var NestedRouter = React . createClass ( {
6
- render : function ( ) {
7
- return React . DOM . div ( null ,
8
- Router . Locations ( null ,
9
- Router . Location ( { path : '/__zuul/nested/' } , function ( props ) {
10
- return 'nested/root' ;
11
- } ) ,
12
- Router . Location ( { path : '/__zuul/nested/page' } , function ( props ) {
13
- return 'nested/page' ;
14
- } )
15
- ) ) ;
16
- }
17
- } ) ;
18
-
19
- var ContextualRouter = React . createClass ( {
20
- render : function ( ) {
21
- return React . DOM . div ( null ,
22
- Router . Locations ( { contextual : true } ,
23
- Router . Location ( { path : '/' } , function ( props ) {
24
- return 'contextual/root' ;
25
- } ) ,
26
- Router . Location ( { path : '/page' } , function ( props ) {
27
- return 'contextual/page' ;
28
- } )
29
- ) ) ;
30
- }
31
- } ) ;
32
-
33
- var App = React . createClass ( {
34
-
35
- render : function ( ) {
36
- return Router . Locations ( { ref : 'router' , className : 'App' } ,
37
- Router . Location ( { path : '/__zuul' } , function ( props ) {
38
- return Router . Link ( { ref : 'link' , href : '/__zuul/hello' } , 'mainpage' )
39
- } ) ,
40
- Router . Location ( { path : '/__zuul/nested/*' } , function ( props ) {
41
- return NestedRouter ( ) ;
42
- } ) ,
43
- Router . Location ( { path : '/__zuul/contextual/*' } , function ( props ) {
44
- return ContextualRouter ( ) ;
45
- } ) ,
46
- Router . Location ( { path : '/__zuul/:slug' } , function ( props ) {
47
- return props . slug
48
- } )
49
- ) ;
50
- }
51
- } ) ;
5
+ var historyAPI = window . history !== undefined && window . history . pushState !== undefined ;
52
6
53
7
function getText ( node ) {
54
8
return node . textContent || node . innerText ;
55
9
}
56
10
57
11
describe ( 'react-router-component' , function ( ) {
58
12
13
+ if ( ! historyAPI ) return ;
14
+
15
+ var NestedRouter = React . createClass ( {
16
+ render : function ( ) {
17
+ return React . DOM . div ( null ,
18
+ Router . Locations ( null ,
19
+ Router . Location ( { path : '/__zuul/nested/' } , function ( props ) {
20
+ return 'nested/root' ;
21
+ } ) ,
22
+ Router . Location ( { path : '/__zuul/nested/page' } , function ( props ) {
23
+ return 'nested/page' ;
24
+ } )
25
+ ) ) ;
26
+ }
27
+ } ) ;
28
+
29
+ var ContextualRouter = React . createClass ( {
30
+ render : function ( ) {
31
+ return React . DOM . div ( null ,
32
+ Router . Locations ( { contextual : true } ,
33
+ Router . Location ( { path : '/' } , function ( props ) {
34
+ return 'contextual/root' ;
35
+ } ) ,
36
+ Router . Location ( { path : '/page' } , function ( props ) {
37
+ return 'contextual/page' ;
38
+ } )
39
+ ) ) ;
40
+ }
41
+ } ) ;
42
+
43
+ var App = React . createClass ( {
44
+
45
+ render : function ( ) {
46
+ return Router . Locations ( { ref : 'router' , className : 'App' } ,
47
+ Router . Location ( { path : '/__zuul' } , function ( props ) {
48
+ return Router . Link ( { ref : 'link' , href : '/__zuul/hello' } , 'mainpage' )
49
+ } ) ,
50
+ Router . Location ( { path : '/__zuul/nested/*' } , function ( props ) {
51
+ return NestedRouter ( ) ;
52
+ } ) ,
53
+ Router . Location ( { path : '/__zuul/contextual/*' } , function ( props ) {
54
+ return ContextualRouter ( ) ;
55
+ } ) ,
56
+ Router . Location ( { path : '/__zuul/:slug' } , function ( props ) {
57
+ return props . slug
58
+ } )
59
+ ) ;
60
+ }
61
+ } ) ;
62
+
59
63
var host , app , router ;
60
64
61
65
beforeEach ( function ( ) {
@@ -77,7 +81,9 @@ describe('react-router-component', function() {
77
81
78
82
it ( 'renders' , function ( ) {
79
83
assert . equal ( getText ( host ) , 'mainpage' ) ;
80
- assert . ok ( app . getDOMNode ( ) . classList . contains ( 'App' ) ) ;
84
+ var dom = app . getDOMNode ( ) ;
85
+ if ( dom . classList )
86
+ assert . ok ( dom . classList . contains ( 'App' ) ) ;
81
87
} ) ;
82
88
83
89
it ( 'navigates to a different route' , function ( done ) {
@@ -151,6 +157,89 @@ describe('react-router-component', function() {
151
157
} ) ;
152
158
} ) ;
153
159
160
+ it ( 'navigates via onClick event' , function ( done ) {
161
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
162
+ router . refs . link . onClick ( ) ;
163
+ setTimeout ( function ( ) {
164
+ assert . equal ( getText ( host ) , 'hello' ) ;
165
+ done ( ) ;
166
+ } , 200 ) ;
167
+ } ) ;
168
+ } ) ;
169
+ } ) ;
170
+
171
+ describe ( 'react-router-component (hash routing)' , function ( ) {
172
+
173
+ var App = React . createClass ( {
174
+
175
+ render : function ( ) {
176
+ return Router . HashChange . Locations ( { ref : 'router' , className : 'App' } ,
177
+ Router . HashChange . Location ( { path : '/' } , function ( props ) {
178
+ return Router . HashChange . Link ( { ref : 'link' , href : '/hello' } , 'mainpage' )
179
+ } ) ,
180
+ Router . HashChange . Location ( { path : '/:slug' } , function ( props ) {
181
+ return props . slug
182
+ } )
183
+ ) ;
184
+ }
185
+ } ) ;
186
+
187
+ var host , app , router ;
188
+
189
+ beforeEach ( function ( ) {
190
+ host = document . createElement ( 'div' ) ;
191
+ document . body . appendChild ( host ) ;
192
+ app = React . renderComponent ( App ( ) , host ) ;
193
+ router = app . refs . router ;
194
+ } ) ;
195
+
196
+ afterEach ( function ( done ) {
197
+ React . unmountComponentAtNode ( host ) ;
198
+ document . body . removeChild ( host ) ;
199
+ host = null ;
200
+ app = null ;
201
+ router = null
202
+ window . location . hash = '' ;
203
+ setTimeout ( done , 200 ) ;
204
+ } ) ;
205
+
206
+ it ( 'renders' , function ( ) {
207
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
208
+ var dom = app . getDOMNode ( ) ;
209
+ if ( dom . classList )
210
+ assert . ok ( dom . classList . contains ( 'App' ) ) ;
211
+ } ) ;
212
+
213
+ it ( 'navigates to a different route' , function ( done ) {
214
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
215
+ router . navigate ( '/hello' , function ( ) {
216
+ assert . equal ( getText ( host ) , 'hello' ) ;
217
+ done ( ) ;
218
+ } ) ;
219
+ } ) ;
220
+
221
+ it ( 'handles "haschange" event' , function ( done ) {
222
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
223
+ router . navigate ( '/hello' , function ( ) {
224
+ assert . equal ( getText ( host ) , 'hello' ) ;
225
+ window . location . hash = '/' ;
226
+ setTimeout ( function ( ) {
227
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
228
+ done ( ) ;
229
+ } , 200 ) ;
230
+ } ) ;
231
+ } ) ;
232
+
233
+ describe ( 'Link component' , function ( ) {
234
+
235
+ it ( 'navigates via .navigate(path) call' , function ( done ) {
236
+ assert . equal ( getText ( host ) , 'mainpage' ) ;
237
+ router . refs . link . navigate ( '/hello' , function ( ) {
238
+ assert . equal ( getText ( host ) , 'hello' ) ;
239
+ done ( ) ;
240
+ } ) ;
241
+ } ) ;
242
+
154
243
it ( 'navigates via onClick event' , function ( done ) {
155
244
assert . equal ( getText ( host ) , 'mainpage' ) ;
156
245
router . refs . link . onClick ( ) ;
@@ -162,3 +251,4 @@ describe('react-router-component', function() {
162
251
} ) ;
163
252
164
253
} ) ;
254
+
0 commit comments