Skip to content
Snippets Groups Projects
Select Git revision
  • 2950ec194656f78a314b02adf62670cbc8168cb5
  • master default protected
  • 1.31
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
  • 4.28.0
  • 4.27.0
  • 4.26.0
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
23 results

transformer.mjs

Blame
  • find.mjs 2.18 KiB
    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;
        });
    });