Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ActivationService with get #163

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,70 @@ describe(ActivationsService.name, () => {
});
});

describe('.get', () => {
let serviceIdFixture: ServiceIdentifier;

beforeAll(() => {
serviceIdFixture = 'service-identifier';
});

describe('when called, and activationMaps.get() returns undefined', () => {
let result: unknown;

beforeAll(() => {
result = activationsService.get(serviceIdFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call activationMaps.get()', () => {
expect(activationMapsMock.get).toHaveBeenCalledTimes(1);
expect(activationMapsMock.get).toHaveBeenCalledWith(
'serviceId',
serviceIdFixture,
);
});

it('should return undefined', () => {
expect(result).toBeUndefined();
});
});

describe('when called, and activationMaps.get() returns Iterable', () => {
let bindingActivationFixture: BindingActivation;

let result: unknown;

beforeAll(() => {
bindingActivationFixture = Symbol() as unknown as BindingActivation;

activationMapsMock.get.mockReturnValueOnce(
[bindingActivationFixture].values(),
);

result = activationsService.get(serviceIdFixture);
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call activationMaps.get()', () => {
expect(activationMapsMock.get).toHaveBeenCalledTimes(1);
expect(activationMapsMock.get).toHaveBeenCalledWith(
'serviceId',
serviceIdFixture,
);
});

it('should return BindingActivation[]', () => {
expect(result).toStrictEqual([bindingActivationFixture]);
});
});
});

describe('.removeAllByModule', () => {
let moduleIdFixture: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ export class ActivationsService {
this.#activationMaps.set(activation, relation);
}

public get(
serviceIdentifier: ServiceIdentifier,
): BindingActivation[] | undefined {
const bindings: Iterable<BindingActivation> | undefined =
this.#activationMaps.get(
ActivationRelationKind.serviceId,
serviceIdentifier,
);

if (bindings === undefined) {
return undefined;
}

return [...bindings];
}

public removeAllByModuleId(moduleId: number): void {
this.#activationMaps.removeByRelation(
ActivationRelationKind.moduleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'reflect-metadata';
import { Newable, Right, ServiceIdentifier } from '@inversifyjs/common';
import { getReflectMetadata } from '@inversifyjs/reflect-metadata-utils';

import { BindingActivation } from '../../binding/models/BindingActivation';
import { bindingScopeValues } from '../../binding/models/BindingScope';
import { bindingTypeValues } from '../../binding/models/BindingType';
import { ConstantValueBinding } from '../../binding/models/ConstantValueBinding';
Expand All @@ -15,6 +16,7 @@ import { InstanceBinding } from '../../binding/models/InstanceBinding';
import { Provider } from '../../binding/models/Provider';
import { ProviderBinding } from '../../binding/models/ProviderBinding';
import { ServiceRedirectionBinding } from '../../binding/models/ServiceRedirectionBinding';
import { ActivationsService } from '../../binding/services/ActivationsService';
import { BindingService } from '../../binding/services/BindingService';
import { BindingServiceImplementation } from '../../binding/services/BindingServiceImplementation';
import { isPromise } from '../../common/calculations/isPromise';
Expand Down Expand Up @@ -65,6 +67,7 @@ describe(resolve.name, () => {
let serviceRedirectionBinding: ServiceRedirectionBinding<unknown>;
let serviceRedirectionToNonExistentBinding: ServiceRedirectionBinding<unknown>;

let activationService: ActivationsService;
let bindingService: BindingService;
let getClassMetadataFunction: (type: Newable) => ClassMetadata;

Expand Down Expand Up @@ -172,6 +175,7 @@ describe(resolve.name, () => {
type: bindingTypeValues.ServiceRedirection,
};

activationService = new ActivationsService();
bindingService = new BindingServiceImplementation();

bindingService.set(constantValueBinding);
Expand Down Expand Up @@ -247,6 +251,12 @@ describe(resolve.name, () => {

return resolve({
context: resolutionContext,
getActivations: <TActivated>(
serviceIdentifier: ServiceIdentifier,
): BindingActivation<TActivated>[] | undefined =>
activationService.get(serviceIdentifier) as
| BindingActivation<TActivated>[]
| undefined,
planResult,
requestScopeCache: new Map(),
}) as TMultiple extends false
Expand Down Expand Up @@ -420,6 +430,12 @@ describe(resolve.name, () => {

result = resolve({
context: resolutionContext,
getActivations: <TActivated>(
serviceIdentifier: ServiceIdentifier,
): BindingActivation<TActivated>[] | undefined =>
activationService.get(serviceIdentifier) as
| BindingActivation<TActivated>[]
| undefined,
planResult,
requestScopeCache: new Map(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { ServiceIdentifier } from '@inversifyjs/common';

import { BindingActivation } from '../../binding/models/BindingActivation';
import { PlanResult } from '../../planning/models/PlanResult';
import { ResolutionContext } from './ResolutionContext';

export interface ResolutionParams {
context: ResolutionContext;
getActivations: <TActivated>(
serviceIdentifier: ServiceIdentifier<TActivated>,
) => BindingActivation<TActivated>[] | undefined;
planResult: PlanResult;
requestScopeCache: Map<number, unknown>;
}
Loading