2010-09-07 19:27:44 +08:00
|
|
|
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,
|
|
|
|
0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
|
|
|
return el.dispatchEvent(e);
|
|
|
|
} else if (el.fireEvent) {
|
|
|
|
return el.fireEvent('onclick');
|
|
|
|
}
|
|
|
|
}
|
2010-09-07 19:27:44 +08:00
|
|
|
|
2010-09-07 20:04:00 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
el = document.createElement('div');
|
|
|
|
el.style.position = 'absolute';
|
|
|
|
el.style.top = el.style.left = '-10000px';
|
|
|
|
document.body.appendChild(el);
|
|
|
|
});
|
2010-09-07 19:27:44 +08:00
|
|
|
|
2010-09-07 20:04:00 +08:00
|
|
|
afterEach(function() {
|
|
|
|
document.body.removeChild(el);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#addListener', function() {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('adds a listener and calls it on event', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
var listener1 = jasmine.createSpy('listener1'),
|
|
|
|
listener2 = jasmine.createSpy('listener2');
|
|
|
|
|
|
|
|
L.DomEvent.addListener(el, 'click', listener1);
|
|
|
|
L.DomEvent.addListener(el, 'click', listener2);
|
|
|
|
|
|
|
|
simulateClick(el);
|
|
|
|
|
|
|
|
expect(listener1).toHaveBeenCalled();
|
|
|
|
expect(listener2).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('binds "this" to the given context', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
var obj = {foo: 'bar'},
|
|
|
|
result;
|
|
|
|
|
|
|
|
L.DomEvent.addListener(el, 'click', function() {
|
|
|
|
result = this;
|
|
|
|
}, obj);
|
|
|
|
|
|
|
|
simulateClick(el);
|
|
|
|
|
|
|
|
expect(result).toEqual(obj);
|
|
|
|
});
|
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('passes an event object to the listener', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
var type;
|
|
|
|
|
|
|
|
L.DomEvent.addListener(el, 'click', function(e) {
|
|
|
|
type = e && e.type;
|
|
|
|
});
|
|
|
|
simulateClick(el);
|
|
|
|
|
|
|
|
expect(type).toEqual('click');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#removeListener', function() {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('removes a previously added listener', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
var listener = jasmine.createSpy('listener');
|
|
|
|
|
|
|
|
L.DomEvent.addListener(el, 'click', listener);
|
|
|
|
L.DomEvent.removeListener(el, 'click', listener);
|
|
|
|
|
|
|
|
simulateClick(el);
|
|
|
|
|
|
|
|
expect(listener).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#stopPropagation', function() {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('stops propagation of the given event', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
var child = document.createElement('div'),
|
|
|
|
listener = jasmine.createSpy('listener');
|
|
|
|
|
|
|
|
el.appendChild(child);
|
|
|
|
|
|
|
|
L.DomEvent.addListener(child, 'click', L.DomEvent.stopPropagation);
|
|
|
|
L.DomEvent.addListener(el, 'click', listener);
|
|
|
|
|
|
|
|
simulateClick(child);
|
|
|
|
|
|
|
|
expect(listener).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
el.removeChild(child);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('#preventDefault', function() {
|
2013-02-20 04:41:48 +08:00
|
|
|
it('prevents the default action of event', function() {
|
2010-09-07 20:04:00 +08:00
|
|
|
L.DomEvent.addListener(el, 'click', L.DomEvent.preventDefault);
|
|
|
|
|
|
|
|
expect(simulateClick(el)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
2010-09-07 19:27:44 +08:00
|
|
|
});
|