-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(getAccessor): correctly mock get accessors for mocked classes
- Loading branch information
Showing
6 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as ts from 'typescript'; | ||
import { Scope } from '../../scope/scope'; | ||
import { GetDescriptor } from '../descriptor'; | ||
import { GetFunctionReturnType } from '../method/functionReturnType'; | ||
import { PropertySignatureCache } from '../property/cache'; | ||
|
||
export function GetGetAccessorDeclarationDescriptor( | ||
node: ts.GetAccessorDeclaration, | ||
scope: Scope | ||
): ts.Expression { | ||
PropertySignatureCache.instance.set(node.name); | ||
const returnTypeNode: ts.Node = GetFunctionReturnType(node); | ||
|
||
return GetDescriptor(returnTypeNode, scope); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/transformer/descriptor/getAccessor/getAccessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('getAccessor', () => { | ||
class AClass { | ||
public get prop1(): number { | ||
return 4; | ||
} | ||
|
||
public get prop2(): string { | ||
return '42'; | ||
} | ||
|
||
public get prop3(): AClass { | ||
return new AClass(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
public get prop4() { | ||
return 123; | ||
} | ||
} | ||
|
||
it('should mock the correct explicit type', () => { | ||
const mock: AClass = createMock<AClass>(); | ||
expect(mock.prop1).toBe(0); | ||
expect(mock.prop2).toBe(''); | ||
expect(mock.prop3.prop3.prop3.prop1).toBe(0); | ||
}); | ||
|
||
it('should mock the correct type inferred from the body', () => { | ||
const mock: AClass = createMock<AClass>(); | ||
expect(mock.prop4).toBe(123); | ||
}); | ||
|
||
describe('when setter is defined', () => { | ||
it('should ignore it', () => { | ||
class AClassWithSetter { | ||
private aValue: string; | ||
|
||
public set a(value: string) { | ||
this.aValue = value; | ||
} | ||
|
||
public get a(): string { | ||
return ''; | ||
} | ||
|
||
public get b(): string { | ||
return ''; | ||
} | ||
|
||
public set b(value: string) { | ||
this.aValue = value; | ||
} | ||
} | ||
|
||
const mock: AClassWithSetter = createMock<AClassWithSetter>(); | ||
|
||
expect(mock.a).toBe(''); | ||
expect(mock.b).toBe(''); | ||
}); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
test/transformer/descriptor/setAccessor/setAccessor.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('setAccessor', () => { | ||
class AClass { | ||
public _prop1: number; | ||
|
||
public set prop1(value: number) { | ||
this._prop1 = value; | ||
} | ||
} | ||
|
||
it('should always be undefined for only setter', () => { | ||
const mock: AClass = createMock<AClass>(); | ||
expect(mock.prop1).toBeUndefined(); | ||
}); | ||
}); |