-
-
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(constructorType): Add constructor type descriptor (#115)
* feat(constructorType): add constructor type descriptor * update documentation adding support to constructor type * add unit test to cover type
- Loading branch information
Showing
8 changed files
with
141 additions
and
15 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const karmaBaseConfig = require('./karma.config.base'); | ||
|
||
module.exports = function(config) { | ||
const karmaConfig = karmaBaseConfig(config, '../../test/playground/**/*.test.js'); | ||
|
||
config.set(karmaConfig); | ||
}; |
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
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,8 @@ | ||
import * as ts from 'typescript'; | ||
import { TypescriptCreator } from '../../helper/creator'; | ||
import { Scope } from '../../scope/scope'; | ||
import { GetDescriptor } from '../descriptor'; | ||
|
||
export function GetConstructorTypeDescriptor(node: ts.ConstructorTypeNode, scope: Scope): ts.Expression { | ||
return TypescriptCreator.createFunctionExpressionReturn(GetDescriptor(node.type, 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
102 changes: 102 additions & 0 deletions
102
test/transformer/descriptor/constructor/constructorType.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,102 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
|
||
describe('constructorType', () => { | ||
describe('of an interface', () => { | ||
it('should create a concrete newable type of the interface', () => { | ||
interface Test { | ||
a: string; | ||
b: number; | ||
} | ||
|
||
const mockConstructorType: new () => Test = createMock<new () => Test>(); | ||
const mockInstance: Test = new mockConstructorType(); | ||
|
||
expect(mockInstance.a).toEqual(''); | ||
expect(mockInstance.b).toEqual(0); | ||
}); | ||
|
||
it('should not create a singleton newable type of the interface', () => { | ||
interface Test { | ||
a: string; | ||
b: number; | ||
} | ||
|
||
const mockConstructorType: new () => Test = createMock<new () => Test>(); | ||
const mockInstance1: Test = new mockConstructorType(); | ||
const mockInstance2: Test = new mockConstructorType(); | ||
const mockInstance3: Test = new mockConstructorType(); | ||
|
||
mockInstance1.a = 'test1'; | ||
mockInstance2.a = 'test2'; | ||
mockInstance3.a = 'test3'; | ||
|
||
expect(mockInstance1.a).toEqual('test1'); | ||
expect(mockInstance2.a).toEqual('test2'); | ||
expect(mockInstance3.a).toEqual('test3'); | ||
}); | ||
}); | ||
|
||
describe('of a class', () => { | ||
it('should create a concrete newable type of the class', () => { | ||
class Test { | ||
a: string; | ||
b: number; | ||
} | ||
|
||
const mockConstructorType: new () => Test = createMock<new () => Test>(); | ||
const mockInstance: Test = new mockConstructorType(); | ||
|
||
expect(mockInstance.a).toEqual(''); | ||
expect(mockInstance.b).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('in a property', () => { | ||
it('should create a concrete newable type in the property', () => { | ||
interface Test { | ||
a: string; | ||
b: number; | ||
c: new () => Test; | ||
} | ||
|
||
const mock: Test = createMock<Test>(); | ||
const instance: Test = new mock.c(); | ||
|
||
expect(instance.a).toEqual(''); | ||
expect(instance.b).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('in a method', () => { | ||
it('should create a concrete newable type as a method returned value', () => { | ||
interface Test { | ||
a: string; | ||
b: number; | ||
c(): new () => Test; | ||
} | ||
|
||
const mock: Test = createMock<Test>(); | ||
const instance: Test = new (mock.c())(); | ||
|
||
expect(instance.a).toEqual(''); | ||
expect(instance.b).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('from a type', () => { | ||
it('should create a concrete newable type', () => { | ||
interface Test { | ||
a: string; | ||
b: number; | ||
} | ||
|
||
type TestType = new () => Test; | ||
|
||
const mockType: TestType = createMock<TestType>(); | ||
const instance: Test = new mockType(); | ||
|
||
expect(instance.a).toEqual(''); | ||
expect(instance.b).toEqual(0); | ||
}); | ||
}); | ||
}); |