# Ionic React: How to Test IonSearchBar onChange Listener

**URL:** <https://forum.ionicframework.com/t/ionic-react-how-to-test-ionsearchbar-onchange-listener/184802>\
**Category:** Ionic React\
**Created:** [March 9, 2020, 5:45pm UTC](https://forum.ionicframework.com/t/ionic-react-how-to-test-ionsearchbar-onchange-listener/184802 "2020-03-09T17:45:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph-freeman](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-freeman](https://forum.ionicframework.com/u/joseph-freeman)\
**Post date:** [March 9, 2020, 5:45pm UTC](https://forum.ionicframework.com/t/ionic-react-how-to-test-ionsearchbar-onchange-listener/184802/1 "2020-03-09T17:45:12Z")

</div>

I am trying to right a Jest and Enzyme test to for an IonSearchBar implementation. I can’t seem to simulate the onChange event with Enzyme. Is it possible to test the IonSearchBar in this manner?

example,

```auto
const ShallowSearchPage = shallow(<SearchPage/>);

it ('', (should fire on onChange event) => {
    let searchBar = ShallowSearchPage.find('IonSearchbar');
    searchBar.simulate('change', { target: { value: 'joseph' } });
}) 

```

The above fails with the following error:

`Method “simulate” is meant to be run on 1 node. 0 found instead.`

I am using Jest and Enzyme for testing. Any help would be most appreciated.

UPDATE:

Turns out that you can’t use Enzyme to target the component element by the tag name, in this case “IonSearchbar”. Instead add an id to the IonSearchbar to your jsx template.

example.

```auto
<IonSearchbar id="search-bar">

```

Then with enzyme target the id instead of the tag.

example:

```auto
let searchBar = ShallowSearchPage.find('#search-bar');

```

UPDATE:

Know my issue is actually getting the change event to fire when attempting to simulate late it. The following does not work:

```auto
searchBar.simulate('change');
searchBar.simulate('onchange');
searchBar.simulate('onionchange');

```

Any help figuring this out would be most appreciated.

---

<div class="post-metadata">

**Author:** ![joseph-freeman](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@joseph-freeman](https://forum.ionicframework.com/u/joseph-freeman)\
**Post date:** [March 16, 2020, 2:28pm UTC](https://forum.ionicframework.com/t/ionic-react-how-to-test-ionsearchbar-onchange-listener/184802/2 "2020-03-16T14:28:51Z")

</div>

I am trying to right a Jest and Enzyme test to for an IonSearchBar implementation. I can’t seem to simulate the onChange event with Enzyme. Is it possible to test the IonSearchBar in this manner?

example,

```auto
const ShallowSearchPage = shallow(<SearchPage/>);

it ('', (should fire on onChange event) => {
    let searchBar = ShallowSearchPage.find('IonSearchbar');
    searchBar.simulate('change', { target: { value: 'joseph' } });
}) 

```

The above fails with the following error:

`Method “simulate” is meant to be run on 1 node. 0 found instead.`

I am using Jest and Enzyme for testing. Any help would be most appreciated.

UPDATE:

Turns out that you can’t use Enzyme to target the component element by the tag name, in this case “IonSearchbar”. Instead add an id to the IonSearchbar to your jsx template.

example.

```auto
<IonSearchbar id="search-bar">

```

Then with enzyme target the id instead of the tag.

example:

```auto
let searchBar = ShallowSearchPage.find('#search-bar');

```

UPDATE:

Know my issue is actually getting the change event to fire when attempting to simulate late it. The following does not work:

```auto
searchBar.simulate('change');
searchBar.simulate('onchange');
searchBar.simulate('onionchange');

```

Any help figuring this out would be most appreciated.

UPDATE:

After much reading through the Ionic docs, the IonSearchbar 6 events listed on the doc page  
`https://ionicframework.com/docs/api/searchbar`

Using the `ionChange` event in the `.simulate` method works. I think the web docs could use some testing examples that give simple examples on how to test Ionic proprietary components.
