Enzyme: component.find(...).first(...).simulate is not a function

Created on 26 Nov 2017  路  7Comments  路  Source: enzymejs/enzyme

I'm trying to set up a test on a component, where there is an onClick handler on a div with a list-group-item class. Following the examples on the readme, I tried this:

import { render, shallow } from 'enzyme';
const event = {...};
const org = {...};

let component;
let signUpFor = jest.fn();
beforeEach(() => {
    component = render(
        <Event header="foo"
               {...event}
               signUpFor={signUpFor}
               reload={true}
               databaseOrg={org}/>
    );
});

it('calls signUpFor on click', () => {
    component.find('.card>.list-group>.list-group-item').first().simulate('click');
    expect(signUpFor).toHaveBeenCalled();
});

However, this results in an error message:

screen shot 2017-11-26 at 1 47 11 pm

I've tried various permutations of line 3 in the above code snippet, among them:

  1. component.find(...selector...).simulate(...)
  2. component.find(...selector...).first().simulate(...)
  3. component.find(...selector...).at(0).simulate(...)

None of which have worked. Is there something I'm missing?

render question

Most helpful comment

OK, so, render doesn't have simulate - only shallow and mount do.

All 7 comments

I'm confused, in your example, why would signUpFor, which is never passed anywhere in your test, be called by simulate?

How is component created? It kind of suggests you used render, which is cheerio and thus doesn't have simulate at all.

Oh, sorry. I'll add the surrounding code to the issue. I assumed people would just assume that I've constructed a component.

Yes, but enzyme itself provides three APIs for doing so, and the way one does that (including which props you pass) are often critical to solving the issue.

Yes. I updated the code snippet above. Apparently I was using cheerio.

OK, so, render doesn't have simulate - only shallow and mount do.

Got it. Thanks!

@ljharb Your comment helped me a lot. I have been confused why the console always throws error of simulate is not a function. Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aweary picture aweary  路  3Comments

andrewhl picture andrewhl  路  3Comments

heikkimu picture heikkimu  路  3Comments

timhonders picture timhonders  路  3Comments

AdamYahid picture AdamYahid  路  3Comments