react-native - 如何模拟反应原生 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
解决方案
实际上,我使用的是正在运行的显式模块工厂,而不是使用 Jest 的自动模拟功能react-native-mock-render。
像这样
jest.mock('react-native', () => require('react-native-mock-render'), {
virtual: true
})
当我浏览这个库代码时,我在模拟中找不到 I18nManager。所以我分叉了那个 repo 并自己添加了 I18nManager 文件。
推荐阅读
- python - Looking to send Python API request using cURL
- java - 搜索后接口返回null
- r - 功能回归问题的数据生成
- iis - 用于 Visual FoxPro 的 Microsoft OLE DB 提供程序:路径或文件名无效
- visual-studio - 在客户端计算机上加载报告失败 - 在开发系统上工作
- javascript - 通过使用 es6 获取数组实例来延迟
- svelte - 带有参数的按钮单击处理程序
- html - 如何通过Django中的超链接将一个页面链接到另一个页面?
- openssl - 在 libssl 1.0.2e 或 1.0.2g 中找不到 SSL_set_tlsext_host_name API
- bash - 查找子字符串中数字最大的命令输出