-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
29253ec
Use only public API for ChangeEventPlugin-test.js
Ethan-Arrowood df01d2b
precommit commands complete
Ethan-Arrowood c7777f9
Removed comments
Ethan-Arrowood b6283f0
Merge branch 'master' into open-test-to-api
gaearon 27ec33f
Merge branch 'master' into open-test-to-api
jquense bff4f3b
Improving event dispatchers
Ethan-Arrowood 8be2a24
Merge remote-tracking branch 'upstream/master' into open-test-to-api
Ethan-Arrowood 054c955
Updated tests
Ethan-Arrowood 6238df3
Fixed for revisions
Ethan-Arrowood 852e663
Prettified
Ethan-Arrowood 7bb2124
Add more details and fixes to tests
gaearon 0570084
Not internal anymore
gaearon d34a205
Remove unused code
gaearon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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})); | ||
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); | ||
|
@@ -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} />, | ||
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. Seems like it was important to do this for all types of inputs, and not just |
||
].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); | ||
}); | ||
|
@@ -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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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).
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.
Can you please elaborate on what a 'common' is?
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.
Is this what you mean?
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.
Sorry, autocorrect. Meant "we should leave a comment". Like in the sample I wrote above.