Skip to content

Commit 5bbb0aa

Browse files
committed
test: Fixed event-based tests on <= IE11
1 parent c47f4c2 commit 5bbb0aa

File tree

2 files changed

+55
-84
lines changed

2 files changed

+55
-84
lines changed

test/integration/frame.html

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,50 @@
3939
}());
4040

4141
/**
42-
* Custom event factories for cross-browser compatibility
43-
*
44-
* Gecko browsers are using non-standard `initKeyEvent`, where others implemented `initKeyboardEvent`.
45-
* To make it more consistent, we try to use standardized `MouseEvent`/`KeyboardEvent` now
46-
* and fallback to older implementations for legacy browsers only.
47-
*
48-
* See deprecation notes:
49-
* https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent
50-
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyEvent
51-
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyboardEvent
42+
* DOM4 MouseEvent and KeyboardEvent Polyfills
5243
*
5344
* References:
54-
* https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent#Specifications
55-
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Specifications
45+
* https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent
46+
* https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent#Polyfill
47+
* https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent
5648
*/
57-
function createMouseEvent (options) {
58-
var options = {
59-
bubbles: true,
60-
cancelable: true,
61-
view: window
49+
(function () {
50+
try {
51+
new MouseEvent('click');
52+
return false; // No need to polyfill
53+
} catch (e) {
54+
// Need to polyfill - fall through
6255
}
6356

64-
if ('MouseEvent' in window) {
65-
return new MouseEvent('click', options);
66-
} else {
67-
var event = document.createEvent('MouseEvent');
68-
event.initMouseEvent(
69-
'click',
70-
options.bubbles,
71-
options.cancelable,
72-
options.view
73-
);
74-
75-
return event;
57+
var MouseEvent = function (eventType) {
58+
var mouseEvent = document.createEvent('MouseEvent');
59+
mouseEvent.initMouseEvent(eventType, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
60+
return mouseEvent;
7661
}
77-
}
7862

79-
function createKeyboardEvent (key) {
80-
var options = {
81-
bubbles: true,
82-
cancelable: true,
83-
view: window,
84-
key: key || 'a'
63+
MouseEvent.prototype = Event.prototype;
64+
window.MouseEvent = MouseEvent;
65+
})();
66+
67+
(function () {
68+
try {
69+
new KeyboardEvent('keypress');
70+
return false; // No need to polyfill
71+
} catch (e) {
72+
// Need to polyfill - fall through
8573
}
86-
options.charCode = options.key.charCodeAt();
87-
88-
if ('KeyboardEvent' in window) {
89-
return new KeyboardEvent('keypress', options);
90-
} else {
91-
var event = document.createEvent('KeyboardEvent');
92-
event.initKeyboardEvent(
93-
'keypress',
94-
options.bubbles,
95-
options.cancelable,
96-
options.view,
97-
options.key,
98-
options.charCode
99-
);
100-
101-
return event;
74+
75+
var KeyboardEvent = function (eventType) {
76+
var keyboardEvent = document.createEvent('KeyboardEvent');
77+
if (keyboardEvent.initKeyboardEvent) keyboardEvent.initKeyboardEvent(eventType, true, true, window, false, false, false, false, 'a', 0);
78+
if (keyboardEvent.initKeyEvent) keyboardEvent.initKeyEvent(eventType, true, true, window, false, false, false, false, 'a');
79+
return keyboardEvent;
10280
}
103-
}
81+
82+
KeyboardEvent.prototype = Event.prototype;
83+
window.KeyboardEvent = KeyboardEvent;
84+
})();
10485
</script>
105-
<script src="../../node_modules/jquery/dist/jquery.js"></script>
10686
<script src="../../build/raven.js"></script>
10787
<script>
10888
// stub _makeRequest so we don't actually transmit any data

test/integration/test.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ describe('integration', function() {
456456
false
457457
);
458458

459-
var click = createMouseEvent();
459+
var click = new MouseEvent('click');
460460
div.dispatchEvent(click);
461461
},
462462
function() {
@@ -485,14 +485,8 @@ describe('integration', function() {
485485
div.addEventListener('click', fooFn, false);
486486
div.removeEventListener('click', fooFn);
487487

488-
var evt;
489-
if (document.createEvent) {
490-
evt = document.createEvent('MouseEvents');
491-
evt.initEvent('click', true, false);
492-
div.dispatchEvent(evt);
493-
} else if (document.createEventObject) {
494-
div.fireEvent('onclick');
495-
}
488+
var click = new MouseEvent('click');
489+
div.dispatchEvent(click);
496490
},
497491
function() {
498492
var ravenData = iframe.contentWindow.ravenData[0];
@@ -820,8 +814,8 @@ describe('integration', function() {
820814
input.addEventListener('click', clickHandler);
821815

822816
// click <input/>
823-
var evt = createMouseEvent();
824-
input.dispatchEvent(evt);
817+
var click = new MouseEvent('click');
818+
input.dispatchEvent(click);
825819
},
826820
function() {
827821
var Raven = iframe.contentWindow.Raven,
@@ -853,10 +847,9 @@ describe('integration', function() {
853847
Raven._breadcrumbs = [];
854848

855849
// click <input/>
856-
var evt = createMouseEvent();
857-
850+
var click = new MouseEvent('click');
858851
var input = document.getElementsByTagName('input')[0];
859-
input.dispatchEvent(evt);
852+
input.dispatchEvent(click);
860853
},
861854
function() {
862855
var Raven = iframe.contentWindow.Raven,
@@ -899,10 +892,9 @@ describe('integration', function() {
899892
document.querySelector('.c').addEventListener('click', clickHandler);
900893

901894
// click <input/>
902-
var evt = createMouseEvent();
903-
895+
var click = new MouseEvent('click');
904896
var input = document.querySelector('.a'); // leaf node
905-
input.dispatchEvent(evt);
897+
input.dispatchEvent(click);
906898
},
907899
function() {
908900
var Raven = iframe.contentWindow.Raven,
@@ -932,16 +924,15 @@ describe('integration', function() {
932924
Raven._breadcrumbs = [];
933925

934926
// click <input/>
935-
var evt = createMouseEvent();
936-
927+
var click = new MouseEvent('click');
937928
function kaboom() {
938929
throw new Error('lol');
939930
}
940-
Object.defineProperty(evt, 'type', {get: kaboom});
941-
Object.defineProperty(evt, 'target', {get: kaboom});
931+
Object.defineProperty(click, 'type', {get: kaboom});
932+
Object.defineProperty(click, 'target', {get: kaboom});
942933

943934
var input = document.querySelector('.a'); // leaf node
944-
input.dispatchEvent(evt);
935+
input.dispatchEvent(click);
945936
},
946937
function() {
947938
var Raven = iframe.contentWindow.Raven,
@@ -969,8 +960,8 @@ describe('integration', function() {
969960
Raven._breadcrumbs = [];
970961

971962
// keypress <input/> twice
972-
var keypress1 = createKeyboardEvent('a');
973-
var keypress2 = createKeyboardEvent('b');
963+
var keypress1 = new KeyboardEvent('keypress');
964+
var keypress2 = new KeyboardEvent('keypress');
974965

975966
var input = document.getElementsByTagName('input')[0];
976967
input.dispatchEvent(keypress1);
@@ -1004,7 +995,7 @@ describe('integration', function() {
1004995
Raven._breadcrumbs = [];
1005996

1006997
// keypress <input/>
1007-
var keypress = createKeyboardEvent();
998+
var keypress = new KeyboardEvent('keypress');
1008999

10091000
var input = document.getElementsByTagName('input')[0];
10101001
input.dispatchEvent(keypress);
@@ -1042,11 +1033,11 @@ describe('integration', function() {
10421033
Raven._breadcrumbs = [];
10431034

10441035
// 1st keypress <input/>
1045-
var keypress1 = createKeyboardEvent('a');
1036+
var keypress1 = new KeyboardEvent('keypress');
10461037
// click <input/>
1047-
var click = createMouseEvent();
1038+
var click = new MouseEvent('click');
10481039
// 2nd keypress
1049-
var keypress2 = createKeyboardEvent('b');
1040+
var keypress2 = new KeyboardEvent('keypress');
10501041

10511042
var input = document.getElementsByTagName('input')[0];
10521043
input.dispatchEvent(keypress1);
@@ -1096,8 +1087,8 @@ describe('integration', function() {
10961087
Raven._breadcrumbs = [];
10971088

10981089
// keypress <input/> twice
1099-
var keypress1 = createKeyboardEvent('a');
1100-
var keypress2 = createKeyboardEvent('b');
1090+
var keypress1 = new KeyboardEvent('keypress');
1091+
var keypress2 = new KeyboardEvent('keypress');
11011092

11021093
var div = document.querySelector('[contenteditable]');
11031094
div.dispatchEvent(keypress1);

0 commit comments

Comments
 (0)