Skip to content

Commit

Permalink
fix(mockProperty): make sure a falsy value can be assigned to a mock …
Browse files Browse the repository at this point in the history
…property (#208)
  • Loading branch information
Pmyl committed Jan 30, 2020
1 parent e56dd7e commit 0b37699
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/transformer/descriptor/mock/mockPropertiesAssignments.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
17 changes: 17 additions & 0 deletions test/transformer/create-mock-values.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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<Interface>({
property: null,
method: null,
subInterface: null,
});

expect(properties.property).toBeNull();
expect(properties.method).toBeNull();
expect(properties.subInterface).toBeNull();
});
});
49 changes: 49 additions & 0 deletions test/transformer/descriptor/properties/assignFalsy.test.ts
Original file line number Diff line number Diff line change
@@ -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>();

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>();

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>();

anInterface.aProp = null;

expect(anInterface.aProp).toBeNull();
});
});
});

0 comments on commit 0b37699

Please sign in to comment.