Skip to content

Commit 55b3172

Browse files
SadPandaBeargaearon
authored andcommitted
Rewrite ReactDOMInput-test depending on internal API (#11299) (#11309)
* Rewrite tests depending on internal API * Remove focus being called when there was no blur event function before * Remove triggering function and just let ReactTestUtils take care * Use native events * Remove duplicate * Simulate native event when changing value on reentrant * Change wasn't being called * Use Simulate only * Use React event handlers on test * Move commentary * Lint commit * Use native event * Comment native event dispatching * Prettier * add setUntrackedValue * Prettier-all * Use dispatchEvent instead of ReactTestUtils Simulates; * Prettier * Fix lint * Remove useless arg * Wrap event dispatcher into function * Remove deprecated Event * Remove unused change event dispatcher * Fix merge * Prettier * Add missing focus/blur calls They are necessary to cover for the fix in #8240.
1 parent f751bd8 commit 55b3172

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

packages/react-dom/src/__tests__/ReactDOMInput-test.js

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,28 @@ describe('ReactDOMInput', () => {
1616
var ReactDOM;
1717
var ReactDOMServer;
1818
var ReactTestUtils;
19-
var inputValueTracking;
19+
var setUntrackedValue;
2020

2121
function normalizeCodeLocInfo(str) {
2222
return str && str.replace(/\(at .+?:\d+\)/g, '(at **)');
2323
}
2424

25-
function setUntrackedValue(elem, value) {
26-
var tracker = inputValueTracking._getTrackerFromNode(elem);
27-
var current = tracker.getValue();
28-
elem.value = value;
29-
tracker.setValue(current);
25+
function dispatchEventOnNode(node, type) {
26+
node.dispatchEvent(new Event(type, {bubbles: true, cancelable: true}));
3027
}
3128

3229
beforeEach(() => {
3330
jest.resetModules();
31+
32+
setUntrackedValue = Object.getOwnPropertyDescriptor(
33+
HTMLInputElement.prototype,
34+
'value',
35+
).set;
36+
3437
React = require('react');
3538
ReactDOM = require('react-dom');
3639
ReactDOMServer = require('react-dom/server');
3740
ReactTestUtils = require('react-dom/test-utils');
38-
// TODO: can we express this test with only public API?
39-
inputValueTracking = require('../client/inputValueTracking');
4041
spyOn(console, 'error');
4142
});
4243

@@ -49,30 +50,18 @@ describe('ReactDOMInput', () => {
4950
var node = ReactDOM.findDOMNode(stub);
5051
expectDev(console.error.calls.count()).toBe(1);
5152

52-
// Simulate a native change event
53-
setUntrackedValue(node, 'giraffe');
53+
setUntrackedValue.call(node, 'giraffe');
5454

5555
// This must use the native event dispatching. If we simulate, we will
5656
// bypass the lazy event attachment system so we won't actually test this.
57-
var nativeEvent = document.createEvent('Event');
58-
nativeEvent.initEvent('change', true, true);
59-
node.dispatchEvent(nativeEvent);
57+
dispatchEventOnNode(node, 'change');
6058

6159
expect(node.value).toBe('lion');
6260

6361
document.body.removeChild(container);
6462
});
6563

6664
it('should control a value in reentrant events', () => {
67-
// This must use the native event dispatching. If we simulate, we will
68-
// bypass the lazy event attachment system so we won't actually test this.
69-
var inputEvent = document.createEvent('Event');
70-
inputEvent.initEvent('input', true, true);
71-
// This must use the native event dispatching. If we simulate, we will
72-
// bypass the lazy event attachment system so we won't actually test this.
73-
var changeEvent = document.createEvent('Event');
74-
changeEvent.initEvent('change', true, true);
75-
7665
class ControlledInputs extends React.Component {
7766
state = {value: 'lion'};
7867
a = null;
@@ -82,8 +71,8 @@ describe('ReactDOMInput', () => {
8271
this.setState({value: newValue});
8372
// Calling focus here will blur the text box which causes a native
8473
// change event. Ideally we shouldn't have to fire this ourselves.
85-
// I don't know how to simulate a change event on a text box.
86-
this.a.dispatchEvent(changeEvent);
74+
// Don't remove unless you've verified the fix in #8240 is still covered.
75+
dispatchEventOnNode(this.a, 'change');
8776
this.b.focus();
8877
}
8978
blur(currentValue) {
@@ -114,11 +103,14 @@ describe('ReactDOMInput', () => {
114103
// We need it to be in the body to test native event dispatching.
115104
document.body.appendChild(container);
116105

106+
// Focus the field so we can later blur it.
107+
// Don't remove unless you've verified the fix in #8240 is still covered.
117108
instance.a.focus();
118-
// Simulate a native keyup event
119-
setUntrackedValue(instance.a, 'giraffe');
120-
121-
instance.a.dispatchEvent(inputEvent);
109+
setUntrackedValue.call(instance.a, 'giraffe');
110+
// This must use the native event dispatching. If we simulate, we will
111+
// bypass the lazy event attachment system so we won't actually test this.
112+
dispatchEventOnNode(instance.a, 'change');
113+
dispatchEventOnNode(instance.a, 'blur');
122114

123115
expect(instance.a.value).toBe('giraffe');
124116
expect(instance.switchedFocus).toBe(true);
@@ -127,11 +119,6 @@ describe('ReactDOMInput', () => {
127119
});
128120

129121
it('should control values in reentrant events with different targets', () => {
130-
// This must use the native event dispatching. If we simulate, we will
131-
// bypass the lazy event attachment system so we won't actually test this.
132-
var inputEvent = document.createEvent('Event');
133-
inputEvent.initEvent('input', true, true);
134-
135122
class ControlledInputs extends React.Component {
136123
state = {value: 'lion'};
137124
a = null;
@@ -164,11 +151,10 @@ describe('ReactDOMInput', () => {
164151
// We need it to be in the body to test native event dispatching.
165152
document.body.appendChild(container);
166153

167-
// Simulate a native keyup event
168-
setUntrackedValue(instance.a, 'giraffe');
169-
instance.a.dispatchEvent(inputEvent);
170-
171-
// These should now both have been restored to their controlled value.
154+
setUntrackedValue.call(instance.a, 'giraffe');
155+
// This must use the native event dispatching. If we simulate, we will
156+
// bypass the lazy event attachment system so we won't actually test this.
157+
dispatchEventOnNode(instance.a, 'input');
172158

173159
expect(instance.a.value).toBe('lion');
174160
expect(instance.b.checked).toBe(true);
@@ -604,7 +590,7 @@ describe('ReactDOMInput', () => {
604590
var container = document.createElement('div');
605591
var node = ReactDOM.render(stub, container);
606592

607-
setUntrackedValue(node, 'giraffe');
593+
setUntrackedValue.call(node, 'giraffe');
608594

609595
var fakeNativeEvent = function() {};
610596
fakeNativeEvent.target = node;

0 commit comments

Comments
 (0)