From ca991501e2e37360c6eb23a4aed4cce21c1b6546 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 12 May 2025 21:54:40 -0500 Subject: [PATCH] tests: add test --- tests/serviceCollection.spec.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/serviceCollection.spec.ts b/tests/serviceCollection.spec.ts index 85f117a..5918d9c 100644 --- a/tests/serviceCollection.spec.ts +++ b/tests/serviceCollection.spec.ts @@ -94,4 +94,30 @@ describe('ServiceCollection', () => { expect(serviceInstance1).not.toBe(serviceInstance2); }); + + it('should add a service with a factory and dependencies', () => { + type IDependencyService = object + class DependencyService implements IDependencyService {} + + type ITestService = { + dependency: IDependencyService; + } + class TestService implements ITestService { + constructor(public dependency: DependencyService) {} + } + + const testServiceIdentifier = createServiceIdentifier(); + const dependencyServiceIdentifier = createServiceIdentifier(); + + const collection = new ServiceCollection(); + collection.addSingleton(dependencyServiceIdentifier, DependencyService); + collection.addSingleton(testServiceIdentifier, (provider) => new TestService(provider.getService(dependencyServiceIdentifier))); + + const provider = collection.build(); + const serviceInstance1 = provider.getService(testServiceIdentifier); + const serviceInstance2 = provider.getService(testServiceIdentifier); + + expect(serviceInstance1).toBe(serviceInstance2); + expect(serviceInstance1.dependency).toBe(serviceInstance2.dependency); + }); });