From 3fc01df11357b1656fcb6b0e8341a88938f566d4 Mon Sep 17 00:00:00 2001 From: OZGCloud <ozgcloud@mgm-tp.com> Date: Tue, 17 Dec 2024 16:33:55 +0100 Subject: [PATCH] OZG-3563 fix tests and new mocking function --- .../libs/test-utils/src/lib/mocking.spec.ts | 26 +++++++++++++++++++ .../libs/test-utils/src/lib/mocking.ts | 10 ------- 2 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 alfa-client/libs/test-utils/src/lib/mocking.spec.ts diff --git a/alfa-client/libs/test-utils/src/lib/mocking.spec.ts b/alfa-client/libs/test-utils/src/lib/mocking.spec.ts new file mode 100644 index 0000000000..f8367a68aa --- /dev/null +++ b/alfa-client/libs/test-utils/src/lib/mocking.spec.ts @@ -0,0 +1,26 @@ +import { Mock, mock } from '@alfa-client/test-utils'; + +describe('mock', () => { + class TestService { + methodTestService() {} + } + + class ExtendedService extends TestService { + prop: string = 'test'; + methodExtendedService() {} + } + + const mockedService: Mock<ExtendedService> = mock(ExtendedService); + + it('should mock methods of a class', () => { + expect(mockedService.methodTestService).toBeDefined(); + }); + + it('should mock methods of inherited class', () => { + expect(mockedService.methodExtendedService).toBeDefined(); + }); + + it('should not mock non-function properties', () => { + expect(mockedService.prop).toBeUndefined(); + }); +}); diff --git a/alfa-client/libs/test-utils/src/lib/mocking.ts b/alfa-client/libs/test-utils/src/lib/mocking.ts index a719f7c6b5..3a382bde3f 100644 --- a/alfa-client/libs/test-utils/src/lib/mocking.ts +++ b/alfa-client/libs/test-utils/src/lib/mocking.ts @@ -47,16 +47,6 @@ export function mock<T>(service: Type<T>): Mock<T> { return mockValue; } -export function mock2<T>(service: Type<T>): Mock<T> { - return <Mock<T>>Object.getOwnPropertyNames(service.prototype).reduce( - (mockValue, key) => ({ - ...mockValue, - [key]: jest.fn(), - }), - <any>{}, - ); -} - function isFunctionDescriptor(prototype: object) { return (key: string) => { const descriptor: PropertyDescriptor = Object.getOwnPropertyDescriptor(prototype, key); -- GitLab