Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

reactjs - enzyme: find() formik components inside an anonymous function

I am using enzyme for testing react components. I have a formik form with this structure:

Form.js

export class Form extends Component {
    constructor(props) {
        //...
    }

    render() {
        <Formik
            inititalValues={{
                //...
            }}
            onSubmit={(values, { resetForm }) => {
                //...
                //...
            }}
            onReset = {(values, formProps) => {
                //...
            }}
            validationSchema={
                //...
            }>

            {
                (props) => {
                    const {values, errors, handleSubmit} = props;
                    return (
                        <form onSubmit={handleSubmit}>
                            <input type='text' name='email' />
                            <input type='password' name='password' />
                            <button type='submit'>Submit</button>
                        </form>
                    )
                }
            }

        </Formik>
    }
}

How do i use enzyme's find() method to get those input fileds?

I can get the main <Formik> component but can't get the input fields inside that anonymous function. Any help would be appriciated.

question from:https://stackoverflow.com/questions/65623777/enzyme-find-formik-components-inside-an-anonymous-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use dive() to expand the nested form.

shallow(<Form {...props} />)
  .find(Formik)
  .dive()

You can also trigger Formik props directly using prop(key)

shallow(<Form {...props} />)
  .find(Formik)
  .prop('onSubmit')(args)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...