Testing TextInput Component in react-native

后端 未结 3 1344
感情败类
感情败类 2021-01-19 06:26

I have some problems with testing TextInput changes in react-native with jest and enzyme.

My component that handles user input basi

3条回答
  •  醉梦人生
    2021-01-19 07:06

    There is no need to add another library. Jest and Enzyme can perform the required testing. Below is the definition of SearchBar component which receives a function as a prop. The function is called with the text typed.

    const SearchBar = ({onSearch}) => {
      return (
        
           onSearch(text)}
          />
        
      );
    };
    

    The testing can be carried as follows

        const onSearch = jest.fn();
        const wrapper = shallow();
        wrapper.find('TextInput').simulate('changeText', 'test search text');
        expect(onSearch).toHaveBeenCalledWith('test search text');
    

提交回复
热议问题