Skip to content
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,36 @@ describe('SelectEventPlugin', () => {
expect(mouseup).toBe(null);
});

it('should extract if an `onSelect` listener is present', () => {
class WithSelect extends React.Component {
render() {
return <input type="text" onSelect={this.props.onSelect} />;
}
}

var cb = jest.fn();

var rendered = ReactTestUtils.renderIntoDocument(
<WithSelect onSelect={cb} />,
it('should register event only if the `onSelect` listener is present', () => {
var select = jest.fn();
var onSelect = event => {
expect(typeof event).toBe('object');
expect(event.type).toBe('select');
expect(event.target).toBe(node);
select(event.currentTarget);
};

var childContainer = document.createElement('div');
var childNode = ReactDOM.render(
<input type="text" onSelect={onSelect} />,
childContainer,
);
var node = ReactDOM.findDOMNode(rendered);
document.body.appendChild(childContainer);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you mind doing this in beforeEach and then cleaning up in afterEach? Like here.


node.selectionStart = 0;
node.selectionEnd = 0;
var node = ReactDOM.findDOMNode(childNode);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There's no need to findDOMNode. render() already gave you the right node. Old test just wasn't updated to reflect this.

node.focus();
expect(select.mock.calls.length).toBe(0);

var focus = extract(node, 'topFocus');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This seems to be testing that a focus DOM event would have been ignored at this point. Shouldn't the new test also verify this?

expect(focus).toBe(null);
var nativeEvent = document.createEvent('Event');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's use the newer API for this? e.g. new MouseEvent('mousedown', {bubbles: true, cancelable: true})

nativeEvent.initEvent('mousedown', true, true);
childNode.dispatchEvent(nativeEvent);
expect(select.mock.calls.length).toBe(0);

var mousedown = extract(node, 'topMouseDown');
expect(mousedown).toBe(null);
nativeEvent = document.createEvent('Event');
nativeEvent.initEvent('mouseup', true, true);
childNode.dispatchEvent(nativeEvent);
expect(select.mock.calls.length).toBe(1);

var mouseup = extract(node, 'topMouseUp');
expect(mouseup).not.toBe(null);
expect(typeof mouseup).toBe('object');
expect(mouseup.type).toBe('select');
expect(mouseup.target).toBe(node);
document.body.removeChild(childContainer);
});
});