Skip to content

Commit

Permalink
Fix exception from objectArrayTrait using MergeStrategy topStratum
Browse files Browse the repository at this point in the history
  • Loading branch information
steve9164 committed Mar 14, 2024
1 parent 36527c8 commit 6fe2312
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### next release (8.6.2)

- Fixed exception thrown from `objectArrayTrait` when a model has 0 strata and a `MergeStrategy` of `topStratum`.
- [The next improvement]

#### 8.6.1 - 2024-03-14
Expand Down
18 changes: 10 additions & 8 deletions lib/Traits/Decorators/objectArrayTrait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ export class ObjectArrayTrait<T extends ModelTraits> extends Trait {
// If merge strategy is topStratum, then we only want to keep the ids that exist in the top stratum
if (this.merge === MergeStrategy.TopStratum) {
const topStratum = model.strataTopToBottom.values().next().value;

const topIds = this.getIdsAcrossStrata(new Map([["top", topStratum]]));
// Remove ids that don't exist in the top stratum
idsInCorrectOrder.forEach((id) => {
if (!topIds.has(id)) {
idsWithCorrectRemovals.delete(id);
}
});
// topStratum will be undefined if a model has 0 strata
if (topStratum !== undefined) {
const topIds = this.getIdsAcrossStrata(new Map([["top", topStratum]]));
// Remove ids that don't exist in the top stratum
idsInCorrectOrder.forEach((id) => {
if (!topIds.has(id)) {
idsWithCorrectRemovals.delete(id);
}
});
}
}

// Correct ids are:
Expand Down
6 changes: 6 additions & 0 deletions test/Traits/objectArrayTraitSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,12 @@ describe("objectArrayTrait", function () {
expect(model.inner[0].baz).toBe(true);
});

it("no error when a model has 0 strata and merge = top", function () {
const terria = new Terria();
const model = new TestModelMergeTop("test", terria);
expect(model.inner.length).toBe(0);
});

it("updates to reflect new strata added after evaluation (with merge = top)", function () {
const terria = new Terria();
const model = new TestModelMergeTop("test", terria);
Expand Down

0 comments on commit 6fe2312

Please sign in to comment.