@@ -12588,6 +12588,97 @@ describe("a router", () => {
1258812588 expect ( currentRouter . state . errors ) . toBe ( null ) ;
1258912589 } ) ;
1259012590 } ) ;
12591+
12592+ describe ( "short circuiting" , ( ) => {
12593+ it ( "short circuits a pipeline if you throw a Redirect from a middleware" , async ( ) => {
12594+ let middleware = jest . fn ( ( { request } ) => {
12595+ if ( request . url . endsWith ( "/a" ) ) {
12596+ throw redirect ( "/b" ) ;
12597+ }
12598+ } ) ;
12599+ let aLoader = jest . fn ( ( arg ) => "❌" ) ;
12600+ let bLoader = jest . fn ( ( arg ) => "✅" ) ;
12601+
12602+ currentRouter = createRouter ( {
12603+ routes : [
12604+ {
12605+ path : "/" ,
12606+ middleware,
12607+ children : [
12608+ {
12609+ path : "a" ,
12610+ loader : aLoader ,
12611+ } ,
12612+ {
12613+ path : "b" ,
12614+ loader : bLoader ,
12615+ } ,
12616+ ] ,
12617+ } ,
12618+ ] ,
12619+ history : createMemoryHistory ( ) ,
12620+ future : { unstable_middleware : true } ,
12621+ } ) . initialize ( ) ;
12622+
12623+ await currentRouter . navigate ( "/a" ) ;
12624+
12625+ expect ( currentRouter . state . location . pathname ) . toBe ( "/b" ) ;
12626+
12627+ expect ( middleware ) . toHaveBeenCalledTimes ( 2 ) ;
12628+ expect ( middleware . mock . calls [ 0 ] [ 0 ] . request . url ) . toEqual (
12629+ "http://localhost/a"
12630+ ) ;
12631+ expect ( middleware . mock . calls [ 1 ] [ 0 ] . request . url ) . toEqual (
12632+ "http://localhost/b"
12633+ ) ;
12634+
12635+ expect ( aLoader ) . toHaveBeenCalledTimes ( 0 ) ;
12636+ expect ( bLoader ) . toHaveBeenCalledTimes ( 1 ) ;
12637+ expect ( bLoader . mock . calls [ 0 ] [ 0 ] . request . url ) . toEqual (
12638+ "http://localhost/b"
12639+ ) ;
12640+ } ) ;
12641+
12642+ it ( "short circuits a pipeline if you throw an Error from a middleware" , async ( ) => {
12643+ let middleware = jest . fn ( ( { request } ) => {
12644+ if ( request . url . endsWith ( "/a" ) ) {
12645+ throw new Error ( "💥" ) ;
12646+ }
12647+ } ) ;
12648+ let aLoader = jest . fn ( ( arg ) => "✅" ) ;
12649+
12650+ currentRouter = createRouter ( {
12651+ routes : [
12652+ {
12653+ path : "/" ,
12654+ middleware,
12655+ children : [
12656+ {
12657+ path : "a" ,
12658+ loader : aLoader ,
12659+ } ,
12660+ ] ,
12661+ } ,
12662+ ] ,
12663+ history : createMemoryHistory ( ) ,
12664+ future : { unstable_middleware : true } ,
12665+ } ) . initialize ( ) ;
12666+
12667+ await currentRouter . navigate ( "/a" ) ;
12668+
12669+ expect ( currentRouter . state . location . pathname ) . toBe ( "/a" ) ;
12670+ expect ( currentRouter . state . loaderData ) . toEqual ( { } ) ;
12671+ expect ( currentRouter . state . errors ) . toEqual ( {
12672+ "0" : new Error ( "💥" ) ,
12673+ } ) ;
12674+
12675+ expect ( middleware ) . toHaveBeenCalledTimes ( 1 ) ;
12676+ expect ( middleware . mock . calls [ 0 ] [ 0 ] . request . url ) . toEqual (
12677+ "http://localhost/a"
12678+ ) ;
12679+ expect ( aLoader ) . toHaveBeenCalledTimes ( 0 ) ;
12680+ } ) ;
12681+ } ) ;
1259112682 } ) ;
1259212683
1259312684 describe ( "ssr" , ( ) => {
0 commit comments