Estoy tratando de implementar un WebView parcialmente simulado para probar en React Native. Intenté configurar un espía en el componente WebView solo anulando el accesorio de JavaScript injectedJavaScript . Hubiera asumido que esto funciona:
import WebView, * as RNWebView from 'react-native-webview'; // keep all props from the WebView componen, but override the injectedJavaScript prop jest .spyOn(RNWebView, 'WebView') .mockImplementation( (props: IOSWebViewProps & AndroidWebViewProps, context: any) => { return ( <WebView {...props} injectedJavaScript={`window.ReactNativeWebView.postMessage("CUSTOM_MESSAGE")`} /> ); }, );Pero me sale el error de tipo:
Argument of type '(props: IOSWebViewProps & AndroidWebViewProps, context: any) => JSX.Element' is not assignable to parameter of type '(props: IOSWebViewProps & AndroidWebViewProps, context: any) => WebView<unknown>'. Type 'ReactElement<any, any>' is missing the following properties from type 'WebView<unknown>': goBack, goForward, reload, stopLoading, and 9 more.ts(2345) no lo entiendo ¿Cómo se reconoce el <WebView> que devuelvo solo como tipo ReactElement<any, any> . ¿No debería el tipo de <WebView> ser literalmente WebView ? Realmente perdido aquí, no estoy seguro de por qué se queja TypeScript. Si agrego as any al final de la declaración de return , TypeScript está satisfecho:
jest .spyOn(RNWebView, 'WebView') .mockImplementation( (props: IOSWebViewProps & AndroidWebViewProps, context: any) => { return ( <WebView {...props} injectedJavaScript={`window.ReactNativeWebView.postMessage("CUSTOM_MESSAGE")`} /> ) as any; }, );Pero esto es un truco y quiero saber por qué TypeScript no entiende mis tipos simulados.