首页 > 解决方案 > 如何模拟反应原生 I18nManager

问题描述

我正在尝试在本机反应中运行笑话案例。我正在使用react-native-router-flux进行导航。我的测试用例因此错误而失败

Test suite failed to run

    TypeError: Cannot read property 'isRTL' of undefined

      at Object.<anonymous> (node_modules/react-native-router-flux/dist/NavBar.js:286:44)
      at Object.<anonymous> (node_modules/react-native-router-flux/dist/navigationStore.js:7:13)
      at Object.<anonymous> (node_modules/react-native-router-flux/dist/Reducer.js:74:36)

确切的线是transform:[{scaleX:_reactNative.I18nManager.isRTL?-1:1}]})

我尝试了不同的方法来模拟但无法得到它

一种方法是

import {I18nManager} from 'react-native'

I18nManager = {
    isRTL : false
}

我把这个片段放在开玩笑的初始配置文件中,但我得到了类似的错误I18nManager is readonly

标签: react-nativemockingjestjsreact-native-router-flux

解决方案


实际上,我使用的是正在运行的显式模块工厂,而不是使用 Jest 的自动模拟功能react-native-mock-render

像这样

jest.mock('react-native', () => require('react-native-mock-render'), {
  virtual: true
})

当我浏览这个库代码时,我在模拟中找不到 I18nManager。所以我分叉了那个 repo 并自己添加了 I18nManager 文件。


推荐阅读