Skip to content

Commit

Permalink
Fix problem with mock instances looking like promises
Browse files Browse the repository at this point in the history
  • Loading branch information
johanblumenberg committed May 22, 2018
1 parent 7e71c4b commit a520724
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ export class Mocker {
this.processFunctionsCode(this.clazz.prototype);
}
if (typeof Proxy !== "undefined") {
this.mock.__tsmockitoInstance = new Proxy(this.instance, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters());
this.mock.__tsmockitoInstance = new Proxy(this.instance, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters("instance"));
}
}

public getMock(): any {
if (typeof Proxy === "undefined") {
return this.mock;
}
return new Proxy(this.mock, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters());
return new Proxy(this.mock, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters("expectation"));
}

public createCatchAllHandlerForRemainingPropertiesWithoutGetters(): any {
public createCatchAllHandlerForRemainingPropertiesWithoutGetters(origin: "instance" | "expectation"): any {
return {
get: (target: any, name: PropertyKey) => {
const hasMethodStub = name in target;
if (!hasMethodStub) {
if (this.mock.__tsmockitoInterface) {
this.createMethodStub(name.toString());
this.createInstanceActionListener(name.toString(), {});
if (origin !== "instance" || name !== "then") {
// Don't make this mock object instance look like a Promise instance by mistake, if someone is checking
this.createMethodStub(name.toString());
this.createInstanceActionListener(name.toString(), {});
}
} else {
this.createPropertyStub(name.toString());
this.createInstancePropertyDescriptorListener(name.toString(), {}, this.clazz.prototype);
Expand Down
21 changes: 20 additions & 1 deletion test/stubbing.method.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {anything, instance, mock, when} from "../src/ts-mockito";
import {anything, imock, instance, mock, when} from "../src/ts-mockito";
import {Foo} from "./utils/Foo";

describe("mocking", () => {
Expand Down Expand Up @@ -213,6 +213,25 @@ describe("mocking", () => {
})
.catch(err => done.fail(err));
});

if (typeof Proxy !== "undefined") {
it("resolves with given mock value", done => {
// given
const sampleValue = "abc";
const expectedResult: Foo = imock();

when(mockedFoo.sampleMethodReturningObjectPromise(sampleValue)).thenResolve(instance(expectedResult));

// when
foo.sampleMethodReturningObjectPromise(sampleValue)
.then(value => {
// then
expect(value).toEqual(instance(expectedResult));
done();
})
.catch(err => done.fail(err));
});
}
});

describe("with stubbed promise rejection", () => {
Expand Down
4 changes: 4 additions & 0 deletions test/utils/Foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ export class Foo {
public sampleMethodReturningVoidPromise(value: string): Promise<void> {
return Promise.resolve();
}

public sampleMethodReturningObjectPromise(value: string): Promise<Foo> {
return Promise.resolve(this);
}
}

0 comments on commit a520724

Please sign in to comment.