这是测试:
describe('<HomeScreen />', () => {
it('Renders correctly', () => {
// const setOptions = jest.fn(); // 1
// const setOptions = (navigation: any, route: any) => { } // 2
// const setOptions = (props: HomeProps) => { } // 3
// const setOptions = (options: Partial<NativeStackNavigationOptions>) => void // 4
render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<HomeScreen navigation={{ setOptions: setOptions }} route={undefined} />
</PersistGate>
</Provider>,
);
});
});
我认为问题在于我使用的是 TypeScript 和 React Navigation v6,因为:
const setOptions = jest.fn();
我得到:Type '{ setOptions: jest.Mock<any, any>; }' is not assignable to type 'NativeStackNavigationProp<RootStackParamList, "Home">'.
NativeStackNavigationProp<RootStackParamList, "Home">
是我对组件的类型。就是这个:
export type HomeProps = NativeStackScreenProps<RootStackParamList, 'Home'>;
后来我导入为
HomeProps
const setOptions = (navigation: any, route: any) => { }
我得到:
Type '(navigation: any, route: any) => void' is not assignable to type '(options: Partial<NativeStackNavigationOptions>) => void'.
const setOptions = (props: HomeProps) => { }
我得到:
Type '(props: HomeProps) => void' is not assignable to type '(options: Partial<NativeStackNavigationOptions>) => void'.
const setOptions = (options: Partial<NativeStackNavigationOptions>) => void
(@captain-yossarian 的建议)
我得到:
Type '{ setOptions: (options: Partial<NativeStackNavigationOptions>) => any; }' is not assignable to type 'NativeStackNavigationProp<RootStackParamList, "Home">'.
我该如何解决这个问题?