From 4e840e0e0e6af86e1cda551f3ec9e50ac57417fa Mon Sep 17 00:00:00 2001 From: Tanner Reits <47483144+tanner-reits@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:13:49 -0500 Subject: [PATCH] fix(mock-doc): add `getAttributeNode` to mock elements (#5070) * fix(mock-doc): add `getAttributeNode` to mock elements * add tests to mock-doc for `getAttributeNode` --- src/mock-doc/node.ts | 8 ++++++++ src/mock-doc/test/attribute.spec.ts | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/mock-doc/node.ts b/src/mock-doc/node.ts index 0ba7484b6e7..b39dbcca92f 100644 --- a/src/mock-doc/node.ts +++ b/src/mock-doc/node.ts @@ -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 }; } diff --git a/src/mock-doc/test/attribute.spec.ts b/src/mock-doc/test/attribute.spec.ts index 57d363399ef..98dd0274d1d 100644 --- a/src/mock-doc/test/attribute.spec.ts +++ b/src/mock-doc/test/attribute.spec.ts @@ -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');