Skip to content
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

Refactor SyntheticKeyboardEvent tests to only use the public API #11631

Merged
merged 18 commits into from
Nov 25, 2017

Conversation

aarboleda1
Copy link
Contributor

I've been working on SyntheticKeyboardEvent test and have gotten a significant amount of tests to pass but wanted to see if the community had feedback on my work so far and advice on how to complete the few remaining tests where I am stuck.

Problem: I am unable to fire events and pass tests that require a 'keypress' event. I've looked thru other examples, specifically mdn docs and PR #11365 to fire the keypress event, specifically on line 41.

Via MDN and other PR's, I've found no significant difference on why a 'keypress' event and and the 'keydown'/'keyup' which fire as expected. @gaearon any feedback or tips would be on my approach/code appreciated 😃

cancelable: true,
});
container.firstChild.dispatchEvent(nativeEvent);
expect(charCode).toBe(13);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I expect charCode at this point to be 13 but get this message.

image

I'm wondering if there is a difference between the keydown and keypress event that I am missing.

expect(syntheticEvent.target).toBe(target);
expect(syntheticEvent.type).toBe(undefined);
event = document.createEvent('Event');
event.initEvent('keypress', true, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The EventInterface, the keypress event also seems to be not firing in the same manner as keydown and keyup.

@gaearon gaearon mentioned this pull request Nov 24, 2017
26 tasks

beforeEach(() => {
// Mock getEventCharCode for proper unit testing
jest.mock('../getEventCharCode');
getEventCharCode = require('../getEventCharCode').default;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is also problematic. It's importing an internal module—which is exactly the situation we're trying to avoid.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In fact I think the tests from getEventCharCode should just be "rolled" into this test case. Since they're covering the same thing.

container = null;
});


describe('KeyboardEvent interface', () => {
describe('charCode', () => {
describe('when event is `keypress`', () => {
it('returns whatever getEventCharCode returns', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A lot of these tests were written in a very unit testing centric way. You need to try to liberate from that mindset. Think what's actually valuable to test for from perspective of library user. Not all the possible inputs to internal functions that never get called with those inputs in the first place.

Copy link
Collaborator

@gaearon gaearon left a comment

Choose a reason for hiding this comment

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

Using getEventCharCode is the wrong strategy here. Instead, if necessary, feel free to inline the assertions from getEventCharCode-test here, and actually test what they are doing together rather than that one calls the other.

@gaearon
Copy link
Collaborator

gaearon commented Nov 25, 2017

I did the changes. Hopefully this passes CI!

@gaearon gaearon merged commit d1cb28c into facebook:master Nov 25, 2017
@aarboleda1
Copy link
Contributor Author

Changes look awesome. Apologies for not being able to act faster - was busy with Thanksgiving/family obligations the past 2 days. Regardless, thank you for the feedback and the opportunity, @gaearon!

HeroProtagonist pushed a commit to HeroProtagonist/react that referenced this pull request Nov 26, 2017
…ebook#11631)

* KeyboardEvent interface-keypress

* Pass first 6 tests

* Roll getEventCharCode-test into SyntheticKeyboardEvent-test

* Run SyntheticKeyboardEvent-test on bundles

* Remove unused code
raphamorim pushed a commit to raphamorim/react that referenced this pull request Nov 27, 2017
…ebook#11631)

* KeyboardEvent interface-keypress

* Pass first 6 tests

* Roll getEventCharCode-test into SyntheticKeyboardEvent-test

* Run SyntheticKeyboardEvent-test on bundles

* Remove unused code
Ethan-Arrowood pushed a commit to Ethan-Arrowood/react that referenced this pull request Dec 8, 2017
…ebook#11631)

* KeyboardEvent interface-keypress

* Pass first 6 tests

* Roll getEventCharCode-test into SyntheticKeyboardEvent-test

* Run SyntheticKeyboardEvent-test on bundles

* Remove unused code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants