import { createMock } from 'ts-auto-mock' ; import { anImportedObject } from '../utils/object/object' ; describe ( 'functions without types defined' , ( ) => { it ( 'should infer basic boolean object literal types' , ( ) => { function functionToMock ( ) { const primitiveValue = false ; function whateverFunction ( ) { return true ; } return { primitiveValue , whateverFunction } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . primitiveValue ) . toBe ( false ) ; expect ( type ( ) . whateverFunction ( ) ) . toBe ( true ) ; } ) ; it ( 'should infer basic string object literal types' , ( ) => { function functionToMock ( ) { const primitiveValue = 'Hello world' ; return { primitiveValue } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . primitiveValue ) . toBe ( 'Hello world' ) ; } ) ; it ( 'should infer basic object literal types' , ( ) => { function functionToMock ( ) { const primitiveValue = { test : 'hello' , } ; return { primitiveValue } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . primitiveValue ) . toEqual ( { test : 'hello' , } ) ; } ) ; it ( 'should use the default behaviour for variables with a type defined' , ( ) => { function functionToMock ( ) { const primitiveValue = true ; function whateverFunction ( ) { return true ; } return { primitiveValue , whateverFunction } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . primitiveValue ) . toBe ( false ) ; } ) ; it ( 'should use the default behaviour for internal function declarations with a type defined' , ( ) => { function functionToMock ( ) { const primitiveValue = true ; function whateverFunction ( ) { return true ; } return { primitiveValue , whateverFunction } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . whateverFunction ( ) ) . toBe ( false ) ; } ) ; it ( 'should use the default behaviour for internal function declarations with a type defined' , ( ) => { function functionToMock ( ) { const primitiveValue = true ; function whateverFunction ( ) { return true ; } return { primitiveValue , whateverFunction } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . whateverFunction ( ) ) . toBe ( false ) ; } ) ; it ( 'should infer object literal return types' , ( ) => { function functionToMock ( ) { return { a : 'hello world' , b : 123 } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) ) . toEqual ( { a : 'hello world' , b : 123 , } ) ; } ) ; it ( 'should infer variables outside the function' , ( ) => { const anObject = { a : 'hello world' , b : 123 } ; function functionToMock ( ) { return anObject ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) ) . toEqual ( { a : 'hello world' , b : 123 , } ) ; } ) ; it ( 'should infer variables from a different file' , ( ) => { function functionToMock ( ) { return anImportedObject ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) ) . toEqual ( { a : 'hello world' , b : 123 , } ) ; } ) ; it ( 'should infer a spread object' , ( ) => { function functionToMock ( ) { return { ... anImportedObject , } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) ) . toEqual ( { a : 'hello world' , b : 123 , } ) ; } ) ; it ( 'should set null when a parameter has not type' , ( ) => { function functionToMock ( param ) { return { param , } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( { test : 'hello' , } ) ) . toEqual ( { param : null , } ) ; } ) ; it ( 'should be able to infer parameter types' , ( ) => { function functionToMock ( param ) { return { param , } ; } const type = createMock < typeof functionToMock > ( ) ; expect ( type ( 'hello' ) ) . toEqual ( { param : '' , } ) ; } ) ; } ) ; describe ( 'arrow functions without types defined' , ( ) => { it ( 'should infer basic number binary expression types' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , b ) => a + b , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 1 , 2 ) ) . toBe ( 0 ) ; } ) ; it ( 'should infer basic string binary expression types' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , b ) => a + b , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 's' , 's2' ) ) . toBe ( '' ) ; } ) ; it ( 'should infer basic string template literal expression types' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , b ) => `${ a + b }` , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toBe ( '' ) ; } ) ; it ( 'should infer basic boolean types' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => ! ! a , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toBe ( false ) ; } ) ; it ( 'should infer basic array type' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => [ ] , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toEqual ( [ ] ) ; } ) ; it ( 'should infer function types' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => ( ) => [ ] , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ( ) ) . toEqual ( [ ] ) ; } ) ; it ( 'should infer identifiers' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => a , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toBe ( '' ) ; } ) ; it ( 'should infer undefined' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => undefined , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toBeUndefined ( ) ; } ) ; it ( 'should infer objects' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => ( { a : 'hello' , } ) , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toEqual ( { a : 'hello' , } ) ; } ) ; it ( 'should infer function calls' , ( ) => { const functionToMock = ( ) => ( { sum : ( a , _b ) => Object . is ( { } , { } ) , } ) ; const type = createMock < typeof functionToMock > ( ) ; expect ( type ( ) . sum ( 'test1' , 'test2' ) ) . toBe ( false ) ; } ) ; } ) ;