11
12
const React = require('react');
13
const ReactDOM = require('react-dom');
14
+const findDOMNode =
15
+ ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.findDOMNode;
16
const StrictMode = React.StrictMode;
17
18
describe('findDOMNode', () => {
19
it('findDOMNode should return null if passed null', () => {
18
- expect(ReactDOM.findDOMNode(null)).toBe(null);
20
+ expect(findDOMNode(null)).toBe(null);
21
});
22
23
// @gate !disableLegacyMode
34
35
const container = document.createElement('div');
36
const myNode = ReactDOM.render(<MyNode />, container);
35
- const myDiv = ReactDOM.findDOMNode(myNode);
36
- const mySameDiv = ReactDOM.findDOMNode(myDiv);
37
+ const myDiv = findDOMNode(myNode);
38
+ const mySameDiv = findDOMNode(myDiv);
39
expect(myDiv.tagName).toBe('DIV');
40
expect(mySameDiv).toBe(myDiv);
41
});
57
const container = document.createElement('div');
58
59
const myNodeA = ReactDOM.render(<MyNode />, container);
58
- const a = ReactDOM.findDOMNode(myNodeA);
60
+ const a = findDOMNode(myNodeA);
61
expect(a).toBe(null);
62
63
const myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
64
expect(myNodeA === myNodeB).toBe(true);
65
64
- const b = ReactDOM.findDOMNode(myNodeB);
66
+ const b = findDOMNode(myNodeB);
67
expect(b.tagName).toBe('SPAN');
68
});
69
70
it('findDOMNode should reject random objects', () => {
71
expect(function () {
70
- ReactDOM.findDOMNode({foo: 'bar'});
72
+ findDOMNode({foo: 'bar'});
73
}).toThrowError('Argument appears to not be a ReactComponent. Keys: foo');
74
});
75
85
const inst = ReactDOM.render(<Foo />, container);
86
ReactDOM.unmountComponentAtNode(container);
87
86
- expect(() => ReactDOM.findDOMNode(inst)).toThrowError(
88
+ expect(() => findDOMNode(inst)).toThrowError(
89
'Unable to find node on an unmounted component.',
90
);
91
});
94
it('findDOMNode should not throw an error when called within a component that is not mounted', () => {
95
class Bar extends React.Component {
96
UNSAFE_componentWillMount() {
95
- expect(ReactDOM.findDOMNode(this)).toBeNull();
97
+ expect(findDOMNode(this)).toBeNull();
98
}
99
100
render() {
129
);
130
131
let match;
130
- expect(() => (match = ReactDOM.findDOMNode(parent))).toErrorDev([
132
+ expect(() => (match = findDOMNode(parent))).toErrorDev([
133
'Warning: findDOMNode is deprecated in StrictMode. ' +
134
'findDOMNode was passed an instance of ContainsStrictModeChild which renders StrictMode children. ' +
135
'Instead, add a ref directly to the element you want to reference. ' +
162
);
163
164
let match;
163
- expect(() => (match = ReactDOM.findDOMNode(parent))).toErrorDev([
165
+ expect(() => (match = findDOMNode(parent))).toErrorDev([
166
'Warning: findDOMNode is deprecated in StrictMode. ' +
167
'findDOMNode was passed an instance of IsInStrictMode which is inside StrictMode. ' +
168
'Instead, add a ref directly to the element you want to reference. ' +