typescript - 如何在使用 React Navigation 和 TypeScript 时使用 Jest 和 Enzyme 对静态 navigationOptions 进行单元测试?

标签 typescript unit-testing react-native jestjs enzyme

我正在使用 TypeScript 构建一个 React Native 应用程序。我正在使用 Jest 和 Enzyme 进行组件测试。我也在使用 React Navigation。

我的一个屏幕有以下 navigationOptions :

static navigationOptions = ({ navigation }: NavigationScreenProps) => ({
headerLeft: Platform.select({
  android: (
    <Icon
      name="md-menu"
      type="ionicon"
      containerStyle={styles.icon}
      onPress={navigation.toggleDrawer}
    />
  ),
  ios: null
}),
headerTitle: strings.homeTitle

});

我想编写单元测试以确保 <Icon />组件被渲染。类似于这里:

const props = createTestProps({});
const wrapper = shallow<HomeScreen>(<HomeScreen {...props} />);

it("should render a <View />", () => {
  expect(wrapper.find(View)).toHaveLength(1);
});

我的问题是,我想不出选择器来选择它。如何选择 static navigationOptions

我也试过像这样检查 wrapper :

expect(wrapper.instance().navigationOptions)

但是包装器的实例没有导航选项。

最佳答案

由于它是静态函数,您可以在不创建新实例的情况下调用它。

test('navigation options', () => {
  const naviProp = { navigation: { navigate: () => {} } };
  const navigationOptions = HomeScreen.navigationOptions(naviProp);

  expect(navigationOptions).toMatchSnapshot();
});

关于typescript - 如何在使用 React Navigation 和 TypeScript 时使用 Jest 和 Enzyme 对静态 navigationOptions 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52699142/

相关文章:

php - SOAP 客户端 TypeScript

javascript - NextJS - fatal error : Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

unit-testing - chalice 2.4.4 : How do mock a transient service inside a domain?

android - React Native 应用程序崩溃 : Unable to load script from assets 'index.android.bundle'

reactjs - 每次选择/取消选择一行时,如何避免 react 原生 FlatList 重新渲染

reactjs - 使用 NextJS 中间件重定向除特定页面外

javascript - angular 2 transient 提供者而不是单例

c# - 如何模拟 LINQ to Entities 助手,例如 'SqlFunctions.StringConvert()'

c++ - 谷歌在同一解决方案中测试多个 C++ 项目

javascript - npx babel 没有从 babel.config.js 读取配置