Skip to content

Add note about triggering onChange with a different value #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2018
Merged

Add note about triggering onChange with a different value #120

merged 2 commits into from
Jun 20, 2018

Conversation

silvenon
Copy link
Contributor

@silvenon silvenon commented Jun 20, 2018

What: I added an explanation and an example how to trigger the onChange handler with fireEvent:

const comment = getByLabelText('Comment')
comment.value = 'Great advice, I love your posts!'
fireEvent.change(comment)

Why: I didn't know how to do this because I was so used to Simulate, it took me a while to figure it out. I'm not 100% sure if this is the "right" way to do it, someone please correct me if it's not.

Checklist:

  • Documentation
  • Tests N/A
  • Ready to be merged
  • Added myself to contributors table

This PR references #66.

Copy link
Member

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

@kentcdodds kentcdodds merged commit b904e14 into testing-library:master Jun 20, 2018
@silvenon silvenon deleted the fireEvent-change-value branch June 20, 2018 20:05
@kentcdodds
Copy link
Member

🎉 This PR is included in version 4.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kelly-tock
Copy link

kelly-tock commented Jul 16, 2018

found this after trying to figure out a form test, I noticed when you fire change, only the target property of event is fired and not currentTarget,

example:

<input onChange={this.handleChange} value={value} data-testid="input" />

const input = getByTestId('input');

 input.value = 'testing';

 fireEvent.change(
    input
 );

in the change handler, target is defined but not current target.

any ideas?

@kelly-tock
Copy link

also as an example, if you're using flowtype or typescript,

handleChange(event: React.SyntheticEvent<HTMLInputElement>) {
    const value = event.currentTarget.value;

works, but target does not.

@kentcdodds
Copy link
Member

That could be a bug with dom-testing-library. Care to file that?

@kelly-tock
Copy link

cool, i'll file it.

@kelly-tock
Copy link

julienw pushed a commit to julienw/react-testing-library that referenced this pull request Dec 20, 2018
Fixes testing-library#120 

<!--
Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated!

Please make sure that you are familiar with and follow the Code of Conduct for
this project (found in the CODE_OF_CONDUCT.md file).

Also, please make sure you're familiar with and follow the instructions in the
contributing guidelines (found in the CONTRIBUTING.md file).

If you're new to contributing to open source projects, you might find this free
video course helpful: http://kcd.im/pull-request

Please fill out the information below to expedite the review and (hopefully)
merge of your pull request!
-->

<!-- What changes are being made? (What feature/bug is being fixed here?) -->

**What**:

<!-- Why are these changes necessary? -->

**Why**:

<!-- How were these changes implemented? -->

**How**:

<!-- Have you done all of these things?  -->

**Checklist**:

<!-- add "N/A" to the end of each line that's irrelevant to your changes -->

<!-- to check an item, place an "x" in the box like so: "- [x] Documentation" -->

- [ ] Documentation
- [ ] Tests
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->
- [ ] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions -->

<!-- feel free to add additional comments -->
lucbpz pushed a commit to lucbpz/react-testing-library that referenced this pull request Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants