简体   繁体   English

如何在Redux连接的组件的函数内部模拟onPress事件

[英]How to simulate onPress event inside a function for redux connected component

How to simulate the onPress event using jest inside TouchableOpacity which is wrapped inside a function? 如何使用包装在函数内的TouchableOpacity中的笑话模拟onPress事件? I tried by mocking the onpress event inside the component Issue came like Method “simulate” is meant to be run on 1 node. 我通过模拟组件内部的onpress事件进行了尝试,就像方法“ simulate”打算在1个节点上运行一样。 0 found instead. 找到0个。

 it('should call _renderItem', () => {
      const mockFunc = jest.fn();
      const wrapper = shallow(
        <test onPress={mockFunc} navigation={navigation} store={store} />)
      wrapper.dive().find('TouchableOpacity').simulate('press');
      expect(wrapper).toMatchSnapshot();
    });
   class test extends PureComponent{

    renderlists(data){
    const press = () => {

      }
       return(
           <Fragment>
              <TouchableOpacity onPress = {press}> //simulate this onpress event
                 {data.list}
              </TouchableOpacity>
           </Fragment>
             )
    }

    render(){
    return(
    <FlatList
         data = {this.state.data}
         renderItem={this.renderlists}
    />
    )}
    }

    const mapStateToProps = (state) => {
        let { Reducer} = state;
        return {
            Reducer
        }
    };

    export default connect(mapStateToProps)(test );
 i tried like that its throwing issue like

Issue : Method “simulate” is meant to be run on 1 node. 0 found instead.

Try importing TouchableOpacity into your test file and then: 尝试将TouchableOpacity导入测试文件,然后:

const wrapper = shallow(<YourComponent />).find(TouchableOpacity);
wrapper.simulate('press');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM