@@ -18,6 +18,8 @@ import {
1818 useConnector ,
1919 action ,
2020 state ,
21+ computed ,
22+ watch ,
2123} from 'reactant' ;
2224import type { IRouterOptions } from '..' ;
2325import {
@@ -497,3 +499,130 @@ test('`router` module with auto provider with createBrowserHistory', async () =>
497499 ) ;
498500 expect ( container . textContent ) . toBe ( 'HomeDashboard0' ) ;
499501} ) ;
502+
503+ test ( '`router` module with auto computed' , ( ) => {
504+ const fn = jest . fn ( ) ;
505+ const computedFn = jest . fn ( ) ;
506+
507+ @injectable ( )
508+ class Count {
509+ @state
510+ num = 0 ;
511+
512+ @action
513+ increase ( ) {
514+ this . num += 1 ;
515+ }
516+ }
517+
518+ @injectable ( )
519+ class DashboardView extends ViewModule {
520+ constructor ( public count : Count ) {
521+ super ( ) ;
522+ }
523+
524+ component ( ) {
525+ const num = useConnector ( ( ) => this . count . num ) ;
526+ return (
527+ < div onClick = { ( ) => this . count . increase ( ) } id = "increase" >
528+ { num }
529+ </ div >
530+ ) ;
531+ }
532+ }
533+ @injectable ( )
534+ class HomeView extends ViewModule {
535+ text = 'app' ;
536+
537+ getProps ( version : string ) {
538+ return {
539+ version : `${ this . text } v${ version } ` ,
540+ } ;
541+ }
542+
543+ component ( { version = '0.0.1' } ) {
544+ const data = useConnector ( ( ) => this . getProps ( version ) ) ;
545+ return < span id = "version" > { data . version } </ span > ;
546+ }
547+ }
548+
549+ @injectable ( )
550+ class AppView extends ViewModule {
551+ constructor (
552+ public homeView : HomeView ,
553+ public dashboardView : DashboardView ,
554+ public router : Router
555+ ) {
556+ super ( ) ;
557+ watch ( this , ( ) => this . currentPath , fn ) ;
558+ }
559+
560+ @computed
561+ get currentPath ( ) {
562+ computedFn ( this . router ?. currentPath ) ;
563+ return this . router ?. currentPath ;
564+ }
565+
566+ component ( ) {
567+ const { ConnectedRouter } = this . router ;
568+ return (
569+ < ConnectedRouter >
570+ < Switch >
571+ < Route exact path = "/" >
572+ < this . homeView . component version = "0.1.0" />
573+ </ Route >
574+ < Route path = "/a" >
575+ < this . dashboardView . component />
576+ </ Route >
577+ < Route path = "/b" >
578+ < this . dashboardView . component />
579+ </ Route >
580+ </ Switch >
581+ </ ConnectedRouter >
582+ ) ;
583+ }
584+ }
585+
586+ const app = createApp ( {
587+ modules : [
588+ {
589+ provide : RouterOptions ,
590+ useValue : {
591+ autoProvide : false ,
592+ createHistory : ( ) => createHashHistory ( ) ,
593+ } as IRouterOptions ,
594+ } ,
595+ ] ,
596+ main : AppView ,
597+ render,
598+ devOptions : {
599+ reduxDevTools : true ,
600+ autoComputed : true ,
601+ } ,
602+ } ) ;
603+ act ( ( ) => {
604+ app . bootstrap ( container ) ;
605+ } ) ;
606+ expect ( fn . mock . calls [ 0 ] [ 0 ] ) . toBe ( '/' ) ;
607+ expect ( computedFn . mock . calls [ 0 ] [ 0 ] ) . toBe ( undefined ) ;
608+ app . instance . router . push ( '/a' ) ;
609+ expect ( fn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/a' ) ;
610+ expect ( computedFn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/a' ) ;
611+ app . instance . router . replace ( '/b' ) ;
612+ expect ( fn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/b' ) ;
613+ expect ( computedFn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/b' ) ;
614+ expect ( app . instance . dashboardView . count . num ) . toBe ( 0 ) ;
615+ expect ( computedFn . mock . calls . length ) . toBe ( 4 ) ;
616+ expect ( app . instance . currentPath ) . toBe ( '/b' ) ;
617+ app . instance . dashboardView . count . increase ( ) ;
618+ expect ( app . instance . dashboardView . count . num ) . toBe ( 1 ) ;
619+ expect ( computedFn . mock . calls . length ) . toBe ( 4 ) ;
620+ app . instance . router . replace ( '/' ) ;
621+ expect ( fn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/' ) ;
622+ expect ( computedFn . mock . calls . slice ( - 1 ) [ 0 ] [ 0 ] ) . toBe ( '/' ) ;
623+
624+ expect ( computedFn . mock . calls . length ) . toBe ( 5 ) ;
625+ expect ( app . instance . currentPath ) . toBe ( '/' ) ;
626+ expect ( app . instance . currentPath ) . toBe ( '/' ) ;
627+ expect ( computedFn . mock . calls . length ) . toBe ( 5 ) ;
628+ } ) ;
0 commit comments