Skip to content

Commit

Permalink
Fix toString bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Roaders committed May 19, 2022
1 parent a5b0fcf commit f379b36
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions main/mock/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export function setupFunction<T, C extends ConstructorFunction<T>, U extends Fun
mocked.functionReplacementLookup['function'] || {});
functionLookup[functionName as string] = mockFunction;

if (mocked.mock[functionName] == null) {
// we do not replace an existing function in case it has already been destructured and sut already has a reference to it
// we do replace the mocked implementation above though
// eslint-disable-next-line @typescript-eslint/ban-types
if ((mocked.mock[functionName] as Function)?.name != functionReplacement.name) {
mocked.mock[functionName] = functionReplacement as any;
}
mocked.functionCallLookup[functionName] = [];
Expand All @@ -53,7 +56,7 @@ export function setupFunction<T, C extends ConstructorFunction<T>, U extends Fun
}

/**
* Mocks a staticfunction on an existing Mock.
* Mocks a static function on an existing Mock.
* Allows function call verification to be performed later in the test.
* You can optionally set a mock function implementation that will be called.
*
Expand Down Expand Up @@ -83,7 +86,8 @@ export function setupStaticFunction<
mocked.functionReplacementLookup['staticFunction'] || {});
staticFunctionLookup[functionName as string] = mockFunction;

if (mocked.mockConstructor[functionName] == null) {
// eslint-disable-next-line @typescript-eslint/ban-types
if ((mocked.mockConstructor[functionName] as Function)?.name != functionReplacement.name) {
mocked.mockConstructor[functionName] = functionReplacement as any;
}
mocked.staticFunctionCallLookup[functionName] = [];
Expand Down
13 changes: 13 additions & 0 deletions spec/mock/mock-static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,15 @@ describe('mock with statics', () => {
expect(newMock.mockConstructor.functionWithParamsAndNoReturn('', 123, true)).toBeUndefined();
expect(newMock.mockConstructor.functionWithParamsAndReturn('')).toBeUndefined();
});

it(`should work with predefined function toString`, () => {
const mockedValue = 'mocked toString return value';
const toStringMock = Mock.create<SampleMockedClass, typeof SampleMockedClass>().setup(
setupStaticFunction('toString', () => mockedValue),
);

expect(toStringMock.mockConstructor.toString()).toEqual(mockedValue);
});
});
});

Expand All @@ -1456,6 +1465,10 @@ class SampleMockedClass {

constructor(_paramsOne: {}, _paramTwo: Date) {}

public static toString(): string {
return 'original toString';
}

public static functionWithNoParamsAndNoReturn(): void {}

public functionWithNoParamsAndNoReturn(): void {}
Expand Down
7 changes: 7 additions & 0 deletions spec/mock/mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,13 @@ describe('mock', () => {
expect(newMock.functionWithParamsAndNoReturn('', 123, true)).toBeUndefined();
expect(newMock.functionWithParamsAndReturn('')).toBeUndefined();
});

it(`should work with predefined function toString`, () => {
const mockedValue = 'mocked toString return value';
const toStringMock = Mock.create<Buffer>().setup(setupFunction('toString', () => mockedValue));

expect(toStringMock.mock.toString()).toEqual(mockedValue);
});
});

describe('parameter comparison', () => {
Expand Down

0 comments on commit f379b36

Please sign in to comment.