File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -12,3 +12,4 @@ export * from './createStubUtxoProvider';
1212export  *  from  './createGenericMockServer' ; 
1313export  *  from  './dataGeneration' ; 
1414export  *  from  './eraSummaries' ; 
15+ export  *  from  './patchObject' ; 
Original file line number Diff line number Diff line change 1+ export  const  patchObject  =  < T  extends  object > ( baseObject : T ,  patches : Partial < T > )  => 
2+   new  Proxy ( baseObject ,  { 
3+     get ( target ,  p ,  receiver )  { 
4+       const  value  =  p  in  patches  ? patches [ p  as  keyof  T ]  : target [ p  as  keyof  T ] ; 
5+       return  typeof  value  ===  'function'  ? value . bind ( receiver )  : value ; 
6+     } 
7+   } ) ; 
Original file line number Diff line number Diff line change 1+ import  {  patchObject  }  from  '../src' ; 
2+ 
3+ describe ( 'patchObject' ,  ( )  =>  { 
4+   const  object  =  { 
5+     fnKey ( )  { 
6+       return  this . key ; 
7+     } , 
8+     key : { } 
9+   } ; 
10+ 
11+   it ( 'preserves unpatched base object properties' ,  ( )  =>  { 
12+     const  patchedObject  =  patchObject ( object ,  { } ) ; 
13+     expect ( patchedObject . key ) . toBe ( object . key ) ; 
14+     expect ( patchedObject . fnKey ( ) ) . toBe ( object . fnKey ( ) ) ; 
15+   } ) ; 
16+ 
17+   it ( 'overrides patched properties' ,  ( )  =>  { 
18+     const  patchedKey  =  { } ; 
19+     const  patchedObject  =  patchObject ( object ,  { 
20+       key : patchedKey 
21+     } ) ; 
22+     expect ( patchedObject . key ) . toBe ( patchedKey ) ; 
23+     expect ( patchedObject . key ) . not . toBe ( object . key ) ; 
24+     expect ( patchedObject . fnKey ( ) ) . toBe ( patchedKey ) ; 
25+   } ) ; 
26+ } ) ; 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments