Leaflet/spec/suites/dom/DomEventSpec.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

describe('DomEvent', function () {
2010-09-07 20:04:00 +08:00
var el;
function simulateClick(el) {
if (document.createEvent) {
var e = document.createEvent('MouseEvents');
e.initMouseEvent('click', true, true, window,
2010-09-07 20:04:00 +08:00
0, 0, 0, 0, 0, false, false, false, false, 0, null);
return el.dispatchEvent(e);
} else if (el.fireEvent) {
return el.fireEvent('onclick');
}
}
2013-03-02 05:49:20 +08:00
beforeEach(function () {
2010-09-07 20:04:00 +08:00
el = document.createElement('div');
el.style.position = 'absolute';
el.style.top = el.style.left = '-10000px';
document.body.appendChild(el);
});
2013-03-02 05:49:20 +08:00
afterEach(function () {
2010-09-07 20:04:00 +08:00
document.body.removeChild(el);
});
2013-03-02 05:49:20 +08:00
describe('#addListener', function () {
it('adds a listener and calls it on event', function () {
2013-03-02 05:49:20 +08:00
var listener1 = sinon.spy(),
2015-09-25 18:55:37 +08:00
listener2 = sinon.spy();
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
L.DomEvent.addListener(el, 'click', listener1);
L.DomEvent.addListener(el, 'click', listener2);
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
simulateClick(el);
2013-03-02 05:49:20 +08:00
expect(listener1.called).to.be.ok();
expect(listener2.called).to.be.ok();
2010-09-07 20:04:00 +08:00
});
2013-03-02 05:49:20 +08:00
it('binds "this" to the given context', function () {
2010-09-07 20:04:00 +08:00
var obj = {foo: 'bar'},
2015-09-25 18:55:37 +08:00
result;
2013-03-02 05:49:20 +08:00
L.DomEvent.addListener(el, 'click', function () {
2010-09-07 20:04:00 +08:00
result = this;
}, obj);
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
simulateClick(el);
2013-03-02 05:49:20 +08:00
expect(result).to.eql(obj);
2010-09-07 20:04:00 +08:00
});
2013-03-02 05:49:20 +08:00
it('passes an event object to the listener', function () {
2010-09-07 20:04:00 +08:00
var type;
2013-03-02 05:49:20 +08:00
L.DomEvent.addListener(el, 'click', function (e) {
2010-09-07 20:04:00 +08:00
type = e && e.type;
});
simulateClick(el);
2013-03-02 05:49:20 +08:00
expect(type).to.eql('click');
2010-09-07 20:04:00 +08:00
});
it('is chainable', function () {
var res = L.DomEvent.addListener(el, 'click', function () {});
expect(res.addListener).to.be.a('function');
});
2010-09-07 20:04:00 +08:00
});
2013-03-02 05:49:20 +08:00
describe('#removeListener', function () {
it('removes a previously added listener', function () {
2013-03-02 05:49:20 +08:00
var listener = sinon.spy();
2010-09-07 20:04:00 +08:00
L.DomEvent.addListener(el, 'click', listener);
L.DomEvent.removeListener(el, 'click', listener);
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
simulateClick(el);
2013-03-02 05:49:20 +08:00
expect(listener.called).to.not.be.ok();
2010-09-07 20:04:00 +08:00
});
it('is chainable', function () {
var res = L.DomEvent.removeListener(el, 'click', function () {});
expect(res.removeListener).to.be.a('function');
});
2010-09-07 20:04:00 +08:00
});
2013-03-02 05:49:20 +08:00
describe('#stopPropagation', function () {
it('stops propagation of the given event', function () {
2010-09-07 20:04:00 +08:00
var child = document.createElement('div'),
2015-09-25 18:55:37 +08:00
listener = sinon.spy();
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
el.appendChild(child);
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
L.DomEvent.addListener(child, 'click', L.DomEvent.stopPropagation);
L.DomEvent.addListener(el, 'click', listener);
2013-03-02 05:49:20 +08:00
2010-09-07 20:04:00 +08:00
simulateClick(child);
2013-03-02 05:49:20 +08:00
expect(listener.called).to.not.be.ok();
2010-09-07 20:04:00 +08:00
el.removeChild(child);
});
});
describe('#preventDefault', function () {
it('prevents the default action of event', function () {
2010-09-07 20:04:00 +08:00
L.DomEvent.addListener(el, 'click', L.DomEvent.preventDefault);
2013-03-02 05:49:20 +08:00
expect(simulateClick(el)).to.be(false);
2010-09-07 20:04:00 +08:00
});
});
2013-03-02 05:49:20 +08:00
});