From f379b36e6174848d769b9344a2b0fe23d005dd42 Mon Sep 17 00:00:00 2001 From: Giles Roadnight <10414642+Roaders@users.noreply.github.com> Date: Thu, 19 May 2022 11:03:22 +0100 Subject: [PATCH] Fix toString bug --- main/mock/operators.ts | 10 +++++++--- spec/mock/mock-static.spec.ts | 13 +++++++++++++ spec/mock/mock.spec.ts | 7 +++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/main/mock/operators.ts b/main/mock/operators.ts index 928ae91..6446e5c 100644 --- a/main/mock/operators.ts +++ b/main/mock/operators.ts @@ -43,7 +43,10 @@ export function setupFunction, 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] = []; @@ -53,7 +56,7 @@ export function setupFunction, 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. * @@ -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] = []; diff --git a/spec/mock/mock-static.spec.ts b/spec/mock/mock-static.spec.ts index da88e43..6a94a28 100644 --- a/spec/mock/mock-static.spec.ts +++ b/spec/mock/mock-static.spec.ts @@ -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().setup( + setupStaticFunction('toString', () => mockedValue), + ); + + expect(toStringMock.mockConstructor.toString()).toEqual(mockedValue); + }); }); }); @@ -1456,6 +1465,10 @@ class SampleMockedClass { constructor(_paramsOne: {}, _paramTwo: Date) {} + public static toString(): string { + return 'original toString'; + } + public static functionWithNoParamsAndNoReturn(): void {} public functionWithNoParamsAndNoReturn(): void {} diff --git a/spec/mock/mock.spec.ts b/spec/mock/mock.spec.ts index 1a1290d..a2d4a39 100644 --- a/spec/mock/mock.spec.ts +++ b/spec/mock/mock.spec.ts @@ -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().setup(setupFunction('toString', () => mockedValue)); + + expect(toStringMock.mock.toString()).toEqual(mockedValue); + }); }); describe('parameter comparison', () => {