@@ -181,9 +181,7 @@ describe('Integration', () => {
181181
182182 const state3 = { loading : false , todos : [ 'todoB' ] } ;
183183 const call3 = 3 ;
184- // its 3+1 because on scope change we do NOT use context and force notify
185- // causing ones that have naturally re-rendered already to re-render once more.
186- expect ( children1 . mock . calls [ call3 + 1 ] ) . toEqual ( [ state3 , expectActions ] ) ;
184+ expect ( children1 . mock . calls [ call3 ] ) . toEqual ( [ state3 , expectActions ] ) ;
187185 expect ( children2 . mock . calls [ call3 ] ) . toEqual ( [ state3 , expectActions ] ) ;
188186 } ) ;
189187
@@ -487,4 +485,156 @@ describe('Integration', () => {
487485 } ) . toThrow ( / s h o u l d b e c o n t a i n e d / ) ;
488486 errorSpy . mockRestore ( ) ;
489487 } ) ;
488+
489+ describe ( 'dispatchTo' , ( ) => {
490+ const createTestElements = ( { mainContainer, otherContainer } ) => {
491+ const actionOther =
492+ ( n ) =>
493+ ( { setState } , { plus } ) =>
494+ setState ( { count : n , plus } ) ;
495+ const StoreOther = createStore ( {
496+ name : 'store-other' ,
497+ containedBy : otherContainer ,
498+ initialState : { } ,
499+ actions : { set : actionOther } ,
500+ } ) ;
501+ const StoreMain = createStore ( {
502+ name : 'store-main' ,
503+ containedBy : mainContainer ,
504+ initialState : { } ,
505+ actions : {
506+ setOther :
507+ ( n ) =>
508+ ( { dispatchTo } ) =>
509+ dispatchTo ( StoreOther , actionOther ( n ) ) ,
510+ } ,
511+ } ) ;
512+
513+ const MainSubscriber = createSubscriber ( StoreMain ) ;
514+ const OtherSubscriber = createSubscriber ( StoreOther ) ;
515+ const mainSpy = jest . fn ( ) . mockReturnValue ( null ) ;
516+ const otherSpy = jest . fn ( ) . mockReturnValue ( null ) ;
517+
518+ const Content = ( ) => (
519+ < >
520+ < MainSubscriber > { mainSpy } </ MainSubscriber >
521+ < OtherSubscriber > { otherSpy } </ OtherSubscriber >
522+ </ >
523+ ) ;
524+ return {
525+ Content,
526+ StoreMain,
527+ mainReturn : ( n = 0 ) => mainSpy . mock . calls [ n ] ,
528+ otherReturn : ( n = 0 ) => otherSpy . mock . calls [ n ] ,
529+ } ;
530+ } ;
531+
532+ it ( 'should allow dispatchTo global -> global' , ( ) => {
533+ const { Content, mainReturn, otherReturn } = createTestElements ( {
534+ mainContainer : null ,
535+ otherContainer : null ,
536+ } ) ;
537+
538+ render ( < Content /> ) ;
539+ const [ , mainActions ] = mainReturn ( 0 ) ;
540+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
541+
542+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
543+ } ) ;
544+
545+ it ( 'should allow dispatchTo contained -> contained' , ( ) => {
546+ const SharedContainer = createContainer ( ) ;
547+ const { Content, mainReturn, otherReturn } = createTestElements ( {
548+ mainContainer : SharedContainer ,
549+ otherContainer : SharedContainer ,
550+ } ) ;
551+
552+ render (
553+ < SharedContainer >
554+ < Content />
555+ </ SharedContainer >
556+ ) ;
557+ const [ , mainActions ] = mainReturn ( 0 ) ;
558+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
559+
560+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
561+ } ) ;
562+
563+ it ( 'should allow dispatchTo contained -> global' , ( ) => {
564+ const MainContainer = createContainer ( ) ;
565+ const { Content, mainReturn, otherReturn } = createTestElements ( {
566+ mainContainer : MainContainer ,
567+ otherContainer : null ,
568+ } ) ;
569+
570+ render (
571+ < MainContainer >
572+ < Content />
573+ </ MainContainer >
574+ ) ;
575+ const [ , mainActions ] = mainReturn ( 0 ) ;
576+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
577+
578+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
579+ } ) ;
580+
581+ it ( 'should allow dispatchTo global -> contained if properly contained' , ( ) => {
582+ const OtherContainer = createContainer ( { displayName : 'OtherContainer' } ) ;
583+ const { Content, mainReturn, otherReturn } = createTestElements ( {
584+ mainContainer : null ,
585+ otherContainer : OtherContainer ,
586+ } ) ;
587+
588+ render (
589+ < OtherContainer >
590+ < Content />
591+ </ OtherContainer >
592+ ) ;
593+ const [ , mainActions ] = mainReturn ( 0 ) ;
594+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
595+
596+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
597+ } ) ;
598+
599+ it ( 'should allow dispatchTo contained -> other contained' , async ( ) => {
600+ const MainContainer = createContainer ( ) ;
601+ const OtherContainer = createContainer ( ) ;
602+
603+ const { Content, mainReturn, otherReturn } = createTestElements ( {
604+ mainContainer : MainContainer ,
605+ otherContainer : OtherContainer ,
606+ } ) ;
607+
608+ render (
609+ < OtherContainer >
610+ < MainContainer >
611+ < Content />
612+ </ MainContainer >
613+ </ OtherContainer >
614+ ) ;
615+ const [ , mainActions ] = mainReturn ( 0 ) ;
616+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
617+
618+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
619+ } ) ;
620+
621+ it ( 'should allow dispatchTo override -> contained' , async ( ) => {
622+ const { Content, StoreMain, mainReturn, otherReturn } =
623+ createTestElements ( {
624+ mainContainer : null ,
625+ otherContainer : null ,
626+ } ) ;
627+ const OverrideContainer = createContainer ( StoreMain ) ;
628+
629+ render (
630+ < OverrideContainer >
631+ < Content />
632+ </ OverrideContainer >
633+ ) ;
634+ const [ , mainActions ] = mainReturn ( 0 ) ;
635+ act ( ( ) => mainActions . setOther ( 1 ) ) ;
636+
637+ expect ( otherReturn ( 1 ) ) . toEqual ( [ { count : 1 } , expect . any ( Object ) ] ) ;
638+ } ) ;
639+ } ) ;
490640} ) ;
0 commit comments