Skip to content
Snippets Groups Projects
Select Git revision
  • 9eed9cea28e841db66c6bab508647832d4c57b37
  • master default protected
  • 1.31
  • 4.38.7
  • 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
23 results

dimension.mjs

Blame
  • dimension.mjs 3.79 KiB
    import {expect} from 'chai';
    import {convertToPixels, getDeviceDPI} from "../../../../application/source/dom/dimension.mjs";
    import {getWindow} from "../../../../application/source/dom/util.mjs";
    import {initJSDOM, isBrowser, JSDOMExport as JSDOM} from "../../util/jsdom.mjs";
    import {getGlobal} from "../../../../application/source/types/global.mjs";
    import {detectRuntimeEnvironment} from "../../../../application/source/util/runtime.mjs";
    
    
    function getMockWindow(dpi) {
    
        if(detectRuntimeEnvironment() === 'browser') {
            return getWindow();
        }
        
        
        const dom = new JSDOM('', {
            pretendToBeVisual: true,
            resources: 'usable',
        });
    
        dom.window.matchMedia = (query) => {
            const dpiRegex = /\(max-resolution: (\d+)dpi\)/;
            const match = query.match(dpiRegex);
    
            if (match) {
                const maxDpi = parseInt(match[1], 10);
                return {matches: dpi <= maxDpi};
            }
    
            return {matches: false};
        };
    
        return dom.window;
    }
    
    describe('dimension', () => {
        let currentEnvironment;
    
        before(function (done) {
            initJSDOM().then(() => {
                //chaiDom(getDocument());
                done();
            });
        })
    
        beforeEach(() => {
    
            const testDpi = 96;
            const testWindow = getMockWindow(testDpi);
            getGlobal().window = testWindow;
    
        });
    
        afterEach(() => {
            delete getGlobal().window;
        });
    
        describe('convertToPixels', () => {
            it('should correctly convert px values', () => {
                const result = convertToPixels('100px');
                expect(result).to.equal(100);
            });
    
            it('should correctly convert em values', () => {
                const testElement = document.createElement('div');
                testElement.style.fontSize = '16px';
                document.body.appendChild(testElement);
    
                const result = convertToPixels('2em', testElement, testElement);
                expect(result).to.equal(32);
    
                document.body.removeChild(testElement);
            });
    
            it('should correctly convert rem values', () => {
                const testElement = document.createElement('div');
                testElement.style.fontSize = '16px';
                document.documentElement.appendChild(testElement);
    
                const result = convertToPixels('2rem', testElement);
                expect(result).to.equal(32);
    
                document.documentElement.removeChild(testElement);
            });
    
            it('should correctly convert percentage values', () => {
                const testElement = document.createElement('div');
                testElement.style.width = '500px';
                document.body.appendChild(testElement);
    
                const result = convertToPixels('50%', testElement);
                expect(result).to.equal(250);
    
                document.body.removeChild(testElement);
            });
    
            it('should throw an error for unsupported units', () => {
                expect(() => convertToPixels('10unsupportedUnit')).to.throw('Unsupported unit: unsupportedUnit');
            });
        });
    
    
        describe('getDeviceDPI', () => {
            it('should return the correct device DPI', () => {
                const testDpi = 96;
                const testWindow = getMockWindow(testDpi);
                getGlobal().window = testWindow;
    
                const deviceDpi = getDeviceDPI();
                expect(deviceDpi).to.equal(testDpi * testWindow.devicePixelRatio);
    
                delete getGlobal().window;
            });
    
            it('should cache the result and return the same value', () => {
                const testDpi = 96;
                const testWindow = getMockWindow(testDpi);
                getGlobal().window = testWindow;
    
                const deviceDpi1 = getDeviceDPI();
                const deviceDpi2 = getDeviceDPI();
                expect(deviceDpi1).to.equal(deviceDpi2);
    
                delete getGlobal().window;
            });
        });
    
    });