Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Testing React-form-hook for Select in Enzyme and jest with Controller API

I want to test my below Select and simulate on change event. but currently the onchange is not working. On selecting certain option from the dropwdown, I wan to show a Input field but I am unable to simulate onchange event. Everything is working fine in the JSX file. but not getting simulated properly in the Test File.

    const accType = watch('accountType');
        <Controller
                control={control}
                name="accountType"
                label="Account Type"
                rules={{ required: true }}
                render={({ onChange, ref }) => (
                  <Select
                    onChange={(e) => onChange(e.target.value)}
                    className={styles.w100}
                    defaultValue={bankAccountTypes[0]}
                  >
                    {bankAccountTypes.map((type) => (
                      <MenuItem key={type} value={type}>
                        {type}
                      </MenuItem>
                    ))}
                  </Select>
                )}
              />

{
          accType === 'NRO Savings Account' ? (
            <Grid item={true} xs={12} className={`${styles.fieldItem}`}>
              <TextField
                InputLabelProps={{ shrink: true }}
                className={styles.w100}
                type="text"
                name="country"
                required={true}
                label="Country"
                inputRef={register({ required: true })}
              />
            </Grid>
          ) : null
        }

Below is my Test file

// Country Field should be hidden
let country = component.find({ name: 'country' });
expect(country).toHaveLength(0);

// Simulate Account type field to display Country Dropdown
await act(async () => {
  accountType.simulate('change', { value: 'NRO Savings Account' });
});

// Country field should be present now
country = component.find({ name: 'country' });
expect(country).toHaveLength(1);

//Expected length: 1
//Received length: 0

I am using Jest and Enzyme here in React

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!