import { findElementWithIdUpwards } from "../../../../application/source/dom/util.mjs"; import {expect} from 'chai'; import {initJSDOM} from "../../util/jsdom.mjs"; function setupTestEnvironment() { class TestComponent extends HTMLElement { constructor() { super(); this.attachShadow({mode: 'open'}); } } if (!customElements.get('test-component')) { customElements.define('test-component', TestComponent); } } function cleanupTestEnvironment() { let mocks = document.getElementById('mocks'); mocks.innerHTML = ""; } describe('findElementWithIdUpwards', () => { before((done) => { initJSDOM().then(() => { setupTestEnvironment(); done() }); }); after(() => { cleanupTestEnvironment(); }); beforeEach(() => { // Set up the DOM let mocks = document.getElementById('mocks'); mocks.innerHTML = ` <div id="container"> <div id="parent"> <div id="child"></div> </div> </div>`; const shadowHost = document.createElement('div'); document.body.appendChild(shadowHost); const shadowRoot = shadowHost.attachShadow({mode: 'open'}); const innerElement = document.createElement('div'); innerElement.id = 'inner'; shadowRoot.appendChild(innerElement); }); it('should find the element with the target ID in the normal DOM', () => { const child = document.getElementById('child'); const result = findElementWithIdUpwards(child, 'parent'); expect(result).to.equal(document.getElementById('parent')); }); it('should find the element with the target ID in the shadow DOM', () => { const innerElement = document.querySelector('div[shadowroot] > div'); const result = findElementWithIdUpwards(innerElement, 'inner'); expect(result).to.equal(innerElement); }); it('should return null if the element with the target ID is not found', () => { const child = document.getElementById('child'); const result = findElementWithIdUpwards(child, 'nonexistent'); expect(result).to.be.null; }); });