Leaflet/spec/suites/dom/DomUtilSpec.js

90 lines
2.5 KiB
JavaScript
Raw Normal View History

2010-09-07 22:02:45 +08:00
describe('DomUtil', function() {
var el;
2013-02-05 23:01:01 +08:00
2010-09-07 22:02:45 +08:00
beforeEach(function() {
el = document.createElement('div');
el.style.position = 'absolute';
el.style.top = el.style.left = '-10000px';
document.body.appendChild(el);
});
2013-02-05 23:01:01 +08:00
2010-09-07 22:02:45 +08:00
afterEach(function() {
document.body.removeChild(el);
});
2013-02-05 23:01:01 +08:00
2010-09-07 22:02:45 +08:00
describe('#get', function() {
it('gets element by id if the given argument is string', function() {
2010-09-07 22:02:45 +08:00
el.id = 'testId';
2013-03-02 05:49:20 +08:00
expect(L.DomUtil.get(el.id)).to.eql(el);
2010-09-07 22:02:45 +08:00
});
2013-02-05 23:01:01 +08:00
it('returns the element if it is given as an argument', function() {
2013-03-02 05:49:20 +08:00
expect(L.DomUtil.get(el)).to.eql(el);
2010-09-07 22:02:45 +08:00
});
});
2013-02-05 23:01:01 +08:00
2011-07-15 20:03:35 +08:00
describe('#addClass, #removeClass, #hasClass', function() {
it('has defined class for test element', function() {
2011-07-15 20:03:35 +08:00
el.className = 'bar foo baz ';
2013-03-02 05:49:20 +08:00
expect(L.DomUtil.hasClass(el, 'foo')).to.be.ok();
expect(L.DomUtil.hasClass(el, 'bar')).to.be.ok();
expect(L.DomUtil.hasClass(el, 'baz')).to.be.ok();
expect(L.DomUtil.hasClass(el, 'boo')).to.not.be.ok();
2011-07-15 20:03:35 +08:00
});
2013-02-05 23:01:01 +08:00
it('adds or removes the class', function() {
2011-07-15 20:03:35 +08:00
el.className = '';
L.DomUtil.addClass(el, 'foo');
2013-02-05 23:01:01 +08:00
2013-03-02 05:49:20 +08:00
expect(el.className).to.eql('foo');
expect(L.DomUtil.hasClass(el, 'foo')).to.be.ok();
2013-02-05 23:01:01 +08:00
2011-07-15 20:03:35 +08:00
L.DomUtil.addClass(el, 'bar');
2013-03-02 05:49:20 +08:00
expect(el.className).to.eql('foo bar');
expect(L.DomUtil.hasClass(el, 'foo')).to.be.ok();
2013-02-05 23:01:01 +08:00
2011-07-15 20:03:35 +08:00
L.DomUtil.removeClass(el, 'foo');
2013-03-02 05:49:20 +08:00
expect(el.className).to.eql('bar');
expect(L.DomUtil.hasClass(el, 'foo')).to.not.be.ok();
2013-02-05 23:01:01 +08:00
2011-07-15 20:03:35 +08:00
el.className = 'foo bar barz';
L.DomUtil.removeClass(el, 'bar');
2013-03-02 05:49:20 +08:00
expect(el.className).to.eql('foo barz');
});
2011-07-15 20:03:35 +08:00
});
2013-02-05 23:01:01 +08:00
describe('#documentIsLtr', function () {
it('returns true if doc direction is ltr', function () {
2013-03-02 05:49:20 +08:00
expect(L.DomUtil.documentIsLtr()).to.eql(true);
expect(L.DomUtil.documentIsLtr()).to.eql(true); // cached
2013-02-05 23:01:01 +08:00
});
});
describe('#getViewportOffset', function () {
it('calculates the viewport offset of an element', function () {
2013-02-20 00:21:01 +08:00
var div = document.createElement('div');
div.style.position = 'absolute';
div.style.top = '100px';
div.style.left = '200px';
div.style.border = '10px solid black';
div.style.padding = '50px';
div.style.visibility = 'hidden';
2013-02-05 23:01:01 +08:00
2013-02-20 00:21:01 +08:00
var div2 = document.createElement('div');
div.style.marginTop = '100px';
2013-02-05 23:01:01 +08:00
2013-02-20 00:21:01 +08:00
div.appendChild(div2);
2013-02-05 23:01:01 +08:00
2013-02-20 00:21:01 +08:00
document.body.appendChild(div);
2013-02-05 23:01:01 +08:00
2013-03-02 05:49:20 +08:00
expect(L.DomUtil.getViewportOffset(div2)).to.eql(new L.Point(260, 260));
2013-02-05 23:01:01 +08:00
2013-02-20 00:21:01 +08:00
document.body.removeChild(div);
});
2013-02-05 23:01:01 +08:00
});
2013-03-02 05:49:20 +08:00
// describe('#setPosition', noSpecs);
2013-02-05 23:01:01 +08:00
2013-03-02 05:49:20 +08:00
// describe('#getStyle', noSpecs);
2013-02-05 23:01:01 +08:00
});