Skip to content

Commit e5a8f23

Browse files
committed
[RFC] Update testing approach to verify exhibited behavior dependent upon methods in ReactDOMComponentTree
1 parent 846dce8 commit e5a8f23

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

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

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
describe('ReactDOMComponentTree', () => {
1313
let React;
1414
let ReactDOM;
15-
let AnotherReactDOM;
1615
let ReactDOMServer;
1716

1817
function renderMarkupIntoDocument(elt) {
@@ -22,10 +21,6 @@ describe('ReactDOMComponentTree', () => {
2221
return ReactDOM.hydrate(elt, container);
2322
}
2423

25-
function getTypeOf(instance) {
26-
return instance.type;
27-
}
28-
2924
function simulateInput(elem, value) {
3025
const inputEvent = new Event('input', {
3126
bubbles: true,
@@ -112,9 +107,9 @@ describe('ReactDOMComponentTree', () => {
112107
simulateClick(document.getElementById(elemID));
113108
});
114109

115-
it('finds instances for nodes when events happen', done => {
110+
it('finds a controlled instance from node and gets its current fiber props', done => {
116111
const inputID = 'inputID';
117-
const startValue = 'start';
112+
const startValue = undefined;
118113
const finishValue = 'finish';
119114

120115
class Controlled extends React.Component {
@@ -124,19 +119,35 @@ describe('ReactDOMComponentTree', () => {
124119
const node = e.currentTarget;
125120
expect(node.value).toEqual(finishValue);
126121
expect(node.id).toBe(inputID);
127-
done();
122+
spyOn(console, 'error');
123+
expectDev(console.error.calls.count()).toBe(0);
124+
this.setState(
125+
{
126+
value: node.value,
127+
},
128+
() => {
129+
expectDev(console.error.calls.count()).toBe(1);
130+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
131+
'Warning: A component is changing an uncontrolled input of ' +
132+
'type text to be controlled. Input elements should not ' +
133+
'switch from uncontrolled to controlled (or vice versa). ' +
134+
'Decide between using a controlled or uncontrolled input ' +
135+
'element for the lifetime of the component. More info: ' +
136+
'https://fb.me/react-controlled-components',
137+
);
138+
done();
139+
},
140+
);
128141
};
129142
render() {
130143
return (
131-
<div>
132-
<input
133-
id={inputID}
134-
type="text"
135-
ref={n => (this.a = n)}
136-
value={this.state.value}
137-
onChange={this._onChange}
138-
/>
139-
</div>
144+
<input
145+
id={inputID}
146+
type="text"
147+
ref={n => (this.a = n)}
148+
value={this.state.value}
149+
onChange={this._onChange}
150+
/>
140151
);
141152
}
142153
}
@@ -148,28 +159,34 @@ describe('ReactDOMComponentTree', () => {
148159
simulateInput(instance.a, finishValue);
149160
});
150161

151-
it('finds instance of node being unmounted', () => {
162+
it('finds instance of node that is attempted to be unmounted', () => {
152163
spyOn(console, 'error');
153-
const component = <div></div>;
164+
const component = <div />;
154165
const container = document.createElement('div');
155-
const instance = ReactDOM.render(component, container);
156-
ReactDOM.unmountComponentAtNode(container);
157-
expectDev(console.error.calls.count()).toBe(0);
166+
const node = ReactDOM.render(<div>{component}</div>, container);
167+
ReactDOM.unmountComponentAtNode(node);
168+
expectDev(console.error.calls.count()).toBe(1);
169+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
170+
"unmountComponentAtNode(): The node you're attempting to unmount " +
171+
'was rendered by React and is not a top-level container. You may ' +
172+
'have accidentally passed in a React root node instead of its ' +
173+
'container.',
174+
);
158175
});
159176

160177
it('finds instance from node to stop rendering over other react rendered components', () => {
161178
spyOn(console, 'error');
162179
const component = <div><span>Hello</span></div>;
163-
const anotherComponent = <div></div>;
180+
const anotherComponent = <div />;
164181
const container = document.createElement('div');
165182
const instance = ReactDOM.render(component, container);
166183
ReactDOM.render(anotherComponent, instance);
167184
expectDev(console.error.calls.count()).toBe(1);
168185
expectDev(console.error.calls.argsFor(0)[0]).toContain(
169186
'render(...): Replacing React-rendered children with a new root ' +
170-
'component. If you intended to update the children of this node, ' +
171-
'you should instead have the existing children update their state ' +
172-
'and render the new components instead of calling ReactDOM.render.'
187+
'component. If you intended to update the children of this node, ' +
188+
'you should instead have the existing children update their state ' +
189+
'and render the new components instead of calling ReactDOM.render.',
173190
);
174191
});
175192
});

0 commit comments

Comments
 (0)