@@ -534,50 +534,49 @@ describe('whenCalledWith', () => {
534534 expect ( branch . mock . calls ) . toEqual ( [ [ 'A' ] ] ) ;
535535 } ) ;
536536
537- it ( 'does not leak the withImplementation temp fn into fallbackImpl ' , ( ) => {
537+ it ( 'lets the withImplementation temp fn take precedence over existing branches ' , ( ) => {
538538 const fn = moduleMocker . fn < ( arg : string ) => string > ( ) ;
539- fn . mockReturnValue ( 'default' ) ;
540539 fn . whenCalledWith ( 'x' ) . mockReturnValue ( 'X' ) ;
541540
541+ expect ( fn ( 'x' ) ) . toBe ( 'X' ) ;
542+
542543 fn . withImplementation (
543544 ( ) => 'temp' ,
544545 ( ) => {
545- // Before any re-arm inside the scope: temp wins for everything.
546546 expect ( fn ( 'x' ) ) . toBe ( 'temp' ) ;
547- expect ( fn ( 'other' ) ) . toBe ( 'temp' ) ;
548- // Calling whenCalledWith inside the scope reinstalls the dispatcher
549- // (mirroring how mockImplementation inside the scope would override).
550- // Registered branches route, non-matches fall through to the temp fn.
547+ } ,
548+ ) ;
549+ } ) ;
550+
551+ it ( 'reactivates branch routing after calling whenCalledWith inside withImplementation' , ( ) => {
552+ const fn = moduleMocker . fn < ( arg : string ) => string > ( ) ;
553+ fn . whenCalledWith ( 'x' ) . mockReturnValue ( 'X' ) ;
554+
555+ fn . withImplementation (
556+ ( ) => 'temp' ,
557+ ( ) => {
558+ expect ( fn ( 'x' ) ) . toBe ( 'temp' ) ;
559+
551560 fn . whenCalledWith ( 'y' ) . mockReturnValue ( 'Y' ) ;
552561 expect ( fn ( 'x' ) ) . toBe ( 'X' ) ;
553562 expect ( fn ( 'y' ) ) . toBe ( 'Y' ) ;
554- expect ( fn ( 'other' ) ) . toBe ( 'temp' ) ;
555563 } ,
556564 ) ;
557-
558- // After the scope: registrations preserved, fallback restored to default.
559- expect ( fn ( 'x' ) ) . toBe ( 'X' ) ;
560- expect ( fn ( 'y' ) ) . toBe ( 'Y' ) ;
561- expect ( fn ( 'other' ) ) . toBe ( 'default' ) ;
562565 } ) ;
563566
564- it ( 'honors withImplementation scope around whenCalledWith routing' , ( ) => {
565- const fn = moduleMocker . fn ( ) ;
566- fn . mockReturnValue ( 'default' ) ;
567- fn . whenCalledWith ( 'match' ) . mockReturnValue ( 'matched' ) ;
568- expect ( fn ( 'match' ) ) . toBe ( 'matched' ) ;
569- expect ( fn ( 'miss' ) ) . toBe ( 'default' ) ;
567+ it ( 'preserves existing and new branches after withImplementation' , ( ) => {
568+ const fn = moduleMocker . fn < ( arg : string ) => string > ( ) ;
569+ fn . whenCalledWith ( 'x' ) . mockReturnValue ( 'X' ) ;
570570
571571 fn . withImplementation (
572572 ( ) => 'temp' ,
573573 ( ) => {
574- expect ( fn ( 'match' ) ) . toBe ( 'temp' ) ;
575- expect ( fn ( 'miss' ) ) . toBe ( 'temp' ) ;
574+ fn . whenCalledWith ( 'y' ) . mockReturnValue ( 'Y' ) ;
576575 } ,
577576 ) ;
578577
579- expect ( fn ( 'match ' ) ) . toBe ( 'matched ' ) ;
580- expect ( fn ( 'miss ' ) ) . toBe ( 'default ' ) ;
578+ expect ( fn ( 'x ' ) ) . toBe ( 'X ' ) ;
579+ expect ( fn ( 'y ' ) ) . toBe ( 'Y ' ) ;
581580 } ) ;
582581
583582 it ( 'discriminates Maps and Sets by content via iterableEquality' , ( ) => {
0 commit comments