Skip to content

Commit

Permalink
Merge pull request #300 from inversify/fix/one-to-many-map-star-to-al…
Browse files Browse the repository at this point in the history
…low-duplicated-models

Fix activation and deactivation service to allow duplicated values
  • Loading branch information
notaphplover authored Jan 14, 2025
2 parents 057cdf9 + fa92319 commit a012bfe
Show file tree
Hide file tree
Showing 16 changed files with 273 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-mice-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": patch
---

Updated `DeactivationService` to allow duplicated deactivations
5 changes: 5 additions & 0 deletions .changeset/tender-laws-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inversifyjs/core": patch
---

Updated `ActivationService` to allow duplicated activations
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ describe(ActivationsService.name, () => {
jest.clearAllMocks();
});

it('should call activationMaps.set()', () => {
expect(activationMapsMock.set).toHaveBeenCalledTimes(1);
expect(activationMapsMock.set).toHaveBeenCalledWith(
it('should call activationMaps.add()', () => {
expect(activationMapsMock.add).toHaveBeenCalledTimes(1);
expect(activationMapsMock.add).toHaveBeenCalledWith(
activationFixture,
relationFixture,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class ActivationsService implements Cloneable<ActivationsService> {
activation: BindingActivation,
relation: BindingActivationRelation,
): void {
this.#activationMaps.set(activation, relation);
this.#activationMaps.add(activation, relation);
}

public clone(): ActivationsService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ describe(BindingService.name, () => {
jest.clearAllMocks();
});

it('should call bindingMaps.set()', () => {
it('should call bindingMaps.add()', () => {
const expectedRelation: BindingRelation = {
serviceId: bindingFixture.serviceIdentifier,
};

expect(bindingMapsMock.set).toHaveBeenCalledTimes(1);
expect(bindingMapsMock.set).toHaveBeenCalledWith(
expect(bindingMapsMock.add).toHaveBeenCalledTimes(1);
expect(bindingMapsMock.add).toHaveBeenCalledWith(
bindingFixture,
expectedRelation,
);
Expand Down Expand Up @@ -395,14 +395,14 @@ describe(BindingService.name, () => {
jest.clearAllMocks();
});

it('should call bindingMaps.set()', () => {
it('should call bindingMaps.add()', () => {
const expectedRelation: BindingRelation = {
moduleId: bindingFixture.moduleId as number,
serviceId: bindingFixture.serviceIdentifier,
};

expect(bindingMapsMock.set).toHaveBeenCalledTimes(1);
expect(bindingMapsMock.set).toHaveBeenCalledWith(
expect(bindingMapsMock.add).toHaveBeenCalledTimes(1);
expect(bindingMapsMock.add).toHaveBeenCalledWith(
bindingFixture,
expectedRelation,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ export class BindingService implements Cloneable<BindingService> {
relation[BindingRelationKind.moduleId] = binding.moduleId;
}

this.#bindingMaps.set(binding as Binding<unknown>, relation);
this.#bindingMaps.add(binding as Binding<unknown>, relation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ describe(DeactivationsService.name, () => {
jest.clearAllMocks();
});

it('should call deactivationMaps.set()', () => {
expect(deactivationMapsMock.set).toHaveBeenCalledTimes(1);
expect(deactivationMapsMock.set).toHaveBeenCalledWith(
it('should call deactivationMaps.add()', () => {
expect(deactivationMapsMock.add).toHaveBeenCalledTimes(1);
expect(deactivationMapsMock.add).toHaveBeenCalledWith(
deactivationFixture,
relationFixture,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class DeactivationsService implements Cloneable<DeactivationsService> {
deactivation: BindingDeactivation,
relation: BindingDeactivationRelation,
): void {
this.#deactivationMaps.set(deactivation, relation);
this.#deactivationMaps.add(deactivation, relation);
}

public clone(): DeactivationsService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe(OneToManyMapStar.name, () => {
},
});

oneToManyMapStar.set(Symbol(), {
oneToManyMapStar.add(Symbol(), {
[RelationKey.bar]: 2,
[RelationKey.foo]: 'foo-value-fixture',
});
Expand Down Expand Up @@ -67,7 +67,7 @@ describe(OneToManyMapStar.name, () => {
},
});

oneToManyMapStar.set(modelFixture, {
oneToManyMapStar.add(modelFixture, {
[relationKeyFixture]: relationValueFixture,
});
});
Expand Down Expand Up @@ -125,6 +125,54 @@ describe(OneToManyMapStar.name, () => {
});
});
});

describe('having a OneToManyMapStartSpec with twice a model', () => {
let modelFixture: unknown;
let relationKeyFixture: RelationKey.foo;
let relationValueFixture: string;

let oneToManyMapStar: OneToManyMapStar<unknown, RelationTest>;

beforeAll(() => {
modelFixture = Symbol();
relationKeyFixture = RelationKey.foo;
relationValueFixture = 'value-fixture';

oneToManyMapStar = new OneToManyMapStar<unknown, RelationTest>({
bar: {
isOptional: true,
},
foo: {
isOptional: false,
},
});

oneToManyMapStar.add(modelFixture, {
[relationKeyFixture]: relationValueFixture,
});

oneToManyMapStar.add(modelFixture, {
[relationKeyFixture]: relationValueFixture,
});
});

describe('when called', () => {
let result: unknown;

beforeAll(() => {
result = [
...(oneToManyMapStar.get(
relationKeyFixture,
relationValueFixture,
) ?? []),
];
});

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

describe('.getAllKeys', () => {
Expand All @@ -150,7 +198,7 @@ describe(OneToManyMapStar.name, () => {
},
});

oneToManyMapStar.set(modelFixture, relationFixture);
oneToManyMapStar.add(modelFixture, relationFixture);
});

describe('when called', () => {
Expand All @@ -168,7 +216,7 @@ describe(OneToManyMapStar.name, () => {
});

describe('.removeByRelation', () => {
describe('having a OneToManyMapStart with a no models', () => {
describe('having a OneToManyMapStart with no models', () => {
let relationFixture: Required<RelationTest>;
let oneToManyMapStar: OneToManyMapStar<unknown, RelationTest>;

Expand Down Expand Up @@ -247,7 +295,7 @@ describe(OneToManyMapStar.name, () => {
},
});

oneToManyMapStar.set(modelFixture, relationFixture);
oneToManyMapStar.add(modelFixture, relationFixture);
});

describe('when called', () => {
Expand Down Expand Up @@ -289,6 +337,79 @@ describe(OneToManyMapStar.name, () => {
});
});
});

describe('having a OneToManyMapStart with twice a model with different relations', () => {
let modelFixture: unknown;
let firstRelationFixture: Required<RelationTest>;
let secondRelationFixture: Required<RelationTest>;
let oneToManyMapStar: OneToManyMapStar<unknown, RelationTest>;

beforeAll(() => {
modelFixture = Symbol();
firstRelationFixture = {
bar: 3,
foo: 'foo',
};
secondRelationFixture = {
bar: 4,
foo: firstRelationFixture[RelationKey.foo],
};
oneToManyMapStar = new OneToManyMapStar<unknown, RelationTest>({
bar: {
isOptional: true,
},
foo: {
isOptional: false,
},
});

oneToManyMapStar.add(modelFixture, firstRelationFixture);
oneToManyMapStar.add(modelFixture, secondRelationFixture);
});

describe('when called', () => {
beforeAll(() => {
oneToManyMapStar.removeByRelation(
RelationKey.bar,
firstRelationFixture[RelationKey.bar],
);
});

describe('when called .get()', () => {
let results: {
[TKey in RelationKey]-?: Iterable<unknown> | undefined;
};

beforeAll(() => {
results = {
[RelationKey.bar]: [
...(oneToManyMapStar.get(
RelationKey.bar,
firstRelationFixture[RelationKey.bar],
) ?? []),
],
[RelationKey.foo]: [
...(oneToManyMapStar.get(
RelationKey.foo,
firstRelationFixture[RelationKey.foo],
) ?? []),
],
};
});

it('should return expected results', () => {
const expectedResults: {
[TKey in RelationKey]-?: Iterable<unknown> | undefined;
} = {
[RelationKey.bar]: [],
[RelationKey.foo]: [modelFixture],
};

expect(results).toStrictEqual(expectedResults);
});
});
});
});
});

describe('.set', () => {
Expand All @@ -314,7 +435,7 @@ describe(OneToManyMapStar.name, () => {

describe('when called', () => {
beforeAll(() => {
oneToManyMapStar.set(modelFixture, relationFixture);
oneToManyMapStar.add(modelFixture, relationFixture);
});

describe('when called .get() with relation values', () => {
Expand Down
Loading

0 comments on commit a012bfe

Please sign in to comment.