【发布时间】:2022-06-21 14:44:20
【问题描述】:
我正在尝试为我拥有 WebView 的组件创建单元测试。
问题是在这个组件内部我调用了 WebView 的 reload() 函数。
当我运行测试时,我得到了这个错误:
Invariant Violation: nodeHandle expected to be non-null
107 | useCallback(() => {
108 | if (navigation.getState().index === 0) {
> 109 | webviewRef.current.reload();
| ^
110 | }
111 | }, [webviewRef, navigation])
112 | );
我尝试按照我在 Jest 网站上找到的示例以这种方式模拟 reload() 函数:
jest.mock('react-native-webview', () => {
const RealComponent = jest.requireActual('react-native-webview');
RealComponent.reload = jest.fn();
return RealComponent;
});
但我收到了完全相同的错误消息。看来开玩笑没有选择我的模型。
如何模拟 WebView 的 reload() 函数?
【问题讨论】:
标签: react-native jestjs react-native-testing-library