Skip to content
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

Use only public API for ChangeEventPlugin-test.js #11333

Merged
merged 13 commits into from
Nov 24, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -11,108 +11,133 @@

var React = require('react');
var ReactDOM = require('react-dom');
var ReactTestUtils = require('react-dom/test-utils');
// TODO: can we express this test with only public API?
var ChangeEventPlugin = require('../ChangeEventPlugin').default;
var inputValueTracking = require('../../client/inputValueTracking');

function getTrackedValue(elem) {
var tracker = inputValueTracking._getTrackerFromNode(elem);
return tracker.getValue();
return elem.value;
}

function setTrackedValue(elem, value) {
var tracker = inputValueTracking._getTrackerFromNode(elem);
tracker.setValue(value);
}

function setUntrackedValue(elem, value) {
var tracker = inputValueTracking._getTrackerFromNode(elem);
var current = tracker.getValue();
var setUntrackedChecked = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'checked',
).set;

if (elem.type === 'checkbox' || elem.type === 'radio') {
elem.checked = value;
} else {
elem.value = value;
}
tracker.setValue(current);
}
var setUntrackedValue = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
).set;

describe('ChangeEventPlugin', () => {
var container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('should fire change for checkbox input', () => {
var called = 0;

function cb(e) {
called = 1;
called++;
expect(e.type).toBe('change');
}

var input = ReactTestUtils.renderIntoDocument(
var node = ReactDOM.render(
<input type="checkbox" onChange={cb} />,
container,
);

setUntrackedValue(input, true);
ReactTestUtils.SimulateNative.click(input);
node.dispatchEvent(new Event('click'));
expect(called).toBe(0);

setUntrackedChecked.call(node, false);
node.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
Copy link
Collaborator

@gaearon gaearon Nov 24, 2017

Choose a reason for hiding this comment

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

We should leave a comment about jsdom click misbehaving. It's not obvious and doesn't happen in the browser (it's also possible jsdom has fixed this in new versions so we need to make it clear in case somebody later tries to update it, and the test will fail).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you please elaborate on what a 'common' is?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

node.dispatchEvent(new Event('click'));
expect(called).toBe(0);

Is this what you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, autocorrect. Meant "we should leave a comment". Like in the sample I wrote above.

expect(called).toBe(1);
});

it('should catch setting the value programmatically', () => {
var input = ReactTestUtils.renderIntoDocument(
<input type="text" defaultValue="foo" />,
);
expect(node.checked).toBe(true);

input.value = 'bar';
expect(getTrackedValue(input)).toBe('bar');
setUntrackedChecked.call(node, true);
node.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
expect(called).toBe(2);
expect(node.checked).toBe(false);
});

it('should not fire change when setting the value programmatically', () => {
it('should not fire change setting the value programmatically', () => {
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

var input = ReactTestUtils.renderIntoDocument(
<input type="text" onChange={cb} defaultValue="foo" />,
var input = ReactDOM.render(
<input type="text" defaultValue="foo" onChange={cb} />,
container,
);

input.value = 'bar';
ReactTestUtils.SimulateNative.change(input);
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
expect(called).toBe(0);

setUntrackedValue(input, 'foo');
ReactTestUtils.SimulateNative.change(input);
setUntrackedValue.call(input, 'foo');
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));

expect(called).toBe(1);
expect(input.value).toBe('foo');

input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
expect(called).toBe(1);
});

it('should not fire change when setting checked programmatically', () => {
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

var input = ReactTestUtils.renderIntoDocument(
var input = ReactDOM.render(
<input type="checkbox" onChange={cb} defaultChecked={true} />,
container,
);

//set programmatically
input.checked = true;
ReactTestUtils.SimulateNative.click(input);

setUntrackedChecked.call(input, false);
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
expect(called).toBe(0);

input.checked = false;
setTrackedValue(input, undefined);
ReactTestUtils.SimulateNative.click(input);
setUntrackedChecked.call(input, true);
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));

expect(called).toBe(1);
});

it('should catch setting the value programmatically', () => {
var called = 0;

function cb(e) {
called++;
expect(e.type).toBe('change');
}

var input = ReactDOM.render(
<input type="text" onChange={cb} defaultValue="foo" />,
container,
);

setUntrackedValue.call(input, 'bar');
expect(called).toBe(0);
expect(getTrackedValue(input)).toBe('bar');
});

it('should unmount', () => {
var container = document.createElement('div');
var input = ReactDOM.render(<input />, container);

ReactDOM.unmountComponentAtNode(container);
Expand All @@ -122,78 +147,71 @@ describe('ChangeEventPlugin', () => {
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

var input = ReactTestUtils.renderIntoDocument(
var input = ReactDOM.render(
<input type="radio" onChange={cb} />,
container,
);
setUntrackedValue(input, true);
ReactTestUtils.SimulateNative.click(input);
ReactTestUtils.SimulateNative.click(input);

setUntrackedChecked.call(input, true);
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('click', {bubbles: true, cancelable: true}));
expect(called).toBe(1);
});

it('should deduplicate input value change events', () => {
var input;
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

[
<input type="text" onChange={cb} />,
<input type="number" onChange={cb} />,
<input type="range" onChange={cb} />,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like it was important to do this for all types of inputs, and not just 'text'?

].forEach(function(element) {
called = 0;
input = ReactTestUtils.renderIntoDocument(element);

setUntrackedValue(input, '40');
ReactTestUtils.SimulateNative.change(input);
ReactTestUtils.SimulateNative.change(input);
expect(called).toBe(1);

called = 0;
input = ReactTestUtils.renderIntoDocument(element);
setUntrackedValue(input, '40');
ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.input(input);
expect(called).toBe(1);

called = 0;
input = ReactTestUtils.renderIntoDocument(element);
setUntrackedValue(input, '40');
ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);
expect(called).toBe(1);
});
var input = ReactDOM.render(<input type="text" onChange={cb} />, container);

setUntrackedValue.call(input, 'foo');

input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

expect(called).toBe(1);

setUntrackedValue.call(input, 'bar');

input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));

expect(called).toBe(2);

setUntrackedValue.call(input, 'foobar');

input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

expect(called).toBe(3);
});

it('should listen for both change and input events when supported', () => {
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

if (!ChangeEventPlugin._isInputEventSupported) {
return;
}

var input = ReactTestUtils.renderIntoDocument(
var input = ReactDOM.render(
<input type="range" onChange={cb} />,
container,
);
setUntrackedValue(input, 'bar');

ReactTestUtils.SimulateNative.input(input);

setUntrackedValue(input, 'foo');
setUntrackedValue.call(input, '0');
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));

ReactTestUtils.SimulateNative.change(input);
setUntrackedValue.call(input, '1');
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

expect(called).toBe(2);
});
Expand All @@ -202,21 +220,22 @@ describe('ChangeEventPlugin', () => {
var called = 0;

function cb(e) {
called += 1;
called++;
expect(e.type).toBe('change');
}

var input = ReactTestUtils.renderIntoDocument(
var input = ReactDOM.render(
<input type="range" onChange={cb} />,
container,
);
setUntrackedValue(input, '40');
ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);
setUntrackedValue.call(input, '40');
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

setUntrackedValue(input, 'foo');
setUntrackedValue.call(input, 'foo');
input.dispatchEvent(new Event('input', {bubbles: true, cancelable: true}));
input.dispatchEvent(new Event('change', {bubbles: true, cancelable: true}));

ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);
expect(called).toBe(2);
});
});