Skip to content

Commit 3a051d2

Browse files
committed
enhancement(transformer): Implement conditional typing for direct mock calls of interfaces with call signatures
1 parent aa20d87 commit 3a051d2

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

src/transformer/descriptor/mock/mockCall.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,38 @@ import { PropertyAssignments } from './mockPropertiesAssignments';
88
export function GetMockCall(properties: PropertyAssignments, signatures: MethodSignature[]): ts.CallExpression {
99
const mockObjectReturnValueName: ts.Identifier = MockIdentifierObjectReturnValue;
1010

11-
if (signatures.length) {
11+
const variableStatements: ts.VariableDeclaration[] = [
12+
TypescriptCreator.createVariableDeclaration(MockIdentifierInternalValues, ts.createObjectLiteral()),
13+
];
14+
const propertyAssignmentStatements: ts.ExpressionStatement[] = [];
15+
16+
const isCallable: boolean = !!signatures.length;
17+
if (isCallable) {
1218
// FIXME: It'd probably be wise to extract the name of the callable
13-
// signature and fallback to `new` or smth if there is none.
14-
return GetMethodDescriptor(ts.createStringLiteral('new'), signatures);
19+
// signature and only fallback to `new` if there is none (or something
20+
// shorter).
21+
const callableEntry: ts.CallExpression = GetMethodDescriptor(ts.createStringLiteral('new'), signatures);
22+
23+
variableStatements.push(
24+
TypescriptCreator.createVariableDeclaration(mockObjectReturnValueName, callableEntry),
25+
);
26+
27+
propertyAssignmentStatements.push(
28+
...properties.literals.map(
29+
(literalProperty: ts.PropertyAssignment) => AssignLiteralPropertyTo(mockObjectReturnValueName, literalProperty)
30+
),
31+
);
32+
} else {
33+
variableStatements.push(
34+
TypescriptCreator.createVariableDeclaration(mockObjectReturnValueName, ts.createObjectLiteral(properties.literals)),
35+
);
1536
}
1637

1738
const statements: ts.Statement[] = [
18-
TypescriptCreator.createVariableStatement([
19-
TypescriptCreator.createVariableDeclaration(MockIdentifierInternalValues, ts.createObjectLiteral()),
20-
TypescriptCreator.createVariableDeclaration(mockObjectReturnValueName, ts.createObjectLiteral(properties.literals)),
21-
]),
39+
TypescriptCreator.createVariableStatement(variableStatements),
40+
...propertyAssignmentStatements,
2241
];
2342

24-
// if (signatures[0]) {
25-
// let literalProperty: ts.PropertyAssignment;
26-
// let index: number = 0;
27-
28-
// // tslint:disable-next-line:no-conditional-assignment
29-
// while ((literalProperty = properties.literals[index++])) {
30-
// statements.push(AssignLiteralPropertyTo(mockObjectReturnValueName, literalProperty));
31-
// }
32-
// }
33-
3443
if (properties.lazy.length) {
3544
const addPropertiesToUniqueVariable: ts.ExpressionStatement = AssignPropertiesTo(properties.lazy, mockObjectReturnValueName);
3645
statements.push(addPropertiesToUniqueVariable);
@@ -47,15 +56,15 @@ export function GetMockCall(properties: PropertyAssignments, signatures: MethodS
4756
return ts.createCall(IFFEFunction, [], []);
4857
}
4958

50-
// function AssignVariableTo(variable: ts.Expression, expression: ts.Expression): ts.ExpressionStatement {
51-
// const binaryExpression: ts.BinaryExpression = ts.createBinary(variable, ts.SyntaxKind.EqualsToken, expression);
52-
// return ts.createExpressionStatement(binaryExpression);
53-
// }
59+
function AssignVariableTo(variable: ts.Expression, expression: ts.Expression): ts.ExpressionStatement {
60+
const binaryExpression: ts.BinaryExpression = ts.createBinary(variable, ts.SyntaxKind.EqualsToken, expression);
61+
return ts.createExpressionStatement(binaryExpression);
62+
}
5463

55-
// function AssignLiteralPropertyTo(mockObjectReturnValueName: ts.Identifier, literalProperty: ts.PropertyAssignment): ts.ExpressionStatement {
56-
// const propertyAccess: ts.ElementAccessExpression = ts.createElementAccess(mockObjectReturnValueName, literalProperty.name as ts.StringLiteral);
57-
// return AssignVariableTo(propertyAccess, literalProperty.initializer);
58-
// }
64+
function AssignLiteralPropertyTo(mockObjectReturnValueName: ts.Identifier, literalProperty: ts.PropertyAssignment): ts.ExpressionStatement {
65+
const propertyAccess: ts.ElementAccessExpression = ts.createElementAccess(mockObjectReturnValueName, literalProperty.name as ts.StringLiteral);
66+
return AssignVariableTo(propertyAccess, literalProperty.initializer);
67+
}
5968

6069
function AssignMockMarkerPropertyTo(identifier: ts.Identifier): ts.ExpressionStatement {
6170
const mockMarkerProperty: Property = GetMockMarkerProperty();

0 commit comments

Comments
 (0)