Skip to content

Commit

Permalink
Merge pull request #241 from inversify/fix/update-service-deactivatio…
Browse files Browse the repository at this point in the history
…n-to-invoke-missing-handlers

Update service deactivation to invoke missing handlers
  • Loading branch information
notaphplover authored Jan 4, 2025
2 parents dec391e + 889f3ac commit 4907abf
Show file tree
Hide file tree
Showing 9 changed files with 504 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ export class ConstantValueBindingFixtures {
};
}

public static get withOnDeactivationAsync(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
onDeactivation: async (): Promise<void> => undefined,
};
}

public static get withOnDeactivationSync(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
onDeactivation: (): void => undefined,
};
}

public static get withOnDeactivationUndefined(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
onDeactivation: undefined,
};
}

public static get withModuleIdUndefined(): ConstantValueBinding<unknown> {
return {
...ConstantValueBindingFixtures.any,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';

jest.mock('../../common/calculations/chain');
jest.mock('../../common/models/OneToManyMapStar');

import { ServiceIdentifier } from '@inversifyjs/common';

import { chain } from '../../common/calculations/chain';
import { OneToManyMapStar } from '../../common/models/OneToManyMapStar';
import { BindingDeactivation } from '../models/BindingDeactivation';
import {
Expand Down Expand Up @@ -83,7 +85,7 @@ describe(DeactivationsService.name, () => {
serviceIdFixture = 'service-identifier';
});

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

let result: unknown;
Expand All @@ -92,8 +94,12 @@ describe(DeactivationsService.name, () => {
bindingActivationFixture = Symbol() as unknown as BindingDeactivation;

activationMapsMock.get
.mockReturnValueOnce(undefined)
.mockReturnValueOnce([bindingActivationFixture]);
.mockReturnValueOnce([bindingActivationFixture])
.mockReturnValueOnce(undefined);

(chain as jest.Mock<typeof chain>).mockReturnValueOnce([
bindingActivationFixture,
]);

result = activationsService.get(serviceIdFixture);
});
Expand All @@ -116,20 +122,34 @@ describe(DeactivationsService.name, () => {
);
});

it('should call chain()', () => {
expect(chain).toHaveBeenCalledTimes(1);
expect(chain).toHaveBeenCalledWith([bindingActivationFixture]);
});

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

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

let result: unknown;

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

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

(chain as jest.Mock<typeof chain>)
.mockReturnValueOnce([bindingActivationFixture])
.mockReturnValueOnce([
bindingActivationFixture,
bindingActivationFixture,
]);

result = activationsService.get(serviceIdFixture);
});
Expand All @@ -139,15 +159,34 @@ describe(DeactivationsService.name, () => {
});

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

it('should call chain()', () => {
expect(chain).toHaveBeenCalledTimes(2);
expect(chain).toHaveBeenNthCalledWith(1, [bindingActivationFixture]);
expect(chain).toHaveBeenNthCalledWith(
2,
[bindingActivationFixture],
[bindingActivationFixture],
);
});

it('should return BindingDeactivation[]', () => {
expect(result).toStrictEqual([bindingActivationFixture]);
expect(result).toStrictEqual([
bindingActivationFixture,
bindingActivationFixture,
]);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ServiceIdentifier } from '@inversifyjs/common';

import { chain } from '../../common/calculations/chain';
import { OneToManyMapStar } from '../../common/models/OneToManyMapStar';
import { BindingDeactivation } from '../models/BindingDeactivation';

Expand Down Expand Up @@ -47,12 +48,30 @@ export class DeactivationsService {
public get(
serviceIdentifier: ServiceIdentifier,
): Iterable<BindingDeactivation> | undefined {
return (
const deactivationIterables: Iterable<BindingDeactivation>[] = [];

const deactivations: Iterable<BindingDeactivation> | undefined =
this.#activationMaps.get(
DeactivationRelationKind.serviceId,
serviceIdentifier,
) ?? this.#parent?.get(serviceIdentifier)
);
);

if (deactivations !== undefined) {
deactivationIterables.push(deactivations);
}

const parentDeactivations: Iterable<BindingDeactivation> | undefined =
this.#parent?.get(serviceIdentifier);

if (parentDeactivations !== undefined) {
deactivationIterables.push(parentDeactivations);
}

if (deactivationIterables.length === 0) {
return undefined;
}

return chain(...deactivationIterables);
}

public removeAllByModuleId(moduleId: number): void {
Expand Down
Loading

0 comments on commit 4907abf

Please sign in to comment.