chore: npm run format
This commit is contained in:
@@ -5,103 +5,102 @@ describe('ServiceCollection', () => {
|
||||
it('should add a singleton service', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addSingleton(serviceIdentifier, TestService);
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
const serviceInstance1 = provider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = provider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
it('should add a scoped service', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addScoped(serviceIdentifier, TestService);
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
const scope1 = provider.createScope();
|
||||
const scope2 = provider.createScope();
|
||||
|
||||
|
||||
const serviceInstance1 = scope1.serviceProvider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = scope2.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).not.toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
it('should add a transient service', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addTransient(serviceIdentifier, TestService);
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
|
||||
|
||||
const serviceInstance1 = provider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = provider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).not.toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
it('should add a single service with a factory', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addSingleton(serviceIdentifier, () => new TestService());
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
const serviceInstance1 = provider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = provider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
it('should add a scoped service with a factory', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addScoped(serviceIdentifier, () => new TestService());
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
const scope1 = provider.createScope();
|
||||
const scope2 = provider.createScope();
|
||||
|
||||
|
||||
const serviceInstance1 = scope1.serviceProvider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = scope2.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).not.toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
|
||||
it('should add a transient service with a factory', () => {
|
||||
class TestService {}
|
||||
const serviceIdentifier = createServiceIdentifier();
|
||||
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addTransient(serviceIdentifier, () => new TestService());
|
||||
|
||||
|
||||
const provider = collection.build();
|
||||
|
||||
|
||||
const serviceInstance1 = provider.getService(serviceIdentifier);
|
||||
const serviceInstance2 = provider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
expect(serviceInstance1).not.toBe(serviceInstance2);
|
||||
});
|
||||
|
||||
it('should add a service with a factory and dependencies', () => {
|
||||
type IDependencyService = object
|
||||
type IDependencyService = object;
|
||||
class DependencyService implements IDependencyService {}
|
||||
|
||||
type ITestService = {
|
||||
dependency: IDependencyService;
|
||||
}
|
||||
};
|
||||
class TestService implements ITestService {
|
||||
constructor(public dependency: DependencyService) {}
|
||||
}
|
||||
@@ -111,12 +110,15 @@ describe('ServiceCollection', () => {
|
||||
|
||||
const collection = new ServiceCollection();
|
||||
collection.addSingleton(dependencyServiceIdentifier, DependencyService);
|
||||
collection.addSingleton(testServiceIdentifier, (provider) => new TestService(provider.getService(dependencyServiceIdentifier)));
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
@@ -247,7 +247,7 @@ describe('ServiceProvider', () => {
|
||||
);
|
||||
|
||||
const userService = serviceProvider.getService(userServiceIdentifier);
|
||||
|
||||
|
||||
const user = userService.getUser();
|
||||
|
||||
expect(user).toBe('John Doe');
|
||||
@@ -255,9 +255,9 @@ describe('ServiceProvider', () => {
|
||||
|
||||
it('should throw an error when service has invalid lifetime', () => {
|
||||
type IService = object;
|
||||
|
||||
|
||||
const serviceIdentifier = createServiceIdentifier<IService>();
|
||||
|
||||
|
||||
const serviceProvider = new ServiceProvider(
|
||||
new Map([
|
||||
[
|
||||
@@ -341,9 +341,9 @@ describe('ServiceProvider', () => {
|
||||
);
|
||||
|
||||
const initialService = serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
serviceProvider.dispose();
|
||||
|
||||
|
||||
const newService = serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
expect(initialService).not.toBe(newService);
|
||||
@@ -380,9 +380,9 @@ describe('ServiceProvider', () => {
|
||||
const scope = serviceProvider.createScope();
|
||||
|
||||
const initialService = scope.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
scope.dispose();
|
||||
|
||||
|
||||
const newService = scope.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
expect(initialService).not.toBe(newService);
|
||||
@@ -419,15 +419,15 @@ describe('ServiceProvider', () => {
|
||||
const scope = serviceProvider.createScope();
|
||||
|
||||
const initialService = scope.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
serviceProvider.dispose();
|
||||
|
||||
|
||||
const newService = scope.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
expect(initialService).toBe(newService);
|
||||
});
|
||||
|
||||
it("should not clear the scoped instances on dispose from another scope when one scope is disposed", () => {
|
||||
it('should not clear the scoped instances on dispose from another scope when one scope is disposed', () => {
|
||||
type IService = {
|
||||
id: string;
|
||||
};
|
||||
@@ -460,9 +460,9 @@ describe('ServiceProvider', () => {
|
||||
|
||||
const initialService1 = scope1.serviceProvider.getService(serviceIdentifier);
|
||||
const initialService2 = scope2.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
|
||||
scope1.dispose();
|
||||
|
||||
|
||||
const newService1 = scope1.serviceProvider.getService(serviceIdentifier);
|
||||
const newService2 = scope2.serviceProvider.getService(serviceIdentifier);
|
||||
|
||||
@@ -501,7 +501,7 @@ describe('ServiceProvider', () => {
|
||||
],
|
||||
]),
|
||||
);
|
||||
|
||||
|
||||
expect(() => {
|
||||
serviceProvider.getService(userServiceIdentifier);
|
||||
}).toThrowError();
|
||||
|
||||
Reference in New Issue
Block a user