Skip to content

Commit

Permalink
fix(enum): fix enum constant computed properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Pmyl committed Sep 13, 2020
1 parent 27bc7c9 commit 9c96a53
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test:features": "cross-env JASMINE_CONFIG=./test/features/jasmine.json TSCONFIG=./test/features/tsconfig.json npm run test:common",
"test:logs": "cross-env JASMINE_CONFIG=./test/logs/jasmine.json TSCONFIG=./test/logs/tsconfig.json npm run test:common",
"test:playground": "cross-env JASMINE_CONFIG=./test/playground/jasmine.json TSCONFIG=./test/playground/tsconfig.json npm run test:common",
"test:playground:build": "cross-env JASMINE_CONFIG=./test/playground/jasmine.build.json TSCONFIG=./test/playground/tsconfig.json npm run test:common",
"test:common": "cross-var ts-node --files -r tsconfig-paths/register --compiler ttypescript --project $TSCONFIG node_modules/jasmine/bin/jasmine --config=$JASMINE_CONFIG",
"dist:collect": "cp -r package.json package-lock.json README.md dist",
"ts-check:src": "tsc --noEmit",
Expand Down
26 changes: 11 additions & 15 deletions src/transformer/descriptor/enum/enumDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,11 @@ export function GetEnumDeclarationDescriptor(
node: ts.EnumDeclaration
): ts.Expression {
const typeChecker: ts.TypeChecker = TypeChecker();
const typesList: ts.LiteralType[] = node.members.map((it: ts.EnumMember) =>
typeChecker.getTypeAtLocation(it)
) as ts.LiteralType[];

if (IsTsAutoMockRandomEnabled()) {
const nodesList: ts.Expression[] = typesList.map(
(type: ts.LiteralType, index: number) => {
if (type.hasOwnProperty('value')) {
return ts.createLiteral(type.value);
}

return ts.createLiteral(index);
}
const nodesList: ts.Expression[] = node.members.map(
(member: ts.EnumMember, index: number) =>
getEnumMemberValue(typeChecker, member, index)
);

return ts.createCall(
Expand All @@ -29,9 +21,13 @@ export function GetEnumDeclarationDescriptor(
);
}

if (typesList[0].hasOwnProperty('value')) {
return ts.createLiteral(typesList[0].value);
}
return getEnumMemberValue(typeChecker, node.members[0]);
}

return ts.createLiteral(0);
function getEnumMemberValue(
typeChecker: ts.TypeChecker,
member: ts.EnumMember,
defaultValue: string | number = 0
): ts.Expression {
return ts.createLiteral(typeChecker.getConstantValue(member) || defaultValue);
}
47 changes: 41 additions & 6 deletions test/features/random/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,50 @@ describe('Random enum', () => {
expect(thirdMock.direction).toBe(Direction.None);
});

it('should have position of "computed" property, because transformer don\'t support computed Enum values', () => {
it('should get the correct const enum member computed property', () => {
const enum WithComputed {
Computed = 1 + 4,
}

interface IWithComputed {
computed: WithComputed;
}

const spy: jasmine.Spy = spyOn(Math, 'floor');

spy.and.returnValue(0);
const mock1: IWithComputed = createMock<IWithComputed>();

expect(mock1.computed).toBe(5);
});

it('should get the correct enum member computed property for constant expression', () => {
enum WithComputed {
Computed = 1 + 4,
}

interface IWithComputed {
computed: WithComputed;
}

const spy: jasmine.Spy = spyOn(Math, 'floor');

spy.and.returnValue(0);
const mock1: IWithComputed = createMock<IWithComputed>();

expect(mock1.computed).toBe(5);
});

it('should have position of non constant "computed" property, because transformer don\'t support non constant computed Enum values', () => {
// Issue https://github.com/Typescript-TDD/ts-auto-mock/issues/339
function getComputed(): number {
return 12;
}

enum WithComputed {
NonComputed,
Computed = getComputed(),
Computed2 = 1 + 4,
Computed1 = getComputed(),
}

interface IWithComputed {
Expand All @@ -113,14 +148,14 @@ describe('Random enum', () => {

const spy: jasmine.Spy = spyOn(Math, 'floor');

spy.and.returnValue(0);
spy.and.returnValue(1);
const mock1: IWithComputed = createMock<IWithComputed>();

expect(mock1.computed).toBe(0);
expect(mock1.computed).toBe(1);

spy.and.returnValue(1);
spy.and.returnValue(2);
const mock2: IWithComputed = createMock<IWithComputed>();

expect(mock2.computed).toBe(1);
expect(mock2.computed).toBe(2);
});
});
5 changes: 5 additions & 0 deletions test/playground/jasmine.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"spec_dir": "./test/playground",
"spec_files": ["**/*test.js"],
"helpers": ["../reporter.js"]
}

0 comments on commit 9c96a53

Please sign in to comment.