Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed problem with a mock resolving a mocked value with Promises #5

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Mocker {
private mockableFunctionsFinder = new MockableFunctionsFinder();
private objectPropertyCodeRetriever = new ObjectPropertyCodeRetriever();
private excludedPropertyNames: string[] = ["hasOwnProperty"];
private defaultedPropertyNames: string[] = ["Symbol(Symbol.toPrimitive)", "then", "catch"];

constructor(private clazz: any, public instance: any = {}) {
this.mock.__tsmockitoInstance = this.instance;
Expand All @@ -39,6 +40,9 @@ export class Mocker {
const hasMethodStub = name in target;

if (!hasMethodStub) {
if (this.defaultedPropertyNames.indexOf(name.toString()) >= 0) {
return undefined;
}
return this.createActionListener(name.toString());
}
return target[name];
Expand Down Expand Up @@ -77,6 +81,9 @@ export class Mocker {
get: (target: any, name: PropertyKey) => {
const hasMethodStub = name in target;
if (!hasMethodStub) {
if (this.defaultedPropertyNames.indexOf(name.toString()) >= 0) {
return undefined;
}
this.createPropertyStub(name.toString());
this.createInstancePropertyDescriptorListener(name.toString(), {}, this.clazz.prototype);
}
Expand Down
71 changes: 59 additions & 12 deletions test/interface.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
import { instance, mock, when } from "../src/ts-mockito";
import { FooInterface } from "./utils/FooInterface";
import { ThenableInterface } from "./utils/Thenable";

describe("Interface", () => {
let mockedFoo: FooInterface;
let foo: FooInterface;

if (typeof Proxy === "undefined") {
pending("Testing browser doesn't support Proxy.");
}

beforeEach(() => {
mockedFoo = mock<FooInterface>();
foo = instance(mockedFoo);
describe("Basic", () => {
let mockedFoo: FooInterface;
let foo: FooInterface;

beforeEach(() => {
mockedFoo = mock<FooInterface>();
foo = instance(mockedFoo);
});

it("should mock interface function", () => {
// Rest cases for interfaces are tested in verification.spec.ts

// when
when(mockedFoo.sumByInterface(2, 3)).thenReturn(1000);

// then
expect(foo.sumByInterface(2, 3)).toEqual(1000);
});
});
describe("Promise", () => {
let mockedThen: ThenableInterface;
let inst: ThenableInterface;

beforeEach(() => {
mockedThen = mock<ThenableInterface>();
inst = instance(mockedThen);
});

it("does not create then descriptor for interface", () => {
// given
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given + when comments are redundant


// when

// then
expect(inst.then).toBeUndefined();
});
it("creates then descriptor for interface", () => {
// given

// when
when(mockedThen.then()).thenReturn("woof");

// then
expect(inst.then).toBeDefined();
});
it("does not create catch descriptor for interface", () => {
// given

// when

it("should mock interface function", () => {
// Rest cases for interfaces are tested in verification.spec.ts
// then
expect(inst.catch).toBeUndefined();
});
it("creates catch descriptor for interface", () => {
// given

// when
when(mockedFoo.sumByInterface(2, 3)).thenReturn(1000);
// when
when(mockedThen.catch()).thenReturn("woof");

// then
expect(foo.sumByInterface(2, 3)).toEqual(1000);
// then
expect(inst.catch).toBeDefined();
});
});
});
66 changes: 63 additions & 3 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {MethodToStub} from "../src/MethodToStub";
import {instance, mock, when} from "../src/ts-mockito";
import {Bar} from "./utils/Bar";
import { MethodToStub } from "../src/MethodToStub";
import { instance, mock, when } from "../src/ts-mockito";
import { Bar } from "./utils/Bar";
import { ThenableClass } from "./utils/Thenable";

describe("mocking", () => {
describe("mocking abstract class", () => {
Expand Down Expand Up @@ -63,6 +64,65 @@ describe("mocking", () => {
expect(foo.sampleString).toBe("42");
});
});
describe("mocking promise methods", () => {
it("does not create then descriptor for class", () => {
// given
const mocked = mock(SampleAbstractClass);
const inst = instance(mocked);

// when

// then
expect((inst as any).then).toBeUndefined();
});

it("does create then descriptor", () => {
// given
const mocked = mock(ThenableClass);
const inst = instance(mocked);

// when
when(mocked.then()).thenReturn("42");

// then
expect(inst.then()).toEqual("42");
});

it("does not create catch descriptor", () => {
// given
const mocked = mock(SampleAbstractClass);
const inst = instance(mocked);

// when

// then
expect((inst as any).catch).toBeUndefined();
});

it("does create catch descriptor", () => {
// given
const mocked = mock(ThenableClass);
const inst = instance(mocked);

// when
when(mocked.catch()).thenReturn("42");

// then
expect(inst.catch()).toEqual("42");
});

it("default object formatting works", () => {
// given
const mocked = mock(ThenableClass);
const inst = instance(mocked);

// when

// then
const str = `"${inst}"`;
expect(str).toEqual('"[object Object]"');
});
});

describe("mocking class with hasOwnProperty", () => {
let mockedFoo: SampleClassWithHasOwnProperty;
Expand Down
15 changes: 15 additions & 0 deletions test/utils/Thenable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export abstract class ThenableClass {
public then(): string {
return "bob";
}

public catch(): string {
return "bob";
}
}

export interface ThenableInterface {
then(): string;

catch(): string;
}