Skip to content

Commit

Permalink
tests: fix #preventDefault in IE
Browse files Browse the repository at this point in the history
Event properties is not preserved across different listeners calls in IE,
so we have to make all checks in the same listener.

Also: control case added.

Note: previously here used to be different implementation that was also reliable,
as it relied on return value of https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent

```
		function dispatchClick(el) { // return: cancelled
			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) { // IE<11
				return el.fireEvent('onclick');
			}
		}

		it('prevents the default action of event', function () {
			expect(dispatchClick(el)).to.be.ok(); // control case

			L.DomEvent.on(el, 'click', L.DomEvent.preventDefault);

			expect(dispatchClick(el)).to.not.be.ok();
		});
```
  • Loading branch information
johndoe committed Feb 25, 2021
1 parent 1dac42b commit 9bd8fca
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions spec/suites/dom/DomEventSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,25 @@ describe('DomEvent', function () {
});

describe('#preventDefault', function () {
it('prevents the default action of event', function () {
L.DomEvent.on(el, 'click', listener);
L.DomEvent.on(el, 'click', L.DomEvent.preventDefault);

happen.click(el);

var e = listener.lastCall.args[0];
function isPrevented(e) {
if ('defaultPrevented' in e) {
expect(e.defaultPrevented).to.be.ok();
} else {
expect(e.returnValue).not.to.be.ok();
return e.defaultPrevented;
} else { // IE<11
return !e.returnValue;
}
}

it('prevents the default action of event', function (done) {
L.DomEvent.on(el, 'click', function (e) {
expect(isPrevented(e)).not.to.be.ok(); // control case

L.DomEvent.preventDefault(e);

expect(isPrevented(e)).to.be.ok();
done();
});

happen.click(el);
});
});
});

0 comments on commit 9bd8fca

Please sign in to comment.