@@ -357,39 +357,21 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
357357 } )
358358
359359 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 ) ;
365363 } )
366364
367365 store . dispatch ( pushPath ( '/foo' ) )
368- expect ( store ) . toContainRoute ( {
369- path : '/foo' ,
370- changeId : 2 ,
371- replace : false ,
372- state : undefined
373- } )
374-
375366 store . dispatch ( pushPath ( '/foo' ) )
376- expect ( store ) . toContainRoute ( {
377- path : '/foo' ,
378- changeId : 3 ,
379- replace : false ,
380- state : undefined
381- } )
382-
383367 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' ] ) ;
390370 } )
391371
392372 it ( 'does not update the router for other state changes' , ( ) => {
373+ const state = store . getState ( ) ;
374+
393375 store . dispatch ( {
394376 type : 'RANDOM_ACTION' ,
395377 payload : {
@@ -399,22 +381,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
399381 }
400382 } )
401383
402- expect ( store ) . toContainRoute ( {
403- path : '/' ,
404- changeId : 1 ,
405- replace : false ,
406- state : undefined
407- } )
384+ expect ( state ) . toEqual ( store . getState ( ) ) ;
408385 } )
409386
410387 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-
418388 history . listenBefore ( location => {
419389 expect ( location . pathname ) . toEqual ( '/foo' )
420390 store . dispatch ( {
@@ -427,125 +397,44 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
427397 } )
428398 } )
429399
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+ } ) ;
445404
446405 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' } ) )
452406
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' ] )
506408 } )
507409
508410 it ( 'allows updating the route from within `listenBefore`' , ( ) => {
509- expect ( store ) . toContainRoute ( {
510- path : '/'
511- } )
512-
513411 history . listenBefore ( location => {
514412 if ( location . pathname === '/foo' ) {
515- expect ( store ) . toContainRoute ( {
516- path : '/foo' ,
517- changeId : 2 ,
518- replace : false ,
519- state : undefined
520- } )
521413 store . dispatch ( pushPath ( '/bar' ) )
522414 }
523415 else if ( location . pathname === '/replace' ) {
524- expect ( store ) . toContainRoute ( {
525- path : '/replace' ,
526- changeId : 4 ,
527- replace : false ,
528- state : { bar : 'baz' }
529- } )
530416 store . dispatch ( replacePath ( '/baz' , { foo : 'bar' } ) )
531417 }
532418 } )
533419
420+ const updates = [ ] ;
421+ history . listen ( location => {
422+ updates . push ( location . pathname ) ;
423+ } ) ;
424+
534425 store . dispatch ( pushPath ( '/foo' ) )
535426 expect ( store ) . toContainRoute ( {
536- path : '/bar' ,
537- changeId : 3 ,
538- replace : false ,
539- state : undefined
427+ path : '/bar'
540428 } )
541429
542430 store . dispatch ( pushPath ( '/replace' , { bar : 'baz' } ) )
543431 expect ( store ) . toContainRoute ( {
544432 path : '/baz' ,
545- changeId : 5 ,
546- replace : true ,
547- state : { foo : 'bar' }
433+ state : { foo : 'bar' } ,
434+ replace : true
548435 } )
436+
437+ expect ( updates ) . toEqual ( [ '/' , '/bar' , '/baz' ] ) ;
549438 } )
550439
551440 it ( 'throws if "routing" key is missing with default selectRouteState' , ( ) => {
@@ -582,10 +471,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
582471
583472 store . dispatch ( pushPath ( '/bar' ) )
584473 expect ( store ) . toContainRoute ( {
585- path : '/bar' ,
586- changeId : 2 ,
587- replace : false ,
588- state : undefined
474+ path : '/bar'
589475 } )
590476
591477 unsubscribe ( )
@@ -602,6 +488,33 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
602488 ( ) => store . dispatch ( pushPath ( '/foo' ) )
603489 ) . toNotThrow ( )
604490 } )
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+ } )
605518 } )
606519
607520 it ( 'handles basename history option' , ( ) => {
@@ -613,18 +526,12 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
613526
614527 store . dispatch ( pushPath ( '/bar' ) )
615528 expect ( store ) . toContainRoute ( {
616- path : '/bar' ,
617- changeId : 2 ,
618- replace : false ,
619- state : undefined
529+ path : '/bar'
620530 } )
621531
622532 history . pushState ( undefined , '/baz' ) ;
623533 expect ( store ) . toContainRoute ( {
624- path : '/baz' ,
625- changeId : 2 ,
626- replace : false ,
627- state : undefined
534+ path : '/baz'
628535 } )
629536 } )
630537 } )
0 commit comments