Skip to content

Commit

Permalink
fix: childCases after adding/removing cases [#186182255] (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson authored Oct 11, 2023
1 parent b89f5b3 commit 543f488
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
15 changes: 15 additions & 0 deletions v3/src/models/data/data-set-collection-groups.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ describe("CollectionGroups", () => {
expect(data.getCasesForAttributes(["cId"])).toEqual(data.cases)
expect(data.groupedAttributes).toEqual([])
expect(data.ungroupedAttributes.map(attr => attr.id)).toEqual(["aId", "bId", "cId"])
// case caches are updated when cases are added/removed
const allCases = data.cases.map(({ __id__ }) => ({ __id__ }))
const childCases = data.childCases()
expect(childCases).toEqual(allCases)
data.addCases([{ __id__: "4-5-6", aId: 4, bId: 5, cId: 6 }])
const allCases2 = data.cases.map(({ __id__ }) => ({ __id__ }))
expect(allCases2).not.toEqual(allCases)
const childCases2 = data.childCases()
expect(childCases2).not.toEqual(childCases)
expect(childCases2).toEqual(allCases2)
data.removeCases(["4-5-6"])
const allCases3 = data.cases.map(({ __id__ }) => ({ __id__ }))
const childCases3 = data.childCases()
expect(childCases3).toEqual(childCases)
expect(childCases3).toEqual(allCases3)
})

it("handles grouping by a single attribute", () => {
Expand Down
12 changes: 8 additions & 4 deletions v3/src/models/data/data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ export const DataSet = types.model("DataSet", {
getCollectionForAttribute,
// leaf-most child cases (i.e. those not grouped in a collection)
childCases() {
if (!isValidCollectionGroups.get()) {
// childCases array cache is built by collectionGroups()
this.collectionGroups // eslint-disable-line no-unused-expressions
}
return _childCases
},
// the resulting collection groups
Expand Down Expand Up @@ -877,8 +881,8 @@ export const DataSet = types.model("DataSet", {
})
insertCaseIDAtIndex(__id__, insertPosition)
})
// invalidate collectionGroups if there's any grouping
self.collections.length && self.invalidateCollectionGroups()
// invalidate collectionGroups (including childCases)
self.invalidateCollectionGroups()
},

// Supports regular cases or pseudo-cases, but not mixing the two.
Expand Down Expand Up @@ -945,8 +949,8 @@ export const DataSet = types.model("DataSet", {
}
}
})
// invalidate collectionGroups if there's grouping going on
self.collections.length && self.invalidateCollectionGroups()
// invalidate collectionGroups (including childCases)
self.invalidateCollectionGroups()
},

selectAll(select = true) {
Expand Down

0 comments on commit 543f488

Please sign in to comment.