Reactjs 用Jest测试onChange事件

Reactjs 用Jest测试onChange事件,reactjs,react-native,jestjs,enzyme,react-hooks,Reactjs,React Native,Jestjs,Enzyme,React Hooks,根据我的代码覆盖率,我需要测试在我的onChange事件中调用的函数。这实际上就是我使用useStatehook更新功能组件状态的地方 以下是我的组件: const Component:React.FC<{}> = () => { const {value, setState} = useState(''); return( <View> <CustomComponent onChange={(value) =&g

根据我的代码覆盖率,我需要测试在我的
onChange
事件中调用的函数。这实际上就是我使用
useState
hook更新功能组件状态的地方

以下是我的组件:

const Component:React.FC<{}> = () => {
  const {value, setState} = useState('');
  return(
    <View>
      <CustomComponent 
        onChange={(value) => setState(value)}
      />
    </View>
  )
}

必须涵盖/之间的代码。我真的不明白我怎么能报道这件事。第一个想法是使用mock函数,但我似乎找不到如何将其模拟到
onChange
事件,因为我没有将任何内容作为道具传递给主组件。

经过几次测试,我终于明白覆盖率不是要求对onChange函数进行实际测试,而是要求实际评估的值。因此,以下是我正在做的:

  • 获取TextInput子组件
  • 更改其文本
  • 评估它呈现的内容
  • 我在这里使用@testing library/react native,因为它使选择树组件变得更容易,例如使用accessibilityLabel(它实际上使我了解了该道具的重要性)。
    以下是测试的外观:

    describe('Testing useState functions', () => {
        test('test', () => {
          //Rendering the component and its tree
          const { container, getByLabelText } = render(<SignupView />);
          //Extracting the child, username_input component with his accessibilityLabel
          const username_input = getByLabelText('username_input');
          const email_input = getByLabelText('email_input');
          //Fire a native changeText event with a specific value
          fireEvent.changeText(username_input, 'doe');
          fireEvent.changeText(email_input, 'doe@joe.com');
          //Checking the rendered value 
          expect(username_input.props.value).toEqual('doe');
          expect(email_input.props.value).toEqual('doe@joe.com');
        });
      });
    
    description('测试useState函数',()=>{
    测试('test',()=>{
    //渲染组件及其树
    const{container,getByLabelText}=render();
    //提取子用户名\输入组件及其accessibilityLabel
    const username_input=getByLabelText('username_input');
    const email_input=getByLabelText(“email_input”);
    //激发具有特定值的本机changeText事件
    changeText(用户名输入'doe');
    fireEvent.changeText(电子邮件输入,'doe@joe.com');
    //检查渲染值
    expect(username\u input.props.value).toEqual('doe');
    expect(email\u input.props.value).toEqual('doe@joe.com');
    });
    });
    
    describe('Testing useState functions', () => {
        test('test', () => {
          //Rendering the component and its tree
          const { container, getByLabelText } = render(<SignupView />);
          //Extracting the child, username_input component with his accessibilityLabel
          const username_input = getByLabelText('username_input');
          const email_input = getByLabelText('email_input');
          //Fire a native changeText event with a specific value
          fireEvent.changeText(username_input, 'doe');
          fireEvent.changeText(email_input, 'doe@joe.com');
          //Checking the rendered value 
          expect(username_input.props.value).toEqual('doe');
          expect(email_input.props.value).toEqual('doe@joe.com');
        });
      });