javascript - 不使用反应的 Jest 和 jquery-i18next
问题描述
我真的需要一些帮助来弄清楚为什么“ .localize() ”不能与我的 Jest 测试套件一起使用。在各种测试中,我总是得到一个未通过的测试结果,因为显然“$(...).localize 不是一个函数”。
我的 jestSetup 文件有 global.localize = jest.fn();
问题似乎是我会有这样的事情:
it('should update with the right animal', () => {
document.body.innerHTML =
`<div class='hopScotch'></div>`
superFunctionMango('OWL')
expect($('.hopScotch').attr('data-
i18n')).toBe('OWL.#')
//and it allpasses except for app.localize
})
superFunctionMango 在实际的 js 文件中是这样的:
function updateNewInvoiceFieldsWithCorrectDocType(bird) {
$("#newBarBird").attr("data-i18n", `${bird}.new`);
$(".newBirdTitle").attr("data-i18n",
`${bird}.#`);
$(".newBirdDate").attr("data-i18n",
`${bird}.date`);
$(".newBirdSelect").attr("data-i18n", `${bird}.name`);
$("#app").localize();
}
就是 $('#app').localize() 似乎总是把事情搞砸。做什么?
解决方案
$.fn.extend({
localize: jest.fn(),
language: jest.fn(),
i18n:{
changeLanguage: jest.fn()
}
});
想通了,真的很简单。您所要做的就是在 setupJest.js 中扩展 jQuery 的方法!!:)
推荐阅读
- python - 使用scrapy,python从DIV中的第二个孩子获取文本
- kubernetes - 从 kubelet 批准证书后,kubenetes 无法获取节点信息
- javascript - 事件处理程序被多次触发
- c - 我在创建一个函数来搜索我的链接列表中的字符串时遇到问题
- json - 将 JSON 解析为 Dart 中的列表
- javascript - JavaScript 很难使用局部变量
- javascript - Bootstrap 5 选项卡和表单
- c - 结构的存储大小未知
- arrays - 将数据作为数组从函数存储并传递到另一个函数或代码
- python - AttributeError:“NoneType”对象在使用 Google 翻译时没有属性“组”