Skip to content

Commit 846dce8

Browse files
committed
Remove testing of internals and add test cases for testing behavior exhibited after use of getInstanceFromNode
1 parent 2c317e8 commit 846dce8

File tree

1 file changed

+24
-56
lines changed

1 file changed

+24
-56
lines changed

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

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

1718
function renderMarkupIntoDocument(elt) {
@@ -25,23 +26,10 @@ describe('ReactDOMComponentTree', () => {
2526
return instance.type;
2627
}
2728

28-
function getInstanceFromNode(node) {
29-
const instanceKey = Object.keys(node).find(key =>
30-
key.startsWith('__reactInternalInstance$'),
31-
);
32-
return node[instanceKey];
33-
}
34-
35-
function getFiberPropsFromNode(node) {
36-
const props = Object.keys(node).find(key =>
37-
key.startsWith('__reactEventHandlers$'),
38-
);
39-
return node[props];
40-
}
41-
4229
function simulateInput(elem, value) {
43-
const inputEvent = document.createEvent('Event');
44-
inputEvent.initEvent('input', true, true);
30+
const inputEvent = new Event('input', {
31+
bubbles: true,
32+
});
4533
setUntrackedInputValue.call(elem, value);
4634
elem.dispatchEvent(inputEvent);
4735
}
@@ -103,9 +91,6 @@ describe('ReactDOMComponentTree', () => {
10391
id = 'closestInstance';
10492
_onClick = e => {
10593
const node = e.currentTarget;
106-
const instance = getInstanceFromNode(node);
107-
expect(instance).toBeDefined();
108-
expect(getTypeOf(instance)).toBe('div');
10994
expect(node.id).toBe(this.id);
11095
done();
11196
};
@@ -138,9 +123,6 @@ describe('ReactDOMComponentTree', () => {
138123
_onChange = e => {
139124
const node = e.currentTarget;
140125
expect(node.value).toEqual(finishValue);
141-
const instance = getInstanceFromNode(node);
142-
expect(instance).toBeDefined();
143-
expect(getTypeOf(instance)).toBe('input');
144126
expect(node.id).toBe(inputID);
145127
done();
146128
};
@@ -166,42 +148,28 @@ describe('ReactDOMComponentTree', () => {
166148
simulateInput(instance.a, finishValue);
167149
});
168150

169-
it('updates fiber props on changes', done => {
170-
const startValue = 'start';
171-
const finishValue = 'finish';
172-
173-
class AnotherControlled extends React.Component {
174-
state = {value: startValue};
175-
a = null;
176-
_onChange = e => {
177-
const node = e.currentTarget;
178-
const props = getFiberPropsFromNode(node);
179-
expect(props.value).toBe(startValue);
180-
expect(node.value).toEqual(finishValue);
181-
this.setState({value: e.currentTarget.value}, () => {
182-
const updatedProps = getFiberPropsFromNode(node);
183-
expect(updatedProps.value).toBe(finishValue);
184-
done();
185-
});
186-
};
187-
render() {
188-
return (
189-
<div>
190-
<input
191-
type="text"
192-
ref={n => (this.a = n)}
193-
value={this.state.value}
194-
onChange={this._onChange}
195-
/>
196-
</div>
197-
);
198-
}
199-
}
151+
it('finds instance of node being unmounted', () => {
152+
spyOn(console, 'error');
153+
const component = <div></div>;
154+
const container = document.createElement('div');
155+
const instance = ReactDOM.render(component, container);
156+
ReactDOM.unmountComponentAtNode(container);
157+
expectDev(console.error.calls.count()).toBe(0);
158+
});
200159

201-
const component = <AnotherControlled />;
160+
it('finds instance from node to stop rendering over other react rendered components', () => {
161+
spyOn(console, 'error');
162+
const component = <div><span>Hello</span></div>;
163+
const anotherComponent = <div></div>;
202164
const container = document.createElement('div');
203165
const instance = ReactDOM.render(component, container);
204-
document.body.appendChild(container);
205-
simulateInput(instance.a, finishValue);
166+
ReactDOM.render(anotherComponent, instance);
167+
expectDev(console.error.calls.count()).toBe(1);
168+
expectDev(console.error.calls.argsFor(0)[0]).toContain(
169+
'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.'
173+
);
206174
});
207175
});

0 commit comments

Comments
 (0)