import type * as ts from 'typescript' ; import { Identifiers } from '../../mockIdentifier/mockIdentifier' ; import { createBinaryExpression , createBlock , createCall , createElementAccessExpression , createExpressionStatement , createIdentifier , createIIFE , createObjectLiteral , createPropertyAccess , createPropertyAssignment , createReturn , createVariableDeclaration , createVariableStatement , } from '../../../typescriptFactory/typescriptFactory' ; import { core } from '../../core/core' ; import { GetMockMarkerProperty , Property } from './mockMarker' ; import { PropertyAssignments } from './mockPropertiesAssignments' ; export function GetMockCall ( properties , signature ) { const mockObjectReturnValueName = Identifiers . MockIdentifierObjectReturnValue ; const statements = [ createVariableStatement ( [ createVariableDeclaration ( Identifiers . MockIdentifierInternalValues , createObjectLiteral ( ) ) , createVariableDeclaration ( mockObjectReturnValueName , signature || createObjectLiteral ( properties . literals ) ) , ] ) , ] ; if ( signature ) { let literalProperty ; let index = 0 ; while ( ( literalProperty = properties . literals [ index ++ ] ) ) { statements . push ( AssignLiteralPropertyTo ( mockObjectReturnValueName , literalProperty ) ) ; } } if ( properties . lazy . length ) { const addPropertiesToUniqueVariable = AssignPropertiesTo ( properties . lazy , mockObjectReturnValueName ) ; statements . push ( addPropertiesToUniqueVariable ) ; } const addMockMarkerToUniqueVariable = AssignMockMarkerPropertyTo ( mockObjectReturnValueName ) ; statements . push ( addMockMarkerToUniqueVariable ) ; statements . push ( createReturn ( mockObjectReturnValueName ) ) ; const functionBlock = createBlock ( statements ) ; return createIIFE ( functionBlock ) ; } function AssignVariableTo ( variable , expression ) { const binaryExpression = createBinaryExpression ( variable , core . ts . SyntaxKind . EqualsToken , expression ) ; return createExpressionStatement ( binaryExpression ) ; } function AssignLiteralPropertyTo ( mockObjectReturnValueName , literalProperty ) { const propertyAccess = createElementAccessExpression ( mockObjectReturnValueName , literalProperty . name as ts . StringLiteral ) ; return AssignVariableTo ( propertyAccess , literalProperty . initializer ) ; } function AssignMockMarkerPropertyTo ( identifier ) { const mockMarkerProperty = GetMockMarkerProperty ( ) ; const argumentsDefineProperty = [ identifier , mockMarkerProperty . name , createObjectLiteral ( [ createPropertyAssignment ( 'value' , mockMarkerProperty . value ) , ] ) , ] ; const objectDefineProperty = createPropertyAccess ( createIdentifier ( 'Object' ) , createIdentifier ( 'defineProperty' ) ) ; const objectDefinePropertyCall = createCall ( objectDefineProperty , argumentsDefineProperty ) ; return createExpressionStatement ( objectDefinePropertyCall ) ; } function AssignPropertiesTo ( properties , uniqueVariable ) { const propertiesObject = createObjectLiteral ( properties , true ) ; const propertyAccessExpression = createPropertyAccess ( createIdentifier ( 'Object' ) , createIdentifier ( 'defineProperties' ) ) ; const callToObjectDefineProperties = createCall ( propertyAccessExpression , [ uniqueVariable , propertiesObject ] ) ; return createExpressionStatement ( callToObjectDefineProperties ) ; }