Skip to content

Commit

Permalink
fix(mock-doc): handle children in contains() (#3363)
Browse files Browse the repository at this point in the history
update implementation for `contains()` to return `true` for
a depth of greater than one (when a node is indeed a descendant
of another)
  • Loading branch information
erwinheitzman committed May 9, 2022
1 parent c01a05a commit 2f8a6c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/mock-doc/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ export class MockNode {
if (otherNode === this) {
return true;
}
return this.childNodes.includes(otherNode);
const childNodes = Array.from(this.childNodes);
if (childNodes.includes(otherNode)) {
return true;
}

return childNodes.some((node) => this.contains.bind(node)(otherNode));
}

removeChild(childNode: MockNode) {
Expand Down
33 changes: 33 additions & 0 deletions src/mock-doc/test/element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,39 @@ describe('element', () => {
});
});

describe('contains', () => {
it('returns true when a node is an direct child of a given node', () => {
const root = document.createElement('div');
const span = document.createElement('span');

root.appendChild(span);

expect(root.contains(span)).toEqual(true);
});

it('returns true when a node is an indirect child of a given node', () => {
const root = document.createElement('div');
const span = document.createElement('span');
const h1 = document.createElement('h1');

root.appendChild(span);
span.appendChild(h1);

expect(root.contains(h1)).toEqual(true);
});

it('returns true when a node is the given node itself', () => {
const root = document.createElement('div');
expect(root.contains(root)).toEqual(true);
});

it('returns false when a node is not the given node itself or not a descendant of the given node ', () => {
const root = document.createElement('div');
const span = document.createElement('span');
expect(root.contains(span)).toEqual(false);
});
});

describe('isConnected', () => {
it('nested true', () => {
const elmParent = document.createElement('div');
Expand Down

0 comments on commit 2f8a6c0

Please sign in to comment.