Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions packages/core/__tests__/view/mixins/CellMixin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,48 @@ describe('isCellRotatable', () => {
).toBeFalsy();
});
});

describe('isValidAncestor', () => {
function configureParentChild(parent: Cell, child: Cell) {
child.setParent(parent);
parent.children.push(child);
}

test('Parent is the direct parent of the Cell, recurse: false', () => {
const parent = new Cell();
const cell = new Cell();
cell.setParent(parent);
expect(createGraphWithoutPlugins().isValidAncestor(cell, parent)).toBeTruthy();
});

test('Cell is direct child of parent but does not declare it as parent, and recurse: false', () => {
const cell = new Cell();
const parent = new Cell();
parent.children.push(cell);
expect(createGraphWithoutPlugins().isValidAncestor(cell, parent)).toBeFalsy();
});

test('Cell is direct child of parent, recurse: true', () => {
const cell = new Cell();
const intermediateParent = new Cell();
configureParentChild(intermediateParent, cell);
const parent = new Cell();
configureParentChild(parent, intermediateParent);
expect(createGraphWithoutPlugins().isValidAncestor(cell, parent, true)).toBeTruthy();
});

test.each([true, false])(
'Cell does not match parent, recurse: %s',
(recurse: boolean) => {
expect(
createGraphWithoutPlugins().isValidAncestor(new Cell(), new Cell(), recurse)
).toBeFalsy();
}
);

test.each([true, false])('null Cell, recurse: %s', (recurse: boolean) => {
expect(
createGraphWithoutPlugins().isValidAncestor(null, new Cell(), recurse)
).toBeFalsy();
});
});
2 changes: 1 addition & 1 deletion packages/core/src/view/mixins/CellsMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ export const CellsMixin: PartialType = {
},

isValidAncestor(cell, parent, recurse = false) {
return recurse ? parent.isAncestor(cell) : cell.getParent() === parent;
return recurse ? parent.isAncestor(cell) : cell?.getParent() === parent;
},

/*****************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/view/mixins/CellsMixin.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ declare module '../Graph' {
* @param parent {@link Cell} the possible parent cell
* @param recurse boolean whether to recurse the child ancestors. Default is `false`.
*/
isValidAncestor: (cell: Cell, parent: Cell, recurse: boolean) => boolean;
isValidAncestor: (cell: Cell | null, parent: Cell, recurse?: boolean) => boolean;

/**
* Returns `true` if the given cell may not be moved, sized, bended, disconnected, edited or selected.
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/view/mixins/EdgeMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ export const EdgeMixin: PartialType = {
(source !== target &&
((incoming &&
target === cell &&
(!parent || this.isValidAncestor(<Cell>source, parent, recurse))) ||
(!parent || this.isValidAncestor(source, parent, recurse))) ||
(outgoing &&
source === cell &&
(!parent || this.isValidAncestor(<Cell>target, parent, recurse)))))
(!parent || this.isValidAncestor(target, parent, recurse)))))
) {
result.push(edges[i]);
}
Expand Down