【问题标题】:How to test screenprops in react native using Jest如何使用 Jest 在 React Native 中测试 screenprops
【发布时间】:2019-12-30 13:31:44
【问题描述】:

我的 React Native 项目中有一个从 Component 扩展而来的类。我在其他类 render 中使用这个类,并通过它传递 screen props

class OtherClass extend Component {

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

}

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

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

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

【问题讨论】:

    标签: unit-testing react-native jestjs testng react-props


    【解决方案1】:

    你可以这样做:

    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} />)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 2022-06-11
      • 2023-04-11
      • 2021-12-30
      • 2018-02-09
      • 2018-01-29
      • 1970-01-01
      相关资源
      最近更新 更多