Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mockProperty): make sure a falsy value can be assigned to a mock property #208

Merged
merged 1 commit into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});