WIP: add setNativeValue for change event #153
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What: This overrides the
fireEvent.change
function to one that allows users to set the target.value using a specialsetNativeValue
function.Why: (#152) Because React tracks when you set the
value
property on an input to keep track of the node's value. When you dispatch achange
event, it checks it's lastvalue
against the current value and if they're the same it does not call any event handlers (as no change has taken place as far as react is concerned). So we have to set the value in a way that React's value setter function will not be called, which is where thesetNativeValue
comes into play.How: Development of the
setNativeValue
function was a team effort: facebook/react#10135 (comment)This is kinda cheating because it's overriding
fireEvent.change
. I think I'd rather we do this withindom-testing-library
directly.I'm also not super happy with the API:
It makes sense to me because in your
onChange
handler if you want the value you'll get it fromevent.target.value
, but I worry that this API will make people think they can change other things about thetarget
as well... I suppose we could make that work by Object.assigning alltarget
properties onto the givennode
(while still treating thevalue
property specially) 🤔Another thing I've considered is just accepting a
value
option:But I don't like that because the 2nd argument is supposed to be event init options... I'm open to feedback here.
Also, this PR is incomplete because we still have to handle
select
,mouseEnter
, andmouseLeave
. I'm not sure how to handle those. More investigation is needed.I'm thinking that we should make these changes in
dom-testing-library
instead of here and we can address each of these one-by-one.Checklist: