-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Update getEventKey tests to use public API (#11299) #11317
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like the coverage is not as complete as it used to be. For example I can completely comment out this block and the test still passes, but it shouldn’t.
Thanks for the feedback @gaearon. I will investigate the coverage. I noticed that removing the |
I'm seeing that changing the to But now I'm not sure how this test was working correctly before. Actually, what I am seeing is that when using |
Hi @sebmarkbage, I saw your comment about not using I can get something like this to work for the first test: class Comp extends React.Component {
cb = (e) => {
this.key = e.key;
}
render() {
return <input ref={n => this.a = n} onKeyDown={this.cb} />;
}
}
var container = document.createElement('div');
var instance = ReactDOM.render(<Comp />, container);
document.body.appendChild(container);
var node = ReactDOM.findDOMNode(instance.a);
var event = document.createEvent('Event');
event.key = 'Del';
event.initEvent('keydown', true, true);
node.dispatchEvent(event);
expect(instance.key).toBe('Delete');
document.body.removeChild(container); Does this look like the right approach? One thing I'm seeing is that it doesn't capture in the var node = ReactDOM.findDOMNode(instance.a);
var nativeEvent = new KeyboardEvent('keydown', { key: 'Del' });
node.dispatchEvent(nativeEvent); but it looks like it never calls the If I add an an event listener to the node, I see it's being called: componentDidMount() {
ReactDOM.findDOMNode(this.a).addEventListener('keydown', e => console.log(e.key));
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this look like the right approach?
Yes. Please change tests to use this approach (with dispatching native events) instead. You can use #11299 as inspiration.
One thing I'm seeing is that it doesn't capture in the onKeyPress handler when swapping the type for keypress though.
This might be related to this check. And it is there to normalize browser behavior which is usually to only fire keypress
for printable characters (unlike Delete
button).
I don't think it's super important to test keypress
specifically. At least that Delete
case. It's not supposed to fire in browsers for Delete
.
It's nice to have at least a single test with it though. Maybe you can just test that {key: 'Whatever'}
turns into Unidentified
(which proves this code ran). For Delete
tests please use keydown
instead.
Thanks @gaearon! There is a test for returning Another thing, on MDN the docs say that class Comp extends React.Component {
cb = (e) => {
this.key = e.key;
}
render() {
return <input ref={n => this.a = n} onKeyDown={this.cb} />;
}
}
var container = document.createElement('div');
var instance = ReactDOM.render(<Comp />, container);
document.body.appendChild(container);
var node = ReactDOM.findDOMNode(instance.a);
var event = new Event('keydown', {bubbles: true, cancelable: true});
event.key = 'Del';
node.dispatchEvent(event);
expect(instance.key).toBe('Delete');
document.body.removeChild(container); If this looks ok, I have all of the tests switched over and working except for the last one as I'm not sure how to test it. This last test is testing unknown event types. The way these tests are now is testing event properties through the callback handlers, i.e. Specifically the last test is testing the last return state when the returned key is an empty string for an unknown event type. What I am not sure about is that React doesn't handle unknown event types on components, so how to test that this condition was met? Also, I'd like to test code coverage to make sure it's not regressed. Should the |
8295b03
to
295bc49
Compare
This sounds fine.
I think it's fine to remove that test if you add Flow typing to the file, with a |
Maybe. I don't know if that infrastructure works properly. Can you try and let me know? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please:
-
Remove usage of
Simulate
andSimulateNative
in favor of dispatching native events. You can use Use only public API for EnterLeaveEventPlugin Tests #11316 and Rewrite ReactDOMInput-test depending on internal API (#11299) #11309 as a reference. -
Delete the last test.
-
Instead, add
@flow
annotation togetEventKey
that verifies it always returns a string.
76f191d
to
534a30b
Compare
Hi @gaearon, thank you for your feedback. Here is the latest update for this PR:
One thing I ran into when adding I did not get a chance to look into coverage in more detail yet. |
Can we just use string keys? |
534a30b
to
a7fce0f
Compare
Yeah, string keys work. Updated with a2adbdf. |
This is great. Thank you! |
Thank you @gaearon! |
…#11317) * Add flow annotation to getEventKey. * Remove Simulate and SimulateNative for native events. * Style nits * Oops
Hi!
This PR is my attempt at updating the tests for
getEventKey.js
to use the public API. I browsed other test examples that were provided in #11299. After reading those andgetEventKey.js
I came up with an approach that usesSimulate
andSimulateNative
on the ref node and captures the synthetic event in theonKeyPressCapture
andonKeyPressCapture
handlers. It then tests properties on the event that was passed in.I had some questions along the way:
Simulate
/SimulateNative
correctly?Thank you for the contribution opportunity!
Cheers