From 6cc775b8c28bf3f9745590148db548eeca5dfd03 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 30 Aug 2023 17:27:04 +0200 Subject: [PATCH] backend-test-utils: some ServiceFactoryTester tweaks Signed-off-by: Patrik Oldsberg --- .../httpRouterServiceFactory.test.ts | 2 +- .../next/wiring/ServiceFactoryTester.test.ts | 8 +++--- .../src/next/wiring/ServiceFactoryTester.ts | 26 +++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/backend-app-api/src/services/implementations/httpRouter/httpRouterServiceFactory.test.ts b/packages/backend-app-api/src/services/implementations/httpRouter/httpRouterServiceFactory.test.ts index ff25be98a6..a6c586d806 100644 --- a/packages/backend-app-api/src/services/implementations/httpRouter/httpRouterServiceFactory.test.ts +++ b/packages/backend-app-api/src/services/implementations/httpRouter/httpRouterServiceFactory.test.ts @@ -23,7 +23,7 @@ import { httpRouterServiceFactory } from './httpRouterServiceFactory'; describe('httpRouterFactory', () => { it('should register plugin paths', async () => { const rootHttpRouter = mockServices.rootHttpRouter.mock(); - const tester = ServiceFactoryTester.from(httpRouterServiceFactory(), { + const tester = ServiceFactoryTester.from(httpRouterServiceFactory, { dependencies: [rootHttpRouter.factory], }); diff --git a/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.test.ts b/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.test.ts index 073b454ad0..14dd427ecf 100644 --- a/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.test.ts +++ b/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.test.ts @@ -54,13 +54,13 @@ const sharedPluginFactory = createServiceFactory({ describe('ServiceFactoryTester', () => { it('should test a root service factory', async () => { - const tester = ServiceFactoryTester.from(rootFactory()); + const tester = ServiceFactoryTester.from(rootFactory); await expect(tester.get()).resolves.toBe('root'); }); it('should test a plugin service factory', async () => { - const tester = ServiceFactoryTester.from(pluginFactory()); + const tester = ServiceFactoryTester.from(pluginFactory); await expect(tester.get('x')).resolves.toBe('x-plugin'); await expect(tester.get('y')).resolves.toBe('y-plugin'); @@ -68,7 +68,7 @@ describe('ServiceFactoryTester', () => { }); it('should test a plugin service factory with root context', async () => { - const tester = ServiceFactoryTester.from(sharedPluginFactory()); + const tester = ServiceFactoryTester.from(sharedPluginFactory); await expect(tester.get('x')).resolves.toBe('x-1-plugin'); await expect(tester.get('y')).resolves.toBe('y-2-plugin'); @@ -76,7 +76,7 @@ describe('ServiceFactoryTester', () => { await expect(tester.get('y')).resolves.toBe('y-2-plugin'); await expect(tester.get('z')).resolves.toBe('z-3-plugin'); - const tester2 = ServiceFactoryTester.from(sharedPluginFactory()); + const tester2 = ServiceFactoryTester.from(sharedPluginFactory); await expect(tester2.get('z')).resolves.toBe('z-1-plugin'); await expect(tester2.get('y')).resolves.toBe('y-2-plugin'); diff --git a/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.ts b/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.ts index b4a038d001..d583614598 100644 --- a/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.ts +++ b/packages/backend-test-utils/src/next/wiring/ServiceFactoryTester.ts @@ -60,23 +60,23 @@ export class ServiceFactoryTester { | (() => ServiceFactory), options?: ServiceFactoryTesterOptions, ) { - return new ServiceFactoryTester( - typeof subject === 'function' ? subject() : subject, - options?.dependencies, - ); + const subjectFactory = typeof subject === 'function' ? subject() : subject; + const registry = new ServiceRegistry([ + ...defaultServiceFactories, + ...(options?.dependencies?.map(f => + typeof f === 'function' ? f() : f, + ) ?? []), + subjectFactory, + ]); + return new ServiceFactoryTester(subjectFactory.service, registry); } private constructor( - subject: ServiceFactory, - dependencies?: Array ServiceFactory)>, + subject: ServiceRef, + registry: ServiceRegistry, ) { - this.#subject = subject.service; - - this.#registry = new ServiceRegistry([ - ...defaultServiceFactories, - ...(dependencies?.map(f => (typeof f === 'function' ? f() : f)) ?? []), - subject, - ]); + this.#subject = subject; + this.#registry = registry; } /**