From c5ba5b0456a711d157e317f220e9c739226e7f50 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Tue, 10 Jan 2012 01:54:37 +0100 Subject: Installed leaflet in extlib --- extlib/leaflet/spec/suites/dom/DomEventSpec.js | 102 +++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 extlib/leaflet/spec/suites/dom/DomEventSpec.js (limited to 'extlib/leaflet/spec/suites/dom/DomEventSpec.js') diff --git a/extlib/leaflet/spec/suites/dom/DomEventSpec.js b/extlib/leaflet/spec/suites/dom/DomEventSpec.js new file mode 100644 index 00000000..83d08541 --- /dev/null +++ b/extlib/leaflet/spec/suites/dom/DomEventSpec.js @@ -0,0 +1,102 @@ +describe('DomEvent', function() { + 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'); + } + } + + beforeEach(function() { + el = document.createElement('div'); + el.style.position = 'absolute'; + el.style.top = el.style.left = '-10000px'; + document.body.appendChild(el); + }); + + afterEach(function() { + document.body.removeChild(el); + }); + + describe('#addListener', function() { + it('should add a listener and call it on event', function() { + 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(); + }); + + it('should have "this" keyword point to the given context', function() { + var obj = {foo: 'bar'}, + result; + + L.DomEvent.addListener(el, 'click', function() { + result = this; + }, obj); + + simulateClick(el); + + expect(result).toEqual(obj); + }); + + it('should pass an event object to the listener', function() { + var type; + + L.DomEvent.addListener(el, 'click', function(e) { + type = e && e.type; + }); + simulateClick(el); + + expect(type).toEqual('click'); + }); + }); + + describe('#removeListener', function() { + it('should remove prevously added listener', function() { + 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() { + it('should stop propagation of the given event', function() { + 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() { + it('should prevent the default action of event', function() { + L.DomEvent.addListener(el, 'click', L.DomEvent.preventDefault); + + expect(simulateClick(el)).toBe(false); + }); + }); +}); \ No newline at end of file -- cgit v1.2.3