@@ -936,6 +936,42 @@ describe("a router", () => {
936936 } ) ;
937937 } ) ;
938938
939+ it ( "does not run middlewares when complete hydrationData exists" , async ( ) => {
940+ let middlewareSpy = jest . fn ( ) ;
941+ let loaderSpy = jest . fn ( ) ;
942+ let router = createRouter ( {
943+ history : createMemoryHistory ( ) ,
944+ routes : [
945+ {
946+ id : "index" ,
947+ path : "/" ,
948+ middleware : [ middlewareSpy ] ,
949+ loader : loaderSpy ,
950+ } ,
951+ ] ,
952+ hydrationData : {
953+ loaderData : {
954+ index : "INDEX DATA" ,
955+ } ,
956+ } ,
957+ } ) ;
958+ router . initialize ( ) ;
959+
960+ expect ( router . state ) . toMatchObject ( {
961+ historyAction : "POP" ,
962+ location : {
963+ pathname : "/" ,
964+ } ,
965+ initialized : true ,
966+ navigation : IDLE_NAVIGATION ,
967+ loaderData : {
968+ index : "INDEX DATA" ,
969+ } ,
970+ } ) ;
971+ expect ( middlewareSpy ) . not . toHaveBeenCalled ( ) ;
972+ expect ( loaderSpy ) . not . toHaveBeenCalled ( ) ;
973+ } ) ;
974+
939975 it ( "kicks off initial data load if no hydration data is provided" , async ( ) => {
940976 let parentDfd = createDeferred ( ) ;
941977 let parentSpy = jest . fn ( ( ) => parentDfd . promise ) ;
@@ -993,6 +1029,61 @@ describe("a router", () => {
9931029 router . dispose ( ) ;
9941030 } ) ;
9951031
1032+ it ( "run middlewares without loaders on initial load if no hydration data is provided" , async ( ) => {
1033+ let parentDfd = createDeferred ( ) ;
1034+ let parentSpy = jest . fn ( ( ) => parentDfd . promise ) ;
1035+ let childDfd = createDeferred ( ) ;
1036+ let childSpy = jest . fn ( ( ) => childDfd . promise ) ;
1037+ let router = createRouter ( {
1038+ history : createMemoryHistory ( ) ,
1039+ routes : [
1040+ {
1041+ path : "/" ,
1042+ middleware : [ parentSpy ] ,
1043+ children : [
1044+ {
1045+ index : true ,
1046+ middleware : [ childSpy ] ,
1047+ } ,
1048+ ] ,
1049+ } ,
1050+ ] ,
1051+ } ) ;
1052+ router . initialize ( ) ;
1053+ await tick ( ) ;
1054+
1055+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
1056+ expect ( parentSpy . mock . calls . length ) . toBe ( 1 ) ;
1057+ expect ( childSpy . mock . calls . length ) . toBe ( 0 ) ;
1058+ expect ( router . state ) . toMatchObject ( {
1059+ historyAction : "POP" ,
1060+ location : expect . objectContaining ( { pathname : "/" } ) ,
1061+ initialized : false ,
1062+ navigation : IDLE_NAVIGATION ,
1063+ } ) ;
1064+ expect ( router . state . loaderData ) . toEqual ( { } ) ;
1065+
1066+ await parentDfd . resolve ( undefined ) ;
1067+ expect ( router . state ) . toMatchObject ( {
1068+ historyAction : "POP" ,
1069+ location : expect . objectContaining ( { pathname : "/" } ) ,
1070+ initialized : false ,
1071+ navigation : IDLE_NAVIGATION ,
1072+ } ) ;
1073+ expect ( router . state . loaderData ) . toEqual ( { } ) ;
1074+
1075+ await childDfd . resolve ( undefined ) ;
1076+ expect ( router . state ) . toMatchObject ( {
1077+ historyAction : "POP" ,
1078+ location : expect . objectContaining ( { pathname : "/" } ) ,
1079+ initialized : true ,
1080+ navigation : IDLE_NAVIGATION ,
1081+ loaderData : { } ,
1082+ } ) ;
1083+
1084+ router . dispose ( ) ;
1085+ } ) ;
1086+
9961087 it ( "allows routes to be initialized with undefined loaderData" , async ( ) => {
9971088 let t = setup ( {
9981089 routes : [
0 commit comments