import { OverrideCode } from './override-code' ; describe ( 'OverrideCode' , ( ) => { it ( 'should throw when trying to wrap unknown member' , ( ) => { const foo = { } ; expect ( ( ) => { OverrideCode . wrap ( foo , 'bar' , 'test' ) ; } ) . toThrow ( ) ; } ) ; it ( 'should throw when trying to access not wrapped original function' , ( ) => { const unknownKey = 'foo' ; expect ( ( ) => { OverrideCode . getOriginal ( unknownKey ) ; } ) . toThrow ( ) ; } ) ; it ( 'should wrap original function' , ( ) => { const originalFunc = jest . fn ( ) . mockReturnValueOnce ( 'bar return value' ) ; const foo = { bar : originalFunc } ; OverrideCode . wrap ( foo , 'bar' , 'barBackup' ) ; foo . bar ( ) ; expect ( originalFunc ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'should use original function name as store key if none provided' , ( ) => { const originalFunc = jest . fn ( ) . mockReturnValueOnce ( 'bar return value' ) ; const foo = { bar : originalFunc } ; const overrideResponse = Symbol ( 'override return value' ) ; const overrideFunc = jest . fn ( ) . mockReturnValueOnce ( overrideResponse ) ; OverrideCode . wrap ( foo , 'bar' ) ; OverrideCode . override ( 'bar' , overrideFunc ) ; const result = foo . bar ( ) ; expect ( result ) . toBe ( overrideResponse ) ; } ) ; it ( 'should return value of override function' , ( ) => { const barResponse = Symbol ( 'bar return response' ) ; const overrideResponse = Symbol ( 'override return value' ) ; const originalFunc = jest . fn ( ) . mockReturnValueOnce ( barResponse ) ; const overrideFunc = jest . fn ( ) . mockReturnValueOnce ( overrideResponse ) ; const foo = { bar : originalFunc } ; OverrideCode . wrap ( foo , 'bar' , 'foo.bar' ) ; OverrideCode . override ( 'foo.bar' , overrideFunc ) ; const result = foo . bar ( ) ; expect ( result ) . toBe ( overrideResponse ) ; } ) ; it ( 'should allow to call original function inside override' , ( ) => { const originalFunc = jest . fn ( ) . mockReturnValueOnce ( 'bar return value' ) ; const overrideFunc = ( ) => { const originalFromOverride = OverrideCode . getOriginal ( 'foo.bar' ) ; originalFromOverride ( ) ; return 'override return value' ; } ; const foo = { bar : originalFunc } ; OverrideCode . wrap ( foo , 'bar' , 'foo.bar' ) ; OverrideCode . override ( 'foo.bar' , overrideFunc ) ; const result = foo . bar ( ) ; expect ( originalFunc ) . toHaveBeenCalledTimes ( 1 ) ; expect ( result ) . toBe ( 'override return value' ) ; } ) ; it ( 'should pass arguments as expected in original function in default wrapper' , ( ) => { const originalFunc = jest . fn ( ) . mockReturnValueOnce ( 'bar return value' ) ; const foo = { bar : originalFunc } ; OverrideCode . wrap ( foo , 'bar' , 'foo.bar' ) ; foo . bar ( 'a' , 'b' , 'c' ) ; expect ( originalFunc ) . toHaveBeenCalledWith ( 'a' , 'b' , 'c' ) ; } ) ; it ( 'should pass arguments as expected in original function' , ( ) => { const originalFunc = jest . fn ( ) . mockReturnValueOnce ( 'bar return value in custom override' ) ; const overrideFunc = ( a , b , c ) => { const originalFromOverride = OverrideCode . getOriginal ( 'foo.bar' ) ; originalFromOverride ( a , b , c ) ; return 'override return value' ; } ; const foo = { bar : originalFunc } ; OverrideCode . wrap ( foo , 'bar' , 'foo.bar' ) ; OverrideCode . override ( 'foo.bar' , overrideFunc ) ; foo . bar ( 'a' , 'b' , 'c' ) ; expect ( originalFunc ) . toHaveBeenCalledWith ( 'a' , 'b' , 'c' ) ; } ) ; } ) ;