Skip to content

Commit

Permalink
fix(mock-doc): add getAttributeNode to mock elements (#5070)
Browse files Browse the repository at this point in the history
* fix(mock-doc): add `getAttributeNode` to mock elements

* add tests to mock-doc for `getAttributeNode`
  • Loading branch information
tanner-reits committed Nov 14, 2023
1 parent 71a4110 commit 4e840e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/mock-doc/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ Testing components with ElementInternals is fully supported in e2e tests.`,
return null;
}

getAttributeNode(attrName: string): MockAttr | null {
if (!this.hasAttribute(attrName)) {
return null;
}

return new MockAttr(attrName, this.getAttribute(attrName));
}

getBoundingClientRect() {
return { bottom: 0, height: 0, left: 0, right: 0, top: 0, width: 0, x: 0, y: 0 };
}
Expand Down
18 changes: 18 additions & 0 deletions src/mock-doc/test/attribute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,24 @@ describe('attributes', () => {
expect(img.draggable).toEqual(false);
});

describe('getAttributeNode', () => {
it('should return an attribute node if the attribute exists', () => {
const div = doc.createElement('div');
div.setAttribute('draggable', 'true');
expect(div.getAttributeNode('draggable')).toEqual({
_name: 'draggable',
_namespaceURI: null,
_value: 'true',
});
});

it('should return `null` if the attribute does not exist', () => {
const div = doc.createElement('div');
div.setAttribute('draggable', 'true');
expect(div.getAttributeNode('test')).toEqual(null);
});
});

function testNsAttributes(element: MockHTMLElement) {
element.setAttributeNS('tEst', 'viewBox', '1');
element.setAttributeNS('tEst', 'viewbox', '2');
Expand Down

0 comments on commit 4e840e0

Please sign in to comment.