-
Notifications
You must be signed in to change notification settings - Fork 273
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
feature: fireEvent
should provide generated event object
#714
Comments
This would be very useful. It's possible to do a partial workaround by passing an additional arg to fireEvent(button, 'press', {});
expect(typeof onPress.mock.calls[0][1]).toBe(typeof 'object'); ...which is better than nothing, but is a flawed approach overall which can give false positives. For example, const onChangeText = jest.fn()
const { getByA11yLabel } = render(<TextInput accessibilityLabel="test" onChangeText={onChangeText} />)
fireEvent.changeText(getByA11yLabel('test'), 'some text', { test: 'event' })
// This passes, but real-life onChangeText doesn't get any extra args beyond the string
expect(onChangeText).toHaveBeenLastCalledWith('some text', { test: 'event' }) It'd be better for |
It seems that RTL We should match that if feasible. |
Yes, I ran into the same problem a while ago, where reading some properties from an event handler broke my tests since I was not passing a fake event. Finding smart values is not trivial, but it's definitely worth pursuing. |
I am hoping to be able to implement this feature. I have a hackathon on Oct 13 where I'm hoping to make significant progress on this. It's possible my time will be pulled away with some workshops, though, so if someone else would like to work on this feature, don't let me stop you 👍 I will post updates here. |
@CodingItWrong looking forward to your PR. If you make a meaningful partial progress, do not hesitate to submit a draft PR. |
In a hackathon today we ended up exploring how the arguments to callbacks should work. We haven’t looked into the structure of the fake event so far. I’ve pushed up a draft PR that shows the cases we covered, and the form the code took so far. This investigation uncovered two questions that need to be answered to land this work: 1. In What Scenarios to Pass the Generated Event ObjectWhich types of event should pass a generated event object to the handler and which should not? It seems that Even if we had an “allowlist” of event types that should receive the generated event object, there is the risk that someone would add a custom event to their component that matches the name of a built-in event type. Maybe the allowlist should to specify both the events and the component types; that should allow it to correctly simulate the cases where RN generates events. 2. Whether to Handle Multiple Event Handler ArgumentsCurrently the API allows an arbitrary number of arguments to be passed to In all the examples I could find, events receive just one argument. Are there built-in RN events that receive multiple arguments? It seems like at least custom events should be able to receive multiple arguments, because again they can be called in arbitrary ways. But if we are already differentiating custom and built-in events due to question 1 above, maybe it is more realistic to not pass along multiple arguments for built-in events. |
@CodingItWrong thanks for taking care of this issue. Couple of key points I've noticed while reviewing your code and researching RTL/DTL implementation here
Answering your questions:
|
Seems like DTL has pretty good docs: We might also want to expose |
@CodingItWrong It seems like progress on this issue has staled, so I plan to start working on it. |
fireEvent
should provide generated event object
fireEvent
should provide generated event objectfireEvent
should provide generated event object
Closing in favor of upcoming User Event feature. |
Describe the Feature
If I understand the behavior correctly, fireEven looks for properties like "onPress" / "onScroll" in the DOM and calls these functions, but does not create an event object. This is a strong difference from the actual behavior of the application.
I understand the potential difficulties in creating these objects in nodejs environment, but it is possible for example to create objects at least partially similar to real events. It's also possible to document which event properties are relevant and which are not.
Even the very fact that a callback will receive an object, will be closer to real situations.
Possible Implementations
Problem example
The text was updated successfully, but these errors were encountered: