Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DomUtil function tests #7547

Merged
merged 11 commits into from
Apr 15, 2022
260 changes: 257 additions & 3 deletions spec/suites/dom/DomUtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('DomUtil', function () {
beforeEach(function () {
el = document.createElement('div');
el.style.position = 'absolute';
el.style.top = el.style.left = '-10000px';
el.style.top = el.style.left = '0px';
document.body.appendChild(el);
});

Expand Down Expand Up @@ -52,8 +52,262 @@ describe('DomUtil', function () {
expect(el.className).to.eql('foo barz');
});
});
describe('#getStyle', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('gets the value for a certain style attribute on an element,', function () {
el.id = 'testId';
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
el.style.color = 'black';
expect(L.DomUtil.getStyle(el, 'color')).to.eql('black');
el.style.color = 'green';
expect(L.DomUtil.getStyle(el, 'color')).to.eql('green');
});
});

describe('#create, #remove, #empty', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('create a HTML element, sets a class, appends it to container element.', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
el.id = 'testId';
expect(el.children.length).to.equal(0);
L.DomUtil.create('h1', 'title', el);
expect(el.children.length).to.equal(1);
});
it('removes element from parent', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
el.id = 'testId';
expect(el.children.length).to.equal(0);
L.DomUtil.create('h1', 'title', el);
expect(el.children.length).to.equal(1);
L.DomUtil.remove(L.DomUtil.get(el.children[0]));
expect(el.children.length).to.equal(0);
});
it('removes all of the elements children elements', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
el.id = 'testId';
expect(el.children.length).to.equal(0);

[0, 1, 2, 3].forEach(function (num) {
L.DomUtil.create('div', 'childContainer' + num, el);
});
expect(el.children.length).to.equal(4);

L.DomUtil.remove(L.DomUtil.get(el.children[0]));
expect(el.children.length).to.equal(3);

L.DomUtil.empty(el);
expect(el.children.length).to.equal(0);
});
});

describe('#toFront, #toBack', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
beforeEach(function () {
L.DomUtil.create('div', 'childContainer', el);
L.DomUtil.create('h1', 'childh1', el);
L.DomUtil.create('p', 'childp', el);
});
it('Moves el to last child position parent element', function () {
el.id = 'testId';
expect(el.children.length).to.equal(3);
expect(Array.from(el.children).indexOf(document.querySelector('.childContainer') === 0));
L.DomUtil.toFront(el.children[0]);
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
expect(Array.from(el.children).indexOf(document.querySelector('.childContainer') === 2));
});
it('Moves el to first child position parent element', function () {
el.id = 'testId';
expect(el.children.length).to.equal(3);
expect(Array.from(el.children).indexOf(document.querySelector('.childp') === 2));
L.DomUtil.toFront(el.children[0]);
expect(Array.from(el.children).indexOf(document.querySelector('.childp') === 0));
});
});

describe('#setClass, #getClass', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('Sets the elements class', function () {
el.id = 'testId';
expect(el.classList.contains('newClass')).to.not.be.ok();
L.DomUtil.setClass(el, 'newClass');
expect(el.classList.contains('newClass')).to.be.ok();
});
it('Gets the elements class', function () {
el.id = 'testId';
expect(L.DomUtil.getClass(el)).to.not.equal('newClass');
L.DomUtil.setClass(el, 'newClass');
expect(L.DomUtil.getClass(el)).to.equal('newClass');
});
});

describe('#setOpacity', function () {
it('Sets opacity of element', function () {
el.id = 'testId';
L.DomUtil.setOpacity(el, 1);
expect(el.style.opacity).to.equal('1');
L.DomUtil.setOpacity(el, 0.5);
expect(el.style.opacity).to.equal('0.5');
L.DomUtil.setOpacity(el, '0');
expect(el.style.opacity).to.equal('0');
});
});

describe('#testProp', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('Check array of style names return first valid style name for element', function () {
var hasProp;
hasProp = L.DomUtil.testProp(['-webkit-transform', '-webkit-transform', '-ms-tranform', '-o-transform']);
expect(hasProp).to.match(/(?:-webkit-transform|-webkit-transform|-ms-tranform|-o-transform)/);
});
});

// describe('#setPosition', noSpecs);
describe('#setTransform', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it("Resets the transform style of an el.", function () {
expect(L.DomUtil.getStyle(el, '-webkit-transform')).to.be.equal('none');
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved

// describe('#getStyle', noSpecs);
var offset = L.point(200, 200);
var scale = 10;
L.DomUtil.setTransform(el, offset, scale);
var transform = L.DomUtil.getStyle(el, '-webkit-transform');
expect(L.DomUtil.getStyle(el, '-webkit-transform')).to.be.equal(transform);

var newScale = 20;
var newOffset = L.point(400, 400);
L.DomUtil.setTransform(el, newOffset, newScale);
expect(L.DomUtil.getStyle(el, '-webkit-transform')).to.not.be.equal(transform);
});
});

describe('#setPosition, #getPosition', function () {
it("Sets position of el to coordinates specified by position.", function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
expect(L.DomUtil.getStyle(el, 'left')).to.be.equal('0px');
expect(L.DomUtil.getStyle(el, 'top')).to.be.equal('0px');

var x = 50;
var y = 55;
var position = L.point(x, y);
L.DomUtil.setPosition(el, position);
expect(L.DomUtil.getPosition(el)).to.be.eql({x: x, y: y});

var newX = 333;
var newY = 666;
var newPosition = L.point(newX, newY);
L.DomUtil.setPosition(el, newPosition);
expect(L.DomUtil.getPosition(el)).to.be.eql({x: newX, y: newY});
});

it("Returns position of an element positioned with setPosition.", function () {
var coordinates = {x: 333, y: 666};
var position = L.point(coordinates);
expect(L.DomUtil.getPosition(el)).to.not.eql(coordinates);
L.DomUtil.setPosition(el, position);
expect(L.DomUtil.getPosition(el)).to.be.an('object');
expect(L.DomUtil.getPosition(el)).to.eql(coordinates);
});
});

describe('#getSizedParentNode', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('Find nearest parent element where height / width are not null', function () {
var child = document.createElement('div');
var grandChild = document.createElement('div');
el.appendChild(child);
child.appendChild(grandChild);
child.style.width = child.style.height = '500px';
expect(L.DomUtil.getSizedParentNode(grandChild)).to.eql(child);
});
});

describe('#getScale', function () {
Falke-Design marked this conversation as resolved.
Show resolved Hide resolved
it('Returns scale of element as x & y scales respectively', function () {
var childEl = document.createElement('div');
childEl.style.width = '250px';
childEl.style.height = '250px';
childEl.style.padding = '15px';
childEl.style.margin = '25px';
el.appendChild(childEl);
var scale = {
x: 1,
y: 1,
boundingClientRect: {
x: 25,
y: 25,
width: 280,
height: 280,
top: 25,
right: 305,
bottom: 305,
left: 25,
}
};
// Not all browsers contain x y inside boundclientRect, always contains top, right, bottom and left properties
expect(L.DomUtil.getScale(childEl).boundingClientRect.top).to.be.equal(scale.boundingClientRect.top);
expect(L.DomUtil.getScale(childEl).boundingClientRect.right).to.be.equal(scale.boundingClientRect.right);
expect(L.DomUtil.getScale(childEl).boundingClientRect.bottom).to.be.equal(scale.boundingClientRect.bottom);
expect(L.DomUtil.getScale(childEl).boundingClientRect.left).to.be.equal(scale.boundingClientRect.left);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be the height and width checked too?


childEl.style.padding = '400px';
expect(L.DomUtil.getScale(childEl).boundingClientRect.bottom).to.not.be.equal(scale.boundingClientRect.bottom);
});
});


describe('#disableTextSelection, #enableTextSelection', function () {
it('Disable / enable the selectstart DOM events for the user ', function () {
var selectionPrevented;
function checkPrevented(e) {
if (e.defaultPrevented) {
selectionPrevented = true;
} else {
selectionPrevented = false;
}
}
var child = document.createElement('div');
el.appendChild(child);

L.DomUtil.disableTextSelection();
window.addEventListener('selectstart', checkPrevented);
happen.once(child, {type: 'selectstart'});
expect(selectionPrevented).to.be.ok();

L.DomUtil.enableTextSelection();
happen.once(child, {type: 'selectstart'});
expect(selectionPrevented).to.not.be.ok();
});
});

describe('#disableImageDrag, #enablerImageDrag', function () {
it('Disable / enable dragstart DOM events for the user', function () {
var selectionPrevented;
function checkPrevented(e) {
if (e.defaultPrevented) {
selectionPrevented = true;
} else {
selectionPrevented = false;
}
}
var child = document.createElement('div');
el.appendChild(child);

L.DomUtil.disableImageDrag();
window.addEventListener('dragstart', checkPrevented);
happen.once(child, {type: 'dragstart'});
expect(selectionPrevented).to.be.ok();

L.DomUtil.enableImageDrag();
happen.once(child, {type: 'dragstart'});
expect(selectionPrevented).to.not.be.ok();
});
});

describe('#preventOutline, #restoreOutline', function () {
it('Prevent / Restore outline for the element', function () {
var child = document.createElement('div');
el.appendChild(child);
child.tabIndex = 0;
expect(child.style.outline).to.be.equal(child.style.outline);
L.DomUtil.preventOutline(child);
expect(child.style.outline).to.match(/(?:none)/);

// Explicit #restoreOutline through direct call
expect(child.style.outline).to.match(/(?:none)/);
L.DomUtil.restoreOutline(child);
expect(child.style.outline).to.be.equal(child.style.outline);

// Implicit #restoreOutline test through simulation
L.DomUtil.preventOutline(child);
expect(child.style.outline).to.match(/(?:none)/);
happen.once(child, {type: 'keydown'});
expect(child.style.outline).to.be.equal(child.style.outline);
});
});
});