backend-test-utils: some ServiceFactoryTester tweaks

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-08-30 17:27:04 +02:00
parent 187cc4680e
commit 6cc775b8c2
3 changed files with 18 additions and 18 deletions
@@ -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],
});
@@ -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');
@@ -60,23 +60,23 @@ export class ServiceFactoryTester<TService, TScope extends 'root' | 'plugin'> {
| (() => ServiceFactory<TService, TScope>),
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<TService, TScope>,
dependencies?: Array<ServiceFactory | (() => ServiceFactory)>,
subject: ServiceRef<TService, TScope>,
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;
}
/**