如何使用 Jest 在 React Native 中测试屏幕道具

How to test screenprops in react native using Jest

我有一个 class 从我的 React Native 项目中的 Component 扩展而来。我在其他 classes render 中使用这个 class 并且我用它传递 screen props:

class OtherClass extend Component {

render() {
return (
         <MyCustomComponent screenprops ={{number : this.state.number}}
   })

}

现在我正在使用 Jest 编写测试。我能够在没有屏幕道具的情况下为组件 class 编写测试:

test('MyCustomComponent renders correctly', () => {
  renderer.create(this.MyCustomComponent);
});

但我不知道如何测试屏幕道具并在我的测试中使用它们。 我搜索了很多,但找不到解决方案。

你可以这样做:

test('renders correctly', () => {

  const propsLink = {
    screenProps: {
      arrayProp: [
        {
          v1: true,
          v2: "my value 1",
        },
        {
          v1: false,
          v2: "my value 2",
        }
      ],
      number: 1
    }
  }

  renderer.create(<MyCustomComponent {...propsLink} />)
});