Skip to content

Conversation

@dleitee
Copy link

@dleitee dleitee commented Oct 23, 2017

Related to #11299 – Express more tests via public API

Hey!! I'm not sure if my code is correct, but I would like to know if I can continue in this way or there is something that I need change.

Thanks 😄

@facebook-github-bot
Copy link
Contributor

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 cla@fb.com. Thanks!

@facebook-github-bot
Copy link
Contributor

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

@dleitee dleitee force-pushed the synthetic-event-public-api branch from aaed094 to baaf16f Compare October 23, 2017 16:22
@dleitee dleitee force-pushed the synthetic-event-public-api branch from baaf16f to 9453a9d Compare October 23, 2017 16:42
@dleitee
Copy link
Author

dleitee commented Oct 23, 2017

I've updated this branch removing the ReactTestUtils.SimulateNative.
Are there some problems to use the Event class?

I can't remove the ReactTestUtils.SimulateNative here (https://github.com/facebook/react/pull/11332/files#diff-288fef2ade58f58c8d1d12571c4c80beR87) because these params doesn't exist on EventImpl from jsdom.

cancelable: true,
...nativeEvent,
};
var event = new Event([eventType, defaultNativeEvent]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is there an array here? Shouldn't it be just new Event(eventType, defaultNativeEvent)?

It would be nice to rename defaultNativeEvent and nativeEvent to eventInit and defaultEventInit, to match how spec calls these arguments.

Copy link
Author

Choose a reason for hiding this comment

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

Why is there an array here? Shouldn't it be just new Event(eventType, defaultNativeEvent)?

I was using arrays because I've seen the Event's constructor on jsdom and the first argument is args and it is an array: https://github.com/tmpvar/jsdom/blob/2d51af302581a57ee5b9b65595f1714d669b7ea2/lib/jsdom/living/events/Event-impl.js#L7

but no problem to change it :D

It would be nice to rename defaultNativeEvent and nativeEvent to eventInit and defaultEventInit, to match how spec calls these arguments.

Ok

document.body.appendChild(container);

var event = createEvent('click', {srcElement: instance});
var elem = ReactDOM.findDOMNode(instance);
Copy link
Collaborator

Choose a reason for hiding this comment

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

instance is the DOM element here. You can remove findDOMNodes.

Copy link
Author

Choose a reason for hiding this comment

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

thanks

var event = createEvent('click', {srcElement: instance});
var elem = ReactDOM.findDOMNode(instance);
elem.dispatchEvent(event);
expect(click).toBeCalledWith(elem);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this test works. If I comment out || nativeEvent.srcElement in getEventTarget.js the test still passes, whereas on master commenting it out fails the test. This means that dispatchEvent() probably fills .target it on the native event and thus it's not a problem.

I'm not sure how to work around that really. Maybe there's no way.

it('should be able to `preventDefault`', () => {
var nativeEvent = {};
var syntheticEvent = createEvent(nativeEvent);
var click = jest.fn();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we call this mock? It's a bit confusing to see click() calls like we're clicking on something.

expect(syntheticEvent.defaultPrevented).toBe(true);
var instance = ReactDOM.render(<div onClick={onClick} />, container);

expect(nativeEvent.returnValue).toBe(false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like we lost these two assertions on e.defaultPrevented and e.nativeEvent.returnValue? Why?

Copy link
Author

Choose a reason for hiding this comment

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

@gaearon there is no value on e.nativeEvent.returnValue, I think it's not testable.

Copy link
Author

Choose a reason for hiding this comment

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

what do you think?

Copy link
Author

Choose a reason for hiding this comment

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

could I use it: ReactTestUtils.SimulateNative.click(instance, {returnValue: false});

expect(nativeEvent.returnValue).toBe(false);
document.body.appendChild(container);

var event = createEvent('click', {srcElement: instance});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Only the first test was checking for srcElement. There shouldn't be a need to add this to every test.


var instance = ReactDOM.render(<div onClick={onClick} />, container);

expect(nativeEvent.cancelBubble).toBe(true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see this being tested (e.nativeEvent.cancelBubble)

expect(nativeEvent.cancelBubble).toBe(true);
document.body.appendChild(container);

var event = createEvent('click', {srcElement: instance});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same re: srcElement.

expect(syntheticEvent.isPersistent()).toBe(false);
syntheticEvent.persist();
expect(syntheticEvent.isPersistent()).toBe(true);
var event = createEvent('click', {srcElement: instance});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same.

@gaearon gaearon mentioned this pull request Oct 26, 2017
26 tasks
@dleitee dleitee force-pushed the synthetic-event-public-api branch from 7d6f12e to 0a68476 Compare October 26, 2017 22:02
@gaearon
Copy link
Collaborator

gaearon commented Oct 31, 2017

Looks like tests are still failing. Do you plan to continue working on this?

@dleitee
Copy link
Author

dleitee commented Oct 31, 2017

@gaearon yes, I've planned to continue it today and tomorrow.

@gaearon
Copy link
Collaborator

gaearon commented Nov 3, 2017

Friendly ping :-)

@gaearon
Copy link
Collaborator

gaearon commented Nov 8, 2017

Second ping. If you're too busy, it's okay—just let us know and somebody can take it from here.


it('should normalize `target` from the nativeEvent', () => {
var target = document.createElement('div');
var syntheticEvent = createEvent({srcElement: target});
Copy link
Collaborator

@gaearon gaearon Nov 10, 2017

Choose a reason for hiding this comment

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

In #11332 (comment), I didn't ask to remove srcElement from all tests like you did in 3b3db3f.

I was saying that only one test intentionally tested for srcElement support (that's why the test existed). That's why after refactoring, there should also be one test that verifies this.

This test verified that this line can fall back to nativeEvent.srcElement. After the changes, the new test doesn't check this.

expect(nativeEvent.returnValue).toBe(false);
var event = createEvent('click');
instance.dispatchEvent(event);
expect(click.mock.calls[0][0]).toBe(false);
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 not testing anything in React. You are creating a jsdom event (equivalent to a browser event) and then check that jsdom was dispatched. Even if the entire React source code was deleted, this test would still pass.

@gaearon
Copy link
Collaborator

gaearon commented Nov 10, 2017

For the reference, #11365 and #11299 might help demonstrate the suggested approach for these kinds of tests.

@timjacobi
Copy link
Member

timjacobi commented Nov 11, 2017

I have submitted a PR in #11525

@gaearon
Copy link
Collaborator

gaearon commented Nov 11, 2017

@dleitee

Thank you for your help! This PR is getting stale and since you're not responding, we're going to continue with #11525 instead. I appreciate your effort and hope you can contribute in the future!

@gaearon gaearon closed this Nov 11, 2017
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.

4 participants