Skip to content

Commit 8b9002e

Browse files
author
Brian Ojeda
committed
Add constructor support
1 parent 65ae6ae commit 8b9002e

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

index.d.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ export type DoubledObjectWithKey<Key extends string> = {};
88

99
export type TestDouble<Function> = Function;
1010

11+
export type TestDoubleConstructor<T> = Constructor<T>;
12+
1113
interface Call {
1214
context: {};
1315
args: any[];
1416
}
1517

18+
interface Constructor<T> {
19+
new (...args: any[]): T
20+
}
21+
1622
export interface Captor {
1723
capture(): any;
1824
value?: any;
@@ -94,6 +100,16 @@ export function explain<T>(f: TestDouble<T>): Explanation;
94100
// fake: constructors
95101
// ----------------------------------------------------------------------------
96102

103+
/**
104+
* Create a fake object constructor the given class.
105+
*
106+
* @export
107+
* @template T
108+
* @param {{ new (...args: any[]): T }} constructor
109+
* @returns {DoubledObject<T>}
110+
*/
111+
export function constructor<T>(constructor: Constructor<T>): TestDoubleConstructor<T>;
112+
97113
//
98114
// fake: functions
99115
// ----------------------------------------------------------------------------
@@ -130,7 +146,7 @@ export { functionDouble as func };
130146
* @param {{ new (...args: any[]): T }} constructor
131147
* @returns {DoubledObject<T>}
132148
*/
133-
export function object<T>(constructor: { new (...args: any[]): T }): DoubledObject<T>;
149+
export function object<T>(constructor: Constructor<T> ): DoubledObject<T>;
134150

135151
/**
136152
* Create a fake object that has the given list of properties.

test/src/typescript/test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ td.when(f(td.matchers.not(false))).thenReject(new Error("rejected"));
2323
const fakeSum = td.function(sum);
2424
td.when(fakeSum(1, 2)).thenReturn(3);
2525

26-
const fakerSum = td.function("sum");
27-
td.when(fakerSum(1, 2)).thenReturn(3);
28-
2926
const fakestSum = td.function("sum");
3027
td.when(fakestSum(1, 2)).thenReturn(3);
3128

@@ -40,7 +37,8 @@ td.when(ff(td.matchers.not(false))).thenReject(new Error("rejected"));
4037

4138
// td.constructor()
4239

43-
const dog = td.constructor(Dog);
40+
const DogFake = td.constructor(Dog);
41+
const dog = new DogFake()
4442
td.when(dog.bark()).thenReturn("bark!");
4543

4644
// td.object()

0 commit comments

Comments
 (0)