【发布时间】:2023-03-03 04:17:01
【问题描述】:
我有一个在 iframed 窗口中运行的 Typescript/Angular 应用程序。在应用程序中,我使用intl-tel-input 模块来格式化和验证我们的电话号码输入。 Intl-tel-input 将“intlTelInputUtils”函数放在 $window 对象上。在没有 iframe 的情况下本地测试应用程序时,我能够成功调用类似的方法(注意周围的窗口只是为了满足 Typescript 编译器:
(<any>window).intlTelInputUtils.getExtension(phoneNumber.userInput, phoneNumber.countryCode);
并通过模拟 window.intlTelInputUtils 对象来通过测试:
$window.intlTelInputUtils = {
'getExtension': function () { return ''; },
'formatNumber': function () { return ''; },
'isValidNumber': function () { return true; }
};
但是,一旦我们在嵌入的 iframe 窗口中,这当然不起作用。 IntlTelInputUtils 现在出现在 parent.window.intl....
我尝试将方法调用更改为:
(<any>parent.window).intlTelInputUtils.getExtension(phoneNumber.userInput, phoneNumber.countryCode);
但我无法将它推送到服务器以查看它是否有效(会吗??),直到我可以通过 Jasmine 测试。我尝试将模拟更改为:
$parent.$window = {
'intlTelInputUtils': {
'getExtension': function () {
return '';
},
'formatNumber': function () {
return '';
},
'isValidNumber': function () {
return true;
}
}
};
但我意识到这是不正确的。
如何在我的测试中正确地模拟 parent.window.intlTelInputUtils??
【问题讨论】:
标签: angularjs typescript jasmine