@@ -357,39 +357,21 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
357
357
} )
358
358
359
359
it ( 'updates the router even if path is the same' , ( ) => {
360
- expect ( store ) . toContainRoute ( {
361
- path : '/' ,
362
- changeId : 1 ,
363
- replace : false ,
364
- state : undefined
360
+ const updates = [ ]
361
+ const historyUnsubscribe = history . listen ( location => {
362
+ updates . push ( location . pathname ) ;
365
363
} )
366
364
367
365
store . dispatch ( pushPath ( '/foo' ) )
368
- expect ( store ) . toContainRoute ( {
369
- path : '/foo' ,
370
- changeId : 2 ,
371
- replace : false ,
372
- state : undefined
373
- } )
374
-
375
366
store . dispatch ( pushPath ( '/foo' ) )
376
- expect ( store ) . toContainRoute ( {
377
- path : '/foo' ,
378
- changeId : 3 ,
379
- replace : false ,
380
- state : undefined
381
- } )
382
-
383
367
store . dispatch ( replacePath ( '/foo' ) )
384
- expect ( store ) . toContainRoute ( {
385
- path : '/foo' ,
386
- changeId : 4 ,
387
- replace : true ,
388
- state : undefined
389
- } )
368
+
369
+ expect ( updates ) . toEqual ( [ '/' , '/foo' , '/foo' , '/foo' ] ) ;
390
370
} )
391
371
392
372
it ( 'does not update the router for other state changes' , ( ) => {
373
+ const state = store . getState ( ) ;
374
+
393
375
store . dispatch ( {
394
376
type : 'RANDOM_ACTION' ,
395
377
payload : {
@@ -399,22 +381,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
399
381
}
400
382
} )
401
383
402
- expect ( store ) . toContainRoute ( {
403
- path : '/' ,
404
- changeId : 1 ,
405
- replace : false ,
406
- state : undefined
407
- } )
384
+ expect ( state ) . toEqual ( store . getState ( ) ) ;
408
385
} )
409
386
410
387
it ( 'only updates the router once when dispatching from `listenBefore`' , ( ) => {
411
- expect ( store ) . toContainRoute ( {
412
- path : '/' ,
413
- changeId : 1 ,
414
- replace : false ,
415
- state : undefined
416
- } )
417
-
418
388
history . listenBefore ( location => {
419
389
expect ( location . pathname ) . toEqual ( '/foo' )
420
390
store . dispatch ( {
@@ -427,125 +397,44 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
427
397
} )
428
398
} )
429
399
430
- store . dispatch ( pushPath ( '/foo' ) )
431
- expect ( store ) . toContainRoute ( {
432
- path : '/foo' ,
433
- changeId : 2 ,
434
- replace : false ,
435
- state : undefined
436
- } )
437
- } )
438
-
439
- it ( 'does not unnecessarily update the store' , ( ) => {
440
- const updates = [ ]
441
-
442
- const unsubscribeFromStore = store . subscribe ( ( ) => {
443
- updates . push ( store . getState ( ) )
444
- } )
400
+ const updates = [ ] ;
401
+ history . listen ( location => {
402
+ updates . push ( location . pathname ) ;
403
+ } ) ;
445
404
446
405
store . dispatch ( pushPath ( '/foo' ) )
447
- store . dispatch ( pushPath ( '/foo' ) )
448
- store . dispatch ( pushPath ( '/foo' , { bar : 'baz' } ) )
449
- history . pushState ( { foo : 'bar' } , '/foo' )
450
- store . dispatch ( replacePath ( '/bar' ) )
451
- store . dispatch ( replacePath ( '/bar' , { bar : 'foo' } ) )
452
406
453
- unsubscribeFromStore ( )
454
-
455
- expect ( updates . length ) . toBe ( 6 )
456
- expect ( updates ) . toEqual ( [
457
- {
458
- routing : {
459
- changeId : 2 ,
460
- path : '/foo' ,
461
- state : undefined ,
462
- replace : false
463
- }
464
- } ,
465
- {
466
- routing : {
467
- changeId : 3 ,
468
- path : '/foo' ,
469
- state : undefined ,
470
- replace : false
471
- }
472
- } ,
473
- {
474
- routing : {
475
- changeId : 4 ,
476
- path : '/foo' ,
477
- state : { bar : 'baz' } ,
478
- replace : false
479
- }
480
- } ,
481
- {
482
- routing : {
483
- changeId : 4 ,
484
- path : '/foo' ,
485
- state : { foo : 'bar' } ,
486
- replace : true
487
- }
488
- } ,
489
- {
490
- routing : {
491
- changeId : 5 ,
492
- path : '/bar' ,
493
- state : undefined ,
494
- replace : true
495
- }
496
- } ,
497
- {
498
- routing : {
499
- changeId : 6 ,
500
- path : '/bar' ,
501
- state : { bar : 'foo' } ,
502
- replace : true
503
- }
504
- }
505
- ] )
407
+ expect ( updates ) . toEqual ( [ '/' , '/foo' ] )
506
408
} )
507
409
508
410
it ( 'allows updating the route from within `listenBefore`' , ( ) => {
509
- expect ( store ) . toContainRoute ( {
510
- path : '/'
511
- } )
512
-
513
411
history . listenBefore ( location => {
514
412
if ( location . pathname === '/foo' ) {
515
- expect ( store ) . toContainRoute ( {
516
- path : '/foo' ,
517
- changeId : 2 ,
518
- replace : false ,
519
- state : undefined
520
- } )
521
413
store . dispatch ( pushPath ( '/bar' ) )
522
414
}
523
415
else if ( location . pathname === '/replace' ) {
524
- expect ( store ) . toContainRoute ( {
525
- path : '/replace' ,
526
- changeId : 4 ,
527
- replace : false ,
528
- state : { bar : 'baz' }
529
- } )
530
416
store . dispatch ( replacePath ( '/baz' , { foo : 'bar' } ) )
531
417
}
532
418
} )
533
419
420
+ const updates = [ ] ;
421
+ history . listen ( location => {
422
+ updates . push ( location . pathname ) ;
423
+ } ) ;
424
+
534
425
store . dispatch ( pushPath ( '/foo' ) )
535
426
expect ( store ) . toContainRoute ( {
536
- path : '/bar' ,
537
- changeId : 3 ,
538
- replace : false ,
539
- state : undefined
427
+ path : '/bar'
540
428
} )
541
429
542
430
store . dispatch ( pushPath ( '/replace' , { bar : 'baz' } ) )
543
431
expect ( store ) . toContainRoute ( {
544
432
path : '/baz' ,
545
- changeId : 5 ,
546
- replace : true ,
547
- state : { foo : 'bar' }
433
+ state : { foo : 'bar' } ,
434
+ replace : true
548
435
} )
436
+
437
+ expect ( updates ) . toEqual ( [ '/' , '/bar' , '/baz' ] ) ;
549
438
} )
550
439
551
440
it ( 'throws if "routing" key is missing with default selectRouteState' , ( ) => {
@@ -582,10 +471,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
582
471
583
472
store . dispatch ( pushPath ( '/bar' ) )
584
473
expect ( store ) . toContainRoute ( {
585
- path : '/bar' ,
586
- changeId : 2 ,
587
- replace : false ,
588
- state : undefined
474
+ path : '/bar'
589
475
} )
590
476
591
477
unsubscribe ( )
@@ -602,6 +488,33 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
602
488
( ) => store . dispatch ( pushPath ( '/foo' ) )
603
489
) . toNotThrow ( )
604
490
} )
491
+
492
+ it ( 'only triggers history once when updating path via store' , ( ) => {
493
+ const updates = [ ]
494
+ const historyUnsubscribe = history . listen ( location => {
495
+ updates . push ( location . pathname ) ;
496
+ } )
497
+
498
+ store . dispatch ( pushPath ( '/bar' ) ) ;
499
+ store . dispatch ( pushPath ( '/baz' ) ) ;
500
+ expect ( updates ) . toEqual ( [ '/' , '/bar' , '/baz' ] )
501
+
502
+ historyUnsubscribe ( )
503
+ } )
504
+
505
+ it ( 'only triggers store once when updating path via store' , ( ) => {
506
+ const updates = [ ]
507
+ const storeUnsubscribe = store . subscribe ( ( ) => {
508
+ updates . push ( store . getState ( ) . routing . path ) ;
509
+ } )
510
+
511
+ store . dispatch ( pushPath ( '/bar' ) ) ;
512
+ store . dispatch ( pushPath ( '/baz' ) ) ;
513
+ store . dispatch ( replacePath ( '/foo' ) ) ;
514
+ expect ( updates ) . toEqual ( [ '/bar' , '/baz' , '/foo' ] )
515
+
516
+ storeUnsubscribe ( )
517
+ } )
605
518
} )
606
519
607
520
it ( 'handles basename history option' , ( ) => {
@@ -613,18 +526,12 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
613
526
614
527
store . dispatch ( pushPath ( '/bar' ) )
615
528
expect ( store ) . toContainRoute ( {
616
- path : '/bar' ,
617
- changeId : 2 ,
618
- replace : false ,
619
- state : undefined
529
+ path : '/bar'
620
530
} )
621
531
622
532
history . pushState ( undefined , '/baz' ) ;
623
533
expect ( store ) . toContainRoute ( {
624
- path : '/baz' ,
625
- changeId : 2 ,
626
- replace : false ,
627
- state : undefined
534
+ path : '/baz'
628
535
} )
629
536
} )
630
537
} )
0 commit comments