Skip to content

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

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

Merged
merged 13 commits into from
Nov 24, 2017
56 changes: 20 additions & 36 deletions packages/react-dom/src/events/__tests__/ChangeEventPlugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,20 @@
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');
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this will work since we stub this in the input tracking code...or is that only the instance property that's stubbed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the only prototype method I could find that would grant me access to this kind of info. If the value is untracked it won't show up in the Object.getOwnPropertyDescriptor() call. (Hence why I am using the .set method).


describe('ChangeEventPlugin', () => {
it('should fire change for checkbox input', () => {
Expand All @@ -51,7 +40,7 @@ describe('ChangeEventPlugin', () => {
<input type="checkbox" onChange={cb} />,
);

setUntrackedValue(input, true);
setUntrackedChecked.call(input, true);
ReactTestUtils.SimulateNative.click(input);

expect(called).toBe(1);
Expand Down Expand Up @@ -82,7 +71,7 @@ describe('ChangeEventPlugin', () => {
ReactTestUtils.SimulateNative.change(input);
expect(called).toBe(0);

setUntrackedValue(input, 'foo');
setUntrackedValue.call(input, 'foo');
ReactTestUtils.SimulateNative.change(input);

expect(called).toBe(1);
Expand All @@ -104,8 +93,7 @@ describe('ChangeEventPlugin', () => {
ReactTestUtils.SimulateNative.click(input);
expect(called).toBe(0);

input.checked = false;
setTrackedValue(input, undefined);
setUntrackedChecked.call(input, false);
ReactTestUtils.SimulateNative.click(input);

expect(called).toBe(1);
Expand All @@ -128,7 +116,7 @@ describe('ChangeEventPlugin', () => {
var input = ReactTestUtils.renderIntoDocument(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This won't work because it's not actually rendering into document (yes, the naming of that method is pretty bad). That's why reviews in other similar tests added setting up a container and adding it to the body before every test.

<input type="radio" onChange={cb} />,
);
setUntrackedValue(input, true);
setUntrackedChecked.call(input, true);
ReactTestUtils.SimulateNative.click(input);
ReactTestUtils.SimulateNative.click(input);
expect(called).toBe(1);
Expand All @@ -151,21 +139,21 @@ describe('ChangeEventPlugin', () => {
called = 0;
input = ReactTestUtils.renderIntoDocument(element);

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

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

called = 0;
input = ReactTestUtils.renderIntoDocument(element);
setUntrackedValue(input, '40');
setUntrackedValue.call(input, '40');
ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);
expect(called).toBe(1);
Expand All @@ -180,18 +168,14 @@ describe('ChangeEventPlugin', () => {
expect(e.type).toBe('change');
}

if (!ChangeEventPlugin._isInputEventSupported) {
return;
}

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

ReactTestUtils.SimulateNative.input(input);

setUntrackedValue(input, 'foo');
setUntrackedValue.call(input, 'foo');

ReactTestUtils.SimulateNative.change(input);

Expand All @@ -209,11 +193,11 @@ describe('ChangeEventPlugin', () => {
var input = ReactTestUtils.renderIntoDocument(
<input type="range" onChange={cb} />,
);
setUntrackedValue(input, '40');
setUntrackedValue.call(input, '40');
ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);

setUntrackedValue(input, 'foo');
setUntrackedValue.call(input, 'foo');

ReactTestUtils.SimulateNative.input(input);
ReactTestUtils.SimulateNative.change(input);
Expand Down