-
Notifications
You must be signed in to change notification settings - Fork 50k
(WIP) Use only public api for SyntheticEvent-test.js #11332
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
Conversation
|
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! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
aaed094 to
baaf16f
Compare
baaf16f to
9453a9d
Compare
|
I've updated this branch removing the I can't remove the |
| cancelable: true, | ||
| ...nativeEvent, | ||
| }; | ||
| var event = new Event([eventType, defaultNativeEvent]); |
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.
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.
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.
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); |
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.
instance is the DOM element here. You can remove findDOMNodes.
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.
thanks
| var event = createEvent('click', {srcElement: instance}); | ||
| var elem = ReactDOM.findDOMNode(instance); | ||
| elem.dispatchEvent(event); | ||
| expect(click).toBeCalledWith(elem); |
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.
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(); |
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.
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); |
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 we lost these two assertions on e.defaultPrevented and e.nativeEvent.returnValue? Why?
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.
@gaearon there is no value on e.nativeEvent.returnValue, I think it's not testable.
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.
what do you think?
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.
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}); |
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.
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); |
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.
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}); |
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.
Same re: srcElement.
| expect(syntheticEvent.isPersistent()).toBe(false); | ||
| syntheticEvent.persist(); | ||
| expect(syntheticEvent.isPersistent()).toBe(true); | ||
| var event = createEvent('click', {srcElement: instance}); |
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.
Same.
7d6f12e to
0a68476
Compare
|
Looks like tests are still failing. Do you plan to continue working on this? |
|
@gaearon yes, I've planned to continue it today and tomorrow. |
|
Friendly ping :-) |
|
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}); |
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.
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); |
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.
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.
|
I have submitted a PR in #11525 |
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 😄