Angular Can';t在Internet Explorer 11中创建用于测试输入组件(角度)的键盘事件

Angular Can';t在Internet Explorer 11中创建用于测试输入组件(角度)的键盘事件,angular,karma-runner,internet-explorer-11,typescript-typings,karma-webpack,Angular,Karma Runner,Internet Explorer 11,Typescript Typings,Karma Webpack,我们为我们的angular应用程序提供了启动和运行的karma测试套件。所有测试均为铬绿色。现在我必须修复一个IE11测试失败的问题。我得到的例外情况如下: TypeError: Object doesn't support this action at createEventWithKeycode (http://localhost:9876/_karma_webpack_/main.bundle.js:3574:5) at Anonymous function (

我们为我们的angular应用程序提供了启动和运行的karma测试套件。所有测试均为铬绿色。现在我必须修复一个IE11测试失败的问题。我得到的例外情况如下:

TypeError: Object doesn't support this action
       at createEventWithKeycode (http://localhost:9876/_karma_webpack_/main.bundle.js:3574:5)
       at Anonymous function (http://localhost:9876/_karma_webpack_/main.bundle.js:3115:13)
       at Anonymous function (http://localhost:9876/_karma_webpack_/vendor.bundle.js:74994:17)
       at ZoneDelegate.prototype.invoke (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:10531:13)
       at ProxyZoneSpec.prototype.onInvoke (http://localhost:9876/_karma_webpack_/vendor.bundle.js:26806:13)
       at ZoneDelegate.prototype.invoke (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:10531:13)
       at Zone.prototype.run (http://localhost:9876/_karma_webpack_/polyfills.bundle.js:10283:17)
       at Anonymous function (http://localhost:9876/_karma_webpack_/vendor.bundle.js:26503:13)
据我所知,测试用例正在angulars TestBed类的帮助下创建一个用于测试的角度组件。通过一些神奇的代码,我们获得了输入组件本身,并在其上运行了一个keydown事件:

inputComponent.onKeyDownInput(event);
但是,当我在IE中运行代码时,当我尝试使用以下代码创建事件对象时,异常已经发生:

const event = new KeyboardEvent('keydown', {         //This triggers the exception
    bubbles: true,
    cancelable: true,
    shiftKey: true
  });
KeyboardEvent对象在打字文件lib.es6.d.ts中定义:

declare var KeyboardEvent: {
    prototype: KeyboardEvent;
    new(typeArg: string, eventInitDict?: KeyboardEventInit): KeyboardEvent;
    readonly DOM_KEY_LOCATION_JOYSTICK: number;
    readonly DOM_KEY_LOCATION_LEFT: number;
    readonly DOM_KEY_LOCATION_MOBILE: number;
    readonly DOM_KEY_LOCATION_NUMPAD: number;
    readonly DOM_KEY_LOCATION_RIGHT: number;
    readonly DOM_KEY_LOCATION_STANDARD: number;
}

有人能解释一下这个问题吗?请记住,一切都与铬-只有IE不行为

如果有人遇到这个问题,我可以让下面的程序正常工作

函数createKeyEvent(
key:key | number=null,选项:{type:'keyup'|'keydown'|'input',bubbles?:布尔,可取消?:布尔}={
键入:“keyup”,
泡泡:是的,
可取消:正确
}):键盘事件{
让eventInitDict:any={bubbles:options.bubbles,cancelable:options.cancelable};
如果(关键){
eventInitDict.key=String.fromCharCode(key);
}
如果(键===键移位){
eventInitDict.shiftKey=true;
key=null;
}
让事件发生;
如果(isBrowser(['ie10','ie11'])){
event=document.createEvent('KeyboardEvent')作为KeyboardEvent;
event.initKeyboardEvent(options.type、options.cancelable、options.bubbles、窗口、键、0、0、0);
}否则{
事件=新键盘事件(options.type,eventInitDict)
}
如果(关键){
defineProperties(事件,{which:{get:()=>key}});
defineProperties(事件,{keyCode:{get:()=>key});
}
返回事件;
}
编辑:添加有关键对象的信息

导出枚举密钥{
退格=8,
Tab=9,
输入=13,
班次=16,
逃逸=27,
空间=32,
结束=35,
Home=36,
箭头左=37,
箭头向上=38,
箭头右=39,
向下箭头=40
}
编辑#2:添加
isBrowser
功能

export-type浏览器='ie9'|'ie10'|'ie11'|'ie'|'edge'|'chrome'|'safari'|'firefox';
导出函数getBrowser(ua=window.navigator.userAgent){
让浏览器='未知';
//IE<11
常量msie=ua.indexOf('msie');
如果(msie>0){
返回'ie'+parseInt(ua.substring(msie+5,ua.indexOf('.',msie)),10);
}
//IE 11
如果(ua.indexOf('Trident/')>0){
设rv=ua.indexOf('rv:');
返回'ie'+parseInt(ua.substring(rv+3,ua.indexOf('.',rv)),10;
}
//边缘
如果(ua.indexOf('Edge/')>0){
返回“边缘”;
}
//铬
如果(ua.indexOf('Chrome/')>0){
返回“chrome”;
}
//狩猎
如果(ua.indexOf('Safari/')>0){
返回‘狩猎’;
}
//火狐
如果(ua.indexOf('Firefox/')>0){
返回“firefox”;
}
如果(浏览器==‘未知’){
抛出新错误(“+ua”的浏览器检测失败);
}
}
导出函数isBrowser(浏览器:Browser | Browser[],ua=window.navigator.userAgent){
让browsersStr=Array.isArray(浏览器)?(浏览器作为浏览器[])。map(x=>x.toString()):[browsers.toString()];
让browser=getBrowser(ua);
if(browserstr.indexOf('ie')>-1&&browser.startsWith('ie')){
返回true;
}否则{
返回browserstr.indexOf(browser)>-1;
}
}

键对象指的是什么类型?@r0bb077更新的答案
iBrowser
的功能是什么?我不知道在IE9之后你可以嗅出IE浏览器。@SolomonClosson用
isBrowser
函数更新了答案。这绝对有可能。你解析了用户代理。太棒了,谢谢你。你有没有解决过这个问题?