Skip to content

Commit

Permalink
Fix tests failing in IE (#7461)
Browse files Browse the repository at this point in the history
* tests: IE<11 does not understand const/let

* tests: specify all required initEvent arguments for (IE)

Otherwise we get: TypeError: Argument not optional

* tests: add profiles to emulate legacy IE versions in IE11

I can confirm that only IE10 profile is working.

Sample commandline:

npm run test-nolint -- --browsers IE,IE10

* tests: consider that style.zIndex is Number type in IE

* tests: set container height explicitly to fix 6 tests failing in IE

in IE when container.style.height is not set, container.clientWidth is always 0, despite container.style.width value.

* tests: fix #preventDefault in IE

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();
		});
```

* tests: skip crossOrigin-related tests in IE<11

https://caniuse.com/mdn-html_elements_img_crossorigin
  • Loading branch information
johnd0e authored Nov 1, 2021
1 parent a4a11f5 commit f018e2d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
12 changes: 12 additions & 0 deletions spec/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ module.exports = function (config) {
}
}
}
},
IE8: { // not working in IE 11!!
base: 'IE',
'X-UA-Compatible': 'IE=EmulateIE8'
},
IE9: { // not working in IE 11!!
base: 'IE',
'X-UA-Compatible': 'IE=EmulateIE9'
},
IE10: {
base: 'IE',
'x-ua-compatible': 'IE=EmulateIE10'
}
},

Expand Down
27 changes: 17 additions & 10 deletions spec/suites/dom/DomEventSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,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);
});
});
});
13 changes: 7 additions & 6 deletions spec/suites/layer/ImageOverlaySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('ImageOverlay', function () {

function raiseImageEvent(event) {
var domEvent = document.createEvent('Event');
domEvent.initEvent(event);
domEvent.initEvent(event, false, false);
overlay._image.dispatchEvent(domEvent);
}

Expand Down Expand Up @@ -108,23 +108,23 @@ describe('ImageOverlay', function () {
it('should update the z-index of the image if it has allready been added to the map', function () {
var overlay = L.imageOverlay('', imageBounds);
overlay.addTo(map);
expect(overlay._image.style.zIndex).to.be('1');
expect(overlay._image.style.zIndex).to.eql('1'); // Number type in IE

overlay.setZIndex('10');
expect(overlay._image.style.zIndex).to.be('10');
expect(overlay._image.style.zIndex).to.eql('10'); // Number type in IE
});

it('should set the z-index of the image when it is added to the map', function () {
var overlay = L.imageOverlay('', imageBounds);
overlay.setZIndex('10');
overlay.addTo(map);
expect(overlay._image.style.zIndex).to.be('10');
expect(overlay._image.style.zIndex).to.eql('10'); // Number type in IE
});

it('should use the z-index specified in options', function () {
var overlay = L.imageOverlay('', imageBounds, {zIndex: 20});
overlay.addTo(map);
expect(overlay._image.style.zIndex).to.be('20');
expect(overlay._image.style.zIndex).to.eql('20'); // Number type in IE
});

it('should be fluent', function () {
Expand All @@ -135,7 +135,8 @@ describe('ImageOverlay', function () {

// For tests that do not actually need to append the map container to the document.
// This saves PhantomJS memory.
describe('_image2', function () {
var _describe = 'crossOrigin' in L.DomUtil.create('img') ? describe : describe.skip; // skip in IE<11
_describe('crossOrigin option', function () {
var overlay;
var blankUrl = "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";

Expand Down
3 changes: 2 additions & 1 deletion spec/suites/layer/tile/TileLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ describe('TileLayer', function () {

});

describe('options', function () {
var _describe = 'crossOrigin' in L.DomUtil.create('img') ? describe : describe.skip; // skip in IE<11
_describe('crossOrigin option', function () {
beforeEach(function () {
map.setView([0, 0], 2);
});
Expand Down
1 change: 1 addition & 0 deletions spec/suites/map/MapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ describe("Map", function () {
clock;

beforeEach(function () {
container.style.height = "100px";
container.style.width = origWidth + "px";
map.setView([0, 0], 0);
map.invalidateSize({pan: false});
Expand Down
16 changes: 8 additions & 8 deletions spec/suites/map/handler/Map.KeyboardSpec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
describe("Map.Keyboard", function () {
const KEYCODE_LOWERCASE_A = 65;
const KEYCODE_ARROW_LEFT = 37;
const KEYCODE_ARROW_UP = 38;
const KEYCODE_ARROW_RIGHT = 39;
const KEYCODE_ARROW_DOWN = 40;
const KEYCODE_PLUS = 171;
const KEYCODE_MINUS = 173;
const KEYCODE_ESC = 27;
var KEYCODE_LOWERCASE_A = 65;
var KEYCODE_ARROW_LEFT = 37;
var KEYCODE_ARROW_UP = 38;
var KEYCODE_ARROW_RIGHT = 39;
var KEYCODE_ARROW_DOWN = 40;
var KEYCODE_PLUS = 171;
var KEYCODE_MINUS = 173;
var KEYCODE_ESC = 27;

var map, container;

Expand Down

0 comments on commit f018e2d

Please sign in to comment.