-
Notifications
You must be signed in to change notification settings - Fork 49.9k
Rewrite SyntheticWheelEvent-test depending on internal API (#11299) #11367
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
Changes from 8 commits
de9137f
317ace4
c44cb12
adf9a08
831e6c6
b7a9c16
9057a70
7e29643
c68d100
33568c3
6ae4d10
e382359
b76762f
3846542
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,61 +9,106 @@ | |
|
|
||
| 'use strict'; | ||
|
|
||
| var SyntheticWheelEvent; | ||
|
|
||
| describe('SyntheticWheelEvent', () => { | ||
| var createEvent; | ||
| var React; | ||
| var ReactDOM; | ||
| var createMouseEvent; | ||
|
|
||
| beforeEach(() => { | ||
| // TODO: can we express this test with only public API? | ||
| SyntheticWheelEvent = require('../SyntheticWheelEvent').default; | ||
|
|
||
| createEvent = function(nativeEvent) { | ||
| var target = require('../getEventTarget').default(nativeEvent); | ||
| return SyntheticWheelEvent.getPooled({}, '', nativeEvent, target); | ||
| React = require('react'); | ||
| ReactDOM = require('react-dom'); | ||
| createMouseEvent = (type = '', options = {}) => { | ||
| const event = document.createEvent('MouseEvent'); | ||
| event.initEvent(type, true, false); | ||
| return Object.assign(event, options); | ||
| }; | ||
| }); | ||
|
|
||
| it('should normalize properties from the Event interface', () => { | ||
| var target = document.createElement('div'); | ||
| var syntheticEvent = createEvent({srcElement: target}); | ||
| const container = document.createElement('div'); | ||
| const event = createMouseEvent('', {srcElement: container}); | ||
| container.dispatchEvent(event); | ||
|
|
||
| expect(syntheticEvent.target).toBe(target); | ||
| expect(syntheticEvent.type).toBe(undefined); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(event.target).toBe(container); | ||
|
||
| expect(event.type).toBe(''); | ||
| }); | ||
|
|
||
| it('should normalize properties from the MouseEvent interface', () => { | ||
| expect(createEvent({which: 2, button: 1}).button).toBe(1); | ||
| const event = new MouseEvent('', { | ||
| bubbles: true, | ||
| cancelable: false, | ||
| which: 2, | ||
| button: 1, | ||
| }); | ||
| expect(event.button).toBe(1); | ||
|
||
| }); | ||
|
|
||
| it('should normalize properties from the WheelEvent interface', () => { | ||
| var standardEvent = createEvent({deltaX: 10, deltaY: -50}); | ||
| expect(standardEvent.deltaX).toBe(10); | ||
| expect(standardEvent.deltaY).toBe(-50); | ||
| var events = []; | ||
| const container = document.createElement('div'); | ||
| var onWheel = event => { | ||
| event.persist(); | ||
| events.push(event); | ||
| }; | ||
| const component = ReactDOM.render(<div onWheel={onWheel} />, container); | ||
| document.body.appendChild(container); | ||
|
|
||
| const node = ReactDOM.findDOMNode(component); | ||
|
||
|
|
||
| node.dispatchEvent(createMouseEvent('wheel', {deltaX: 10, deltaY: -50})); | ||
| expect(events[0].deltaX).toBe(10); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| expect(events[0].deltaY).toBe(-50); | ||
|
|
||
| node.dispatchEvent( | ||
| createMouseEvent('wheel', {wheelDeltaX: -10, wheelDeltaY: 50}), | ||
| ); | ||
| expect(events[1].deltaX).toBe(10); | ||
| expect(events[1].deltaY).toBe(-50); | ||
|
|
||
| var webkitEvent = createEvent({wheelDeltaX: -10, wheelDeltaY: 50}); | ||
| expect(webkitEvent.deltaX).toBe(10); | ||
| expect(webkitEvent.deltaY).toBe(-50); | ||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| it('should be able to `preventDefault` and `stopPropagation`', () => { | ||
| var nativeEvent = {}; | ||
| var syntheticEvent = createEvent(nativeEvent); | ||
| var events = []; | ||
| const container = document.createElement('div'); | ||
| var onWheel = event => { | ||
| event.persist(); | ||
| events.push(event); | ||
| }; | ||
| const component = ReactDOM.render(<div onWheel={onWheel} />, container); | ||
| document.body.appendChild(container); | ||
|
|
||
| const node = ReactDOM.findDOMNode(component); | ||
|
|
||
| node.dispatchEvent(createMouseEvent('wheel')); | ||
| expect(events[0].isDefaultPrevented()).toBe(false); | ||
| events[0].preventDefault(); | ||
| expect(events[0].isDefaultPrevented()).toBe(true); | ||
|
|
||
| expect(syntheticEvent.isDefaultPrevented()).toBe(false); | ||
| syntheticEvent.preventDefault(); | ||
| expect(syntheticEvent.isDefaultPrevented()).toBe(true); | ||
| node.dispatchEvent(createMouseEvent('wheel')); | ||
| expect(events[1].isPropagationStopped()).toBe(false); | ||
| events[1].stopPropagation(); | ||
| expect(events[1].isPropagationStopped()).toBe(true); | ||
|
|
||
| expect(syntheticEvent.isPropagationStopped()).toBe(false); | ||
| syntheticEvent.stopPropagation(); | ||
| expect(syntheticEvent.isPropagationStopped()).toBe(true); | ||
| document.body.removeChild(container); | ||
| }); | ||
|
|
||
| it('should be able to `persist`', () => { | ||
| var syntheticEvent = createEvent({}); | ||
| var events = []; | ||
| const container = document.createElement('div'); | ||
| var onWheel = event => { | ||
| events.push(event); | ||
| }; | ||
| const component = ReactDOM.render(<div onWheel={onWheel} />, container); | ||
| document.body.appendChild(container); | ||
|
|
||
| const node = ReactDOM.findDOMNode(component); | ||
|
|
||
| node.dispatchEvent(createMouseEvent('wheel')); | ||
| expect(events[0].isPersistent()).toBe(false); | ||
| events[0].persist(); | ||
| expect(events[0].isPersistent()).toBe(true); | ||
|
|
||
| expect(syntheticEvent.isPersistent()).toBe(false); | ||
| syntheticEvent.persist(); | ||
| expect(syntheticEvent.isPersistent()).toBe(true); | ||
| document.body.removeChild(container); | ||
| }); | ||
| }); | ||
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 think this doesn't test that
srcElementis used whenevent.targetis not supported. The code just falls back toevent.targetbecause it exists. You need to do extra work to actually "break"event.targetand cause the polyfill path to execute.