Skip to content

Commit 2f8a6c0

Browse files
fix(mock-doc): handle children in contains() (#3363)
update implementation for `contains()` to return `true` for a depth of greater than one (when a node is indeed a descendant of another)
1 parent c01a05a commit 2f8a6c0

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/mock-doc/node.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@ export class MockNode {
141141
if (otherNode === this) {
142142
return true;
143143
}
144-
return this.childNodes.includes(otherNode);
144+
const childNodes = Array.from(this.childNodes);
145+
if (childNodes.includes(otherNode)) {
146+
return true;
147+
}
148+
149+
return childNodes.some((node) => this.contains.bind(node)(otherNode));
145150
}
146151

147152
removeChild(childNode: MockNode) {

src/mock-doc/test/element.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,39 @@ describe('element', () => {
238238
});
239239
});
240240

241+
describe('contains', () => {
242+
it('returns true when a node is an direct child of a given node', () => {
243+
const root = document.createElement('div');
244+
const span = document.createElement('span');
245+
246+
root.appendChild(span);
247+
248+
expect(root.contains(span)).toEqual(true);
249+
});
250+
251+
it('returns true when a node is an indirect child of a given node', () => {
252+
const root = document.createElement('div');
253+
const span = document.createElement('span');
254+
const h1 = document.createElement('h1');
255+
256+
root.appendChild(span);
257+
span.appendChild(h1);
258+
259+
expect(root.contains(h1)).toEqual(true);
260+
});
261+
262+
it('returns true when a node is the given node itself', () => {
263+
const root = document.createElement('div');
264+
expect(root.contains(root)).toEqual(true);
265+
});
266+
267+
it('returns false when a node is not the given node itself or not a descendant of the given node ', () => {
268+
const root = document.createElement('div');
269+
const span = document.createElement('span');
270+
expect(root.contains(span)).toEqual(false);
271+
});
272+
});
273+
241274
describe('isConnected', () => {
242275
it('nested true', () => {
243276
const elmParent = document.createElement('div');

0 commit comments

Comments
 (0)