【问题标题】:Jest and jquery-i18next without using react不使用反应的 Jest 和 jquery-i18next
【发布时间】:2019-03-21 12:54:43
【问题描述】:

我真的需要一些帮助来弄清楚为什么“.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() 似乎总是把事情搞砸。做什么?

【问题讨论】:

    标签: javascript internationalization jestjs i18next


    【解决方案1】:
    $.fn.extend({
     localize: jest.fn(),
     language: jest.fn(),
     i18n:{
     changeLanguage: jest.fn()
     }
    
    });
    

    想通了,真的很简单。你所要做的就是在 setupJest.js 中扩展 jQuery 的方法!! :)

    【讨论】:

      猜你喜欢
      • 2018-07-28
      • 2021-09-14
      • 2020-05-05
      • 1970-01-01
      • 2019-01-29
      • 2021-07-02
      • 2020-08-22
      • 2020-10-09
      • 2017-09-17
      相关资源
      最近更新 更多