@@ -2,13 +2,23 @@ import expect from 'expect'
2
2
import React from 'react'
3
3
import Router from '../MemoryRouter'
4
4
import NavigationPrompt from '../NavigationPrompt'
5
+ import Redirect from '../Redirect'
5
6
import StaticRouter from '../StaticRouter'
6
7
import Match from '../Match'
7
8
import Miss from '../Miss'
8
9
import { Simulate } from 'react-addons-test-utils'
9
10
import Link from '../Link'
10
11
import { render } from 'react-dom'
11
12
13
+ const requiredPropsForStaticRouter = {
14
+ location : '/' ,
15
+ action : 'POP' ,
16
+ createHref : ( ) => { } ,
17
+ blockTransitions : ( ) => { } , // we sure we want this required? servers don't need it.
18
+ onPush : ( ) => { } ,
19
+ onReplace : ( ) => { }
20
+ }
21
+
12
22
describe ( 'Integration Tests' , ( ) => {
13
23
14
24
it ( 'renders root match' , ( ) => {
@@ -254,6 +264,46 @@ describe('clicking around', () => {
254
264
Simulate . click ( div . querySelector ( '#one' ) , leftClickEvent )
255
265
expect ( div . innerHTML ) . toContain ( TEXT1 )
256
266
} )
267
+
268
+ it ( 'pushes a new URL' , ( ) => {
269
+ const div = document . createElement ( 'div' )
270
+ const pushes = [ ]
271
+ const replaces = [ ]
272
+ const TARGET = '/TARGET'
273
+ render ( (
274
+ < StaticRouter
275
+ { ...requiredPropsForStaticRouter }
276
+ onPush = { ( loc ) => { pushes . push ( loc ) } }
277
+ onReplace = { ( loc ) => { replaces . push ( loc ) } }
278
+ >
279
+ < Link id = "target" to = { TARGET } > { TARGET } </ Link >
280
+ </ StaticRouter >
281
+ ) , div )
282
+ Simulate . click ( div . querySelector ( '#target' ) , leftClickEvent )
283
+ expect ( pushes . length ) . toEqual ( 1 )
284
+ expect ( pushes [ 0 ] . pathname ) . toEqual ( TARGET )
285
+ expect ( replaces . length ) . toEqual ( 0 )
286
+ } )
287
+
288
+ it ( 'replaces the current URL with replace' , ( ) => {
289
+ const div = document . createElement ( 'div' )
290
+ const pushes = [ ]
291
+ const replaces = [ ]
292
+ const TARGET = '/TARGET'
293
+ render ( (
294
+ < StaticRouter
295
+ { ...requiredPropsForStaticRouter }
296
+ onPush = { ( loc ) => { pushes . push ( loc ) } }
297
+ onReplace = { ( loc ) => { replaces . push ( loc ) } }
298
+ >
299
+ < Link id = "target" to = { TARGET } replace > { TARGET } </ Link >
300
+ </ StaticRouter >
301
+ ) , div )
302
+ Simulate . click ( div . querySelector ( '#target' ) , leftClickEvent )
303
+ expect ( pushes . length ) . toEqual ( 0 )
304
+ expect ( replaces . length ) . toEqual ( 1 )
305
+ expect ( replaces [ 0 ] . pathname ) . toEqual ( TARGET )
306
+ } )
257
307
} )
258
308
259
309
describe ( 'Link location descriptors' , ( ) => {
@@ -322,23 +372,14 @@ describe('Link with a query', () => {
322
372
323
373
describe ( 'Match and Miss Integration' , ( ) => {
324
374
325
- const requiredProps = {
326
- location : '/' ,
327
- action : 'POP' ,
328
- createHref : ( ) => { } ,
329
- blockTransitions : ( ) => { } , // we sure we want this required? servers don't need it.
330
- onPush : ( ) => { } ,
331
- onReplace : ( ) => { }
332
- }
333
-
334
375
describe ( 'Miss' , ( ) => {
335
376
it ( 'renders when nothing else matches' , ( ) => {
336
377
const div = document . createElement ( 'div' )
337
378
const FOO = '/FOO'
338
379
const MISS = '/MISS'
339
380
render ( (
340
381
< StaticRouter
341
- { ...requiredProps }
382
+ { ...requiredPropsForStaticRouter }
342
383
location = { { pathname : MISS } }
343
384
>
344
385
< div >
@@ -357,7 +398,7 @@ describe('Match and Miss Integration', () => {
357
398
const MISS = '/MISS'
358
399
render ( (
359
400
< StaticRouter
360
- { ...requiredProps }
401
+ { ...requiredPropsForStaticRouter }
361
402
location = { { pathname : FOO } }
362
403
>
363
404
< div >
@@ -402,3 +443,43 @@ describe('NavigationPrompt', () => {
402
443
expect ( message ) . toEqual ( TEXT )
403
444
} )
404
445
} )
446
+
447
+ describe ( 'Redirect' , ( ) => {
448
+ it ( 'replaces the current URL' , ( ) => {
449
+ const div = document . createElement ( 'div' )
450
+ const pushes = [ ]
451
+ const replaces = [ ]
452
+ const REDIRECTED = '/REDIRECTED'
453
+ render ( (
454
+ < StaticRouter
455
+ { ...requiredPropsForStaticRouter }
456
+ onPush = { ( loc ) => { pushes . push ( loc ) } }
457
+ onReplace = { ( loc ) => { replaces . push ( loc ) } }
458
+ >
459
+ < Redirect to = { REDIRECTED } />
460
+ </ StaticRouter >
461
+ ) , div )
462
+ expect ( pushes . length ) . toEqual ( 0 )
463
+ expect ( replaces . length ) . toEqual ( 1 )
464
+ expect ( replaces [ 0 ] . pathname ) . toEqual ( REDIRECTED )
465
+ } )
466
+
467
+ it ( 'pushes a new URL with push' , ( ) => {
468
+ const div = document . createElement ( 'div' )
469
+ const pushes = [ ]
470
+ const replaces = [ ]
471
+ const REDIRECTED = '/REDIRECTED'
472
+ render ( (
473
+ < StaticRouter
474
+ { ...requiredPropsForStaticRouter }
475
+ onPush = { ( loc ) => { pushes . push ( loc ) } }
476
+ onReplace = { ( loc ) => { replaces . push ( loc ) } }
477
+ >
478
+ < Redirect to = { REDIRECTED } push />
479
+ </ StaticRouter >
480
+ ) , div )
481
+ expect ( pushes . length ) . toEqual ( 1 )
482
+ expect ( pushes [ 0 ] . pathname ) . toEqual ( REDIRECTED )
483
+ expect ( replaces . length ) . toEqual ( 0 )
484
+ } )
485
+ } )
0 commit comments