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

Rewrite SyntheticWheelEvent-test depending on internal API (#11299) #11367

Merged
merged 14 commits into from
Nov 10, 2017
Merged

Rewrite SyntheticWheelEvent-test depending on internal API (#11299) #11367

merged 14 commits into from
Nov 10, 2017

Conversation

douglasgimli
Copy link
Contributor

Rewrite SyntheticWheelEvent-test to remove internal API dependencies based in issue #11299

@facebook-github-bot
Copy link

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@gaearon
Copy link
Collaborator

gaearon commented Oct 31, 2017

Can you please rebase on master, run yarn prettier, and commit?

@gaearon
Copy link
Collaborator

gaearon commented Oct 31, 2017

Hmm, something got messed up. Might be easier to start a new branch off master and cherry-pick your commits on it?

@douglasgimli
Copy link
Contributor Author

@gaearon done, can you take a look if everything is how expected?

@gaearon
Copy link
Collaborator

gaearon commented Oct 31, 2017

^^ see my reply above, we commented at the same time

@douglasgimli
Copy link
Contributor Author

douglasgimli commented Nov 4, 2017

@gaearon sorry about the late answer, I was busy with a few jobs. I've merged the current master into my branch and I've run the prettier script, you think this works as you expected?

var target = document.createElement('div');
var syntheticEvent = createEvent({srcElement: target});
const container = document.createElement('div');
const event = createMouseEvent('', {srcElement: container});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this doesn't test that srcElement is used when event.target is not supported. The code just falls back to event.target because it exists. You need to do extra work to actually "break" event.target and cause the polyfill path to execute.

which: 2,
button: 1,
});
expect(event.button).toBe(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't test the React's synthetic event at all. It just creates a jsdom event object and then asserts that object works. This is not a useful test for us because we already know jsdom works (it has its own test suite).

We should instead test that the synthetic event object produced by React has the right fields.

const node = ReactDOM.findDOMNode(component);

node.dispatchEvent(createMouseEvent('wheel', {deltaX: 10, deltaY: -50}));
expect(events[0].deltaX).toBe(10);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is better to first assert the number of expected events. Then, if some are missing, the errors are clearer.

const component = ReactDOM.render(<div onWheel={onWheel} />, container);
document.body.appendChild(container);

const node = ReactDOM.findDOMNode(component);
Copy link
Collaborator

Choose a reason for hiding this comment

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

ReactDOM.render already returns a DOM node. There is no need to call findDOMNode on a DOM node.


expect(syntheticEvent.target).toBe(target);
expect(syntheticEvent.type).toBe(undefined);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It wouldn't be undefined in practice. The old test was odd, but we shouldn't replicate that odd behavior in the new test :-)


expect(syntheticEvent.target).toBe(target);
expect(syntheticEvent.type).toBe(undefined);
expect(event.target).toBe(container);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This does not test the React synthetic event system. Like below, the problem is this is testing jsdom itself. If React were to break the synthetic events completely, this test would still be happily passing.

@gaearon
Copy link
Collaborator

gaearon commented Nov 10, 2017

I pushed changes to address my review comments. I hope they are helpful for future reference!

@gaearon gaearon merged commit b4b09cb into facebook:master Nov 10, 2017
@gaearon gaearon mentioned this pull request Nov 20, 2017
26 tasks
Ethan-Arrowood pushed a commit to Ethan-Arrowood/react that referenced this pull request Dec 8, 2017
…11299) (facebook#11367)

* Update SyntheticWheelEvent tests to use public API

* Replace: ReactTestUtils.SimulateNative to native Events()

* Update: Replaced WheelEvent() interface to document.createEvent

* Fix: Lint SyntheticWheelEvent file

* Update: Custom WheelEvent function to a generic MouseEvent function

* Update: Prettier SyntheticWheelEvent-test.js

* Verify the `button` property is set on synthetic event

* Use MouseEvent constructor over custom helper

* Rewrite to test React rather than jsdom

* Force the .srcElement code path to execute

* Style tweaks and slight code reorganization
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