Skip to content

Commit

Permalink
fix merge issue with nested layouts (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
loreanvictor authored Jan 28, 2024
1 parent 5503272 commit 88338a9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/main/components/store/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ export function merge(oldState: ModelState, newState: ModelState): ModelState {
...oldState,
diagram: {
...oldState.diagram,
ownedElements: oldState.diagram.ownedElements.filter(
(id) => !!newState.elements[id] && !newState.elements[id].owner,
),
ownedElements: Object.keys(newState.elements).filter((id) => !newState.elements[id].owner),
ownedRelationships: oldState.diagram.ownedRelationships.filter((id) => !!newState.elements[id]),
},
elements: Object.keys(newState.elements).reduce((acc, id) => {
Expand Down
12 changes: 12 additions & 0 deletions src/tests/unit/components/store/merge-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { merge } from '../../../../main/components/store/merge';
import { ModelState } from '../../../../main/components/store/model-state';
import diagram from '../../test-resources/class-diagram.json';
import diagram3 from '../../test-resources/class-diagram-3.json';
import diagram4 from '../../test-resources/class-diagram-4.json';

describe('merge', () => {
const pkgId = 'c10b995a-036c-4e9e-aa67-0570ada5cb6a';
Expand All @@ -19,4 +20,15 @@ describe('merge', () => {
expect(merged.elements[class2Id]).toBeDefined();
expect(merged.elements[class1Id].owner).toBeNull();
});

test('calculates the correct owned elements.', () => {
const oldState = ModelState.fromModel(diagram as any) as ModelState;
const newState = ModelState.fromModel(diagram4 as any) as ModelState;

const merged = merge(oldState, newState);

expect(merged.diagram.ownedElements).toContain(class1Id);
expect(merged.diagram.ownedElements).toContain(class2Id);
expect(merged.diagram.ownedElements).toContain(pkgId);
});
});
88 changes: 88 additions & 0 deletions src/tests/unit/test-resources/class-diagram-4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"version": "3.0.0",
"type": "ClassDiagram",
"size": { "width": 860, "height": 260 },
"interactive": { "elements": {}, "relationships": {} },
"elements": {
"c10b995a-036c-4e9e-aa67-0570ada5cb6a": {
"id": "c10b995a-036c-4e9e-aa67-0570ada5cb6a",
"name": "Package",
"type": "Package",
"owner": null,
"bounds": { "x": 0, "y": 0, "width": 350, "height": 220 }
},
"04d3509e-0dce-458b-bf62-f3555497a5a4": {
"id": "04d3509e-0dce-458b-bf62-f3555497a5a4",
"name": "Class",
"type": "Class",
"owner": null,
"bounds": { "x": 80, "y": 70, "width": 200, "height": 100 },
"attributes": ["90f94404-1fc6-4121-97ed-6b2c6d57d525"],
"methods": ["12d8bb82-e4e5-4505-a0eb-48ddab0fc0a0"]
},
"90f94404-1fc6-4121-97ed-6b2c6d57d525": {
"id": "90f94404-1fc6-4121-97ed-6b2c6d57d525",
"name": "+ attribute: Type",
"type": "ClassAttribute",
"owner": "04d3509e-0dce-458b-bf62-f3555497a5a4",
"bounds": { "x": 80, "y": 110, "width": 200, "height": 30 }
},
"12d8bb82-e4e5-4505-a0eb-48ddab0fc0a0": {
"id": "12d8bb82-e4e5-4505-a0eb-48ddab0fc0a0",
"name": "+ method()",
"type": "ClassMethod",
"owner": "04d3509e-0dce-458b-bf62-f3555497a5a4",
"bounds": { "x": 80, "y": 140, "width": 200, "height": 30 }
},
"9eadc4f6-caa0-4835-a24c-71c0c1ccbc39": {
"id": "9eadc4f6-caa0-4835-a24c-71c0c1ccbc39",
"name": "Class",
"type": "Class",
"owner": null,
"bounds": { "x": 620, "y": 90, "width": 200, "height": 100 },
"attributes": ["dbd4193a-4483-43df-8934-77192be006c2"],
"methods": ["e7ef41ee-290e-4df2-a535-199f1c5521fd"]
},
"dbd4193a-4483-43df-8934-77192be006c2": {
"id": "dbd4193a-4483-43df-8934-77192be006c2",
"name": "+ attribute: Type",
"type": "ClassAttribute",
"owner": "9eadc4f6-caa0-4835-a24c-71c0c1ccbc39",
"bounds": { "x": 620, "y": 130, "width": 200, "height": 30 }
},
"e7ef41ee-290e-4df2-a535-199f1c5521fd": {
"id": "e7ef41ee-290e-4df2-a535-199f1c5521fd",
"name": "+ method()",
"type": "ClassMethod",
"owner": "9eadc4f6-caa0-4835-a24c-71c0c1ccbc39",
"bounds": { "x": 620, "y": 160, "width": 200, "height": 30 }
}
},
"relationships": {
"f5c4e20d-8347-4136-bc02-b7a016e017f5": {
"id": "f5c4e20d-8347-4136-bc02-b7a016e017f5",
"name": "",
"type": "ClassBidirectional",
"owner": null,
"bounds": { "x": 280, "y": 130, "width": 340, "height": 1 },
"path": [
{ "x": 340, "y": 0 },
{ "x": 0, "y": 0 }
],
"source": {
"direction": "Left",
"element": "9eadc4f6-caa0-4835-a24c-71c0c1ccbc39",
"multiplicity": "",
"role": ""
},
"target": {
"direction": "Right",
"element": "04d3509e-0dce-458b-bf62-f3555497a5a4",
"multiplicity": "",
"role": ""
},
"isManuallyLayouted": false
}
},
"assessments": {}
}

0 comments on commit 88338a9

Please sign in to comment.