Skip to content
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 77 additions & 32 deletions packages/react-dom/src/events/__tests__/SyntheticWheelEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
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.

container.dispatchEvent(event);

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(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.

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);
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.

});

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);
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.


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.

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);
});
});