-
Notifications
You must be signed in to change notification settings - Fork 49k
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
Changes from 4 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 hidden or 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 |
---|---|---|
|
@@ -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; | ||
|
||
describe('ChangeEventPlugin', () => { | ||
it('should fire change for checkbox input', () => { | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
@@ -128,7 +116,7 @@ describe('ChangeEventPlugin', () => { | |
var input = ReactTestUtils.renderIntoDocument( | ||
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. 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); | ||
|
@@ -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); | ||
|
@@ -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); | ||
|
||
|
@@ -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); | ||
|
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.
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?
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.
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).