2025-05-12 12:09:09 -05:00
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
import { createServiceIdentifier, ServiceCollection } from '../src';
|
|
|
|
|
|
|
|
|
|
describe('ServiceCollection', () => {
|
|
|
|
|
it('should add a singleton service', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addSingleton(serviceIdentifier, TestService);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
|
|
|
|
const serviceInstance1 = provider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = provider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).toBe(serviceInstance2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a scoped service', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addScoped(serviceIdentifier, TestService);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
|
|
|
|
const scope1 = provider.createScope();
|
|
|
|
|
const scope2 = provider.createScope();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const serviceInstance1 = scope1.serviceProvider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = scope2.serviceProvider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).not.toBe(serviceInstance2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a transient service', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addTransient(serviceIdentifier, TestService);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const serviceInstance1 = provider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = provider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).not.toBe(serviceInstance2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a single service with a factory', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addSingleton(serviceIdentifier, () => new TestService());
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
|
|
|
|
const serviceInstance1 = provider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = provider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).toBe(serviceInstance2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a scoped service with a factory', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addScoped(serviceIdentifier, () => new TestService());
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
|
|
|
|
const scope1 = provider.createScope();
|
|
|
|
|
const scope2 = provider.createScope();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const serviceInstance1 = scope1.serviceProvider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = scope2.serviceProvider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).not.toBe(serviceInstance2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should add a transient service with a factory', () => {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const serviceIdentifier = createServiceIdentifier();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addTransient(serviceIdentifier, () => new TestService());
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const provider = collection.build();
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
const serviceInstance1 = provider.getService(serviceIdentifier);
|
|
|
|
|
const serviceInstance2 = provider.getService(serviceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 12:09:09 -05:00
|
|
|
expect(serviceInstance1).not.toBe(serviceInstance2);
|
|
|
|
|
});
|
2025-05-12 21:54:40 -05:00
|
|
|
|
|
|
|
|
it('should add a service with a factory and dependencies', () => {
|
2025-05-12 22:18:55 -05:00
|
|
|
type IDependencyService = object;
|
2025-05-12 21:54:40 -05:00
|
|
|
class DependencyService implements IDependencyService {}
|
|
|
|
|
|
|
|
|
|
type ITestService = {
|
|
|
|
|
dependency: IDependencyService;
|
2025-05-12 22:18:55 -05:00
|
|
|
};
|
2025-05-12 21:54:40 -05:00
|
|
|
class TestService implements ITestService {
|
|
|
|
|
constructor(public dependency: DependencyService) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testServiceIdentifier = createServiceIdentifier<ITestService>();
|
|
|
|
|
const dependencyServiceIdentifier = createServiceIdentifier<IDependencyService>();
|
|
|
|
|
|
|
|
|
|
const collection = new ServiceCollection();
|
|
|
|
|
collection.addSingleton(dependencyServiceIdentifier, DependencyService);
|
2025-05-12 22:18:55 -05:00
|
|
|
collection.addSingleton(
|
|
|
|
|
testServiceIdentifier,
|
|
|
|
|
provider => new TestService(provider.getService(dependencyServiceIdentifier)),
|
|
|
|
|
);
|
|
|
|
|
|
2025-05-12 21:54:40 -05:00
|
|
|
const provider = collection.build();
|
|
|
|
|
const serviceInstance1 = provider.getService(testServiceIdentifier);
|
|
|
|
|
const serviceInstance2 = provider.getService(testServiceIdentifier);
|
2025-05-12 22:18:55 -05:00
|
|
|
|
2025-05-12 21:54:40 -05:00
|
|
|
expect(serviceInstance1).toBe(serviceInstance2);
|
|
|
|
|
expect(serviceInstance1.dependency).toBe(serviceInstance2.dependency);
|
|
|
|
|
});
|
2025-05-12 12:09:09 -05:00
|
|
|
});
|