Skip to content
Snippets Groups Projects
Select Git revision
  • a610a872c24b7c8c9aaf4e065fafe1efbe078c5b
  • master default protected
  • 1.31
  • 4.38.6
  • 4.38.5
  • 4.38.4
  • 4.38.3
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
23 results

dimension.mjs

Blame
  • util.mjs 3.78 KiB
    import {
        getDocument, getWindow, getDocumentFragmentFromString
    } from "../../../../application/source/dom/util.mjs";
    
    import {getContainingDocument} from "../../../../application/source/dom/util.mjs";
    
    import {initJSDOM} from "../../util/jsdom.mjs";
    
    import {expect} from "chai"
    
    
    describe('DOM', function () {
        before(async function () {
            initJSDOM();
        })
        describe('getDocument()', function () {
            it('should return document object', function () {
                let d = getDocument();
                expect(typeof d).is.equal('object');
            });
        });
    
        describe('getWindow()', function () {
            it('should return document object', function () {
                let d = getWindow();
                expect(typeof d).is.equal('object');
            });
        });
    
        describe('getDocumentFragmentFromString()', function () {
    
    
            [
                ['<div></div>'],
                ['<div><div>2</div></div>'],
                ['<div><div>2</div>', '<div><div>2</div></div>'], // test invalid html
            ].forEach(function (data) {
    
                let a = data.shift()
                let b = data.shift()
    
                if (!b) b = a;
    
                it('should return documentFragment object ' + a, function () {
                    let fragment = getDocumentFragmentFromString(a);
                    expect(fragment).to.be.instanceOf(DocumentFragment);
    
                    var div = document.createElement('div');
                    div.appendChild(fragment.cloneNode(true));
                    expect(div.innerHTML).to.be.equal(b);
                });
    
            });
    
        });
    
    
        describe('getContainingDocument', () => {
            let  jsDomDocument;
    
             beforeEach(() => {
                 jsDomDocument = getDocument();
             });
            //
            // afterEach(() => {
            //     dom.window.close();
            // });
    
            it('should throw an error when called with an invalid argument', () => {
                expect(() => getContainingDocument(null)).to.throw('Invalid argument. Expected an HTMLElement.');
            });
    
            it('should return the correct containing document for an element in the main document', () => {
                const element = jsDomDocument.createElement('div');
                const containingDocument = getContainingDocument(element);
    
                expect(containingDocument).to.null;
            });
    
            it('should return the correct containing document for an element inside a shadow root', () => {
                const host = jsDomDocument.createElement('div');
                const shadowRoot = host.attachShadow({ mode: 'open' });
                const element = jsDomDocument.createElement('span');
                shadowRoot.appendChild(element);
    
                const containingDocument = getContainingDocument(element);
                expect(containingDocument).to.not.null;
            });
    
            it('should return the correct containing document for an element inside a nested shadow root', () => {
                const outerHost = jsDomDocument.createElement('div');
                const outerShadowRoot = outerHost.attachShadow({ mode: 'open' });
    
                const innerHost = jsDomDocument.createElement('div');
                outerShadowRoot.appendChild(innerHost);
    
                const innerShadowRoot = innerHost.attachShadow({ mode: 'open' });
    
                const element = jsDomDocument.createElement('span');
                innerShadowRoot.appendChild(element);
    
                const containingDocument = getContainingDocument(element);
                expect(containingDocument).to.not.null;
            });
    
            it('should return null when the element is not attached to any document', () => {
                const detachedElement = jsDomDocument.createElement('div');
                detachedElement.remove();
    
                const containingDocument = getContainingDocument(detachedElement);
                expect(containingDocument).to.be.null;
            });
        });    
        
    });