Skip to content

Commit

Permalink
Fixed bug #80 that instance function worked different if Proxy was su…
Browse files Browse the repository at this point in the history
…pported or not

With Proxy support instance function always returned new object, without support it was always same instance. This changed fixes this issue and instance function now always returns same object.
  • Loading branch information
NagRock committed Jan 23, 2018
1 parent e48a85d commit a68c61d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-mockito",
"version": "2.2.7",
"version": "2.2.8",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",
"typings": "lib/ts-mockito",
Expand Down
4 changes: 4 additions & 0 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class Mocker {
private objectPropertyCodeRetriever = new ObjectPropertyCodeRetriever();

constructor(private clazz: any, protected instance: any = {}) {
if (typeof Proxy !== "undefined") {
this.instance = new Proxy(this.instance, this.createCatchAllHandlerForRemainingPropertiesWithoutGetters());
}

this.mock.__tsmockitoInstance = this.instance;
this.mock.__tsmockitoMocker = this;
if (_.isObject(this.clazz) && _.isObject(this.instance)) {
Expand Down
7 changes: 1 addition & 6 deletions src/ts-mockito.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ export function when<T>(method: T): MethodStubSetter<T> {

export function instance<T>(mockedValue: T): T {
const tsmockitoInstance = (mockedValue as any).__tsmockitoInstance as T;
if (typeof Proxy === "undefined") {
return tsmockitoInstance;
}

const tsmockitoMocker = (mockedValue as any).__tsmockitoMocker as Mocker;
return new Proxy(tsmockitoInstance as any, tsmockitoMocker.createCatchAllHandlerForRemainingPropertiesWithoutGetters());
return tsmockitoInstance;
}

export function capture<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>(method: (a: T0, b: T1, c: T2, d: T3, e: T4, f: T5, g: T6, h: T7, i: T8, j: T9) => any): ArgCaptor10<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>;
Expand Down
20 changes: 20 additions & 0 deletions test/instance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {instance, mock} from "../src/ts-mockito";
import {Foo} from "./utils/Foo";

describe("instance", () => {
describe("getting instance of mock", () => {
let mockedFoo: Foo;

it("returns always same instance", () => {
// given
mockedFoo = mock(Foo);

// when
let firstFooInstance = instance(mockedFoo);
let secondFooInstance = instance(mockedFoo);

// then
expect(firstFooInstance).toBe(secondFooInstance);
});
});
});

0 comments on commit a68c61d

Please sign in to comment.