I'm trying to create a unit test for a component in which I have a WebView.
The problem is that inside this component I call the reload()
function of the WebView.
And when I run the test I get this error:
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 | );
I tried to mockup the reload()
function following an example that I've found on Jest site in this way:
jest.mock('react-native-webview', () => {
const RealComponent = jest.requireActual('react-native-webview');
RealComponent.reload = jest.fn();
return RealComponent;
});
But I'm getting the exact same error message. It seems that jest isn't picking my mockup.
How can I mockup the reload()
function of WebView?
1条答案
按热度按时间enxuqcxy1#
您可以尝试创建一个
react-native-webview
的节点模块模拟。假设组件
TestComponent.tsx
:在
__mocks__/react-native-webview.tsx
中,我们可以得到:此组件的测试如下所示: