diff --git a/src/transformer/descriptor/mock/mockPropertiesAssignments.ts b/src/transformer/descriptor/mock/mockPropertiesAssignments.ts index becd93d8f..b01272be9 100644 --- a/src/transformer/descriptor/mock/mockPropertiesAssignments.ts +++ b/src/transformer/descriptor/mock/mockPropertiesAssignments.ts @@ -1,4 +1,5 @@ import * as ts from 'typescript'; +import { SyntaxKind } from 'typescript'; import { TypescriptCreator } from '../../helper/creator'; import { MockIdentifierInternalValues, MockIdentifierSetParameterName } from '../../mockIdentifier/mockIdentifier'; import { Scope } from '../../scope/scope'; @@ -42,12 +43,25 @@ function GetLiteralMockProperty(descriptor: ts.Expression, member: PropertyLike) function GetLazyMockProperty(descriptor: ts.Expression, member: PropertyLike): ts.PropertyAssignment { const propertyName: string = TypescriptHelper.GetStringPropertyName(member.name); - const variableDeclarationName: ts.ElementAccessExpression = ts.createElementAccess(MockIdentifierInternalValues, ts.createStringLiteral(propertyName)); + const stringPropertyName: ts.StringLiteral = ts.createStringLiteral(propertyName); + const variableDeclarationName: ts.ElementAccessExpression = ts.createElementAccess(MockIdentifierInternalValues, stringPropertyName); const setVariableParameterName: ts.Identifier = MockIdentifierSetParameterName; const expressionGetAssignment: ts.BinaryExpression = ts.createBinary(variableDeclarationName, ts.SyntaxKind.EqualsToken, descriptor); - const getExpressionBody: ts.BinaryExpression = ts.createBinary(variableDeclarationName, ts.SyntaxKind.BarBarToken, expressionGetAssignment); + const hasOwnProperty: ts.Expression = ts.createCall( + ts.createPropertyAccess(MockIdentifierInternalValues, 'hasOwnProperty'), + null, + [stringPropertyName] + ); + + const getExpressionBody: ts.Expression = ts.createConditional( + hasOwnProperty, + ts.createToken(SyntaxKind.QuestionToken), + variableDeclarationName, + ts.createToken(SyntaxKind.ColonToken), + expressionGetAssignment + ); const setExpressionBody: ts.BinaryExpression = ts.createBinary(variableDeclarationName, ts.SyntaxKind.EqualsToken, setVariableParameterName); const returnGetStatement: ts.ReturnStatement = ts.createReturn(getExpressionBody); @@ -63,5 +77,5 @@ function GetLazyMockProperty(descriptor: ts.Expression, member: PropertyLike): t GetBooleanTrueDescriptor(), )]); - return ts.createPropertyAssignment(ts.createStringLiteral(propertyName), literal); + return ts.createPropertyAssignment(stringPropertyName, literal); } diff --git a/test/transformer/create-mock-values.test.ts b/test/transformer/create-mock-values.test.ts index 0869974c7..9b95f817c 100644 --- a/test/transformer/create-mock-values.test.ts +++ b/test/transformer/create-mock-values.test.ts @@ -1,9 +1,14 @@ import { createMock } from 'ts-auto-mock'; describe('create-mock-values', () => { + interface SubInterface { + property: number; + } + interface Interface { property: string; method(): void; + subInterface: SubInterface; } it('should create the mock merging the values provided', () => { @@ -14,4 +19,16 @@ describe('create-mock-values', () => { expect(properties.property).toBe('sss'); properties.method(); }); + + it('should create the mock merging the null values provided', () => { + const properties: Interface = createMock({ + property: null, + method: null, + subInterface: null, + }); + + expect(properties.property).toBeNull(); + expect(properties.method).toBeNull(); + expect(properties.subInterface).toBeNull(); + }); }); diff --git a/test/transformer/descriptor/properties/assignFalsy.test.ts b/test/transformer/descriptor/properties/assignFalsy.test.ts new file mode 100644 index 000000000..643cdcb59 --- /dev/null +++ b/test/transformer/descriptor/properties/assignFalsy.test.ts @@ -0,0 +1,49 @@ +import { createMock } from 'ts-auto-mock'; + +describe('mocking properties', () => { + describe('when property is a mock', () => { + interface SubInterface { + aProp: string; + } + + interface AnInterface { + bProp: SubInterface; + } + + it('should correctly assign null', () => { + const anInterface: AnInterface = createMock(); + + anInterface.bProp = null; + + expect(anInterface.bProp).toBeNull(); + }); + }); + + describe('when property is a value type', () => { + interface AnInterface { + aProp: string; + } + + it('should correctly assign null', () => { + const anInterface: AnInterface = createMock(); + + anInterface.aProp = null; + + expect(anInterface.aProp).toBeNull(); + }); + }); + + describe('when property is a function', () => { + interface AnInterface { + aProp(): string; + } + + it('should correctly assign null', () => { + const anInterface: AnInterface = createMock(); + + anInterface.aProp = null; + + expect(anInterface.aProp).toBeNull(); + }); + }); +});