forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfindDOMNode-test.js
More file actions
166 lines (142 loc) · 4.83 KB
/
findDOMNode-test.js
File metadata and controls
166 lines (142 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
const React = require('react');
const ReactDOM = require('react-dom');
const ReactTestUtils = require('react-dom/test-utils');
const StrictMode = React.StrictMode;
describe('findDOMNode', () => {
it('findDOMNode should return null if passed null', () => {
expect(ReactDOM.findDOMNode(null)).toBe(null);
});
it('findDOMNode should find dom element', () => {
class MyNode extends React.Component {
render() {
return (
<div>
<span>Noise</span>
</div>
);
}
}
const myNode = ReactTestUtils.renderIntoDocument(<MyNode />);
const myDiv = ReactDOM.findDOMNode(myNode);
const mySameDiv = ReactDOM.findDOMNode(myDiv);
expect(myDiv.tagName).toBe('DIV');
expect(mySameDiv).toBe(myDiv);
});
it('findDOMNode should find dom element after an update from null', () => {
function Bar({flag}) {
if (flag) {
return <span>A</span>;
}
return null;
}
class MyNode extends React.Component {
render() {
return <Bar flag={this.props.flag} />;
}
}
const container = document.createElement('div');
const myNodeA = ReactDOM.render(<MyNode />, container);
const a = ReactDOM.findDOMNode(myNodeA);
expect(a).toBe(null);
const myNodeB = ReactDOM.render(<MyNode flag={true} />, container);
expect(myNodeA === myNodeB).toBe(true);
const b = ReactDOM.findDOMNode(myNodeB);
expect(b.tagName).toBe('SPAN');
});
it('findDOMNode should reject random objects', () => {
expect(function() {
ReactDOM.findDOMNode({foo: 'bar'});
}).toThrowError('Argument appears to not be a ReactComponent. Keys: foo');
});
it('findDOMNode should reject unmounted objects with render func', () => {
class Foo extends React.Component {
render() {
return <div />;
}
}
const container = document.createElement('div');
const inst = ReactDOM.render(<Foo />, container);
ReactDOM.unmountComponentAtNode(container);
expect(() => ReactDOM.findDOMNode(inst)).toThrowError(
'Unable to find node on an unmounted component.',
);
});
it('findDOMNode should not throw an error when called within a component that is not mounted', () => {
class Bar extends React.Component {
UNSAFE_componentWillMount() {
expect(ReactDOM.findDOMNode(this)).toBeNull();
}
render() {
return <div />;
}
}
expect(() => ReactTestUtils.renderIntoDocument(<Bar />)).not.toThrow();
});
it('findDOMNode should warn if used to find a host component inside StrictMode', () => {
let parent = undefined;
let child = undefined;
class ContainsStrictModeChild extends React.Component {
render() {
return (
<StrictMode>
<div ref={n => (child = n)} />
</StrictMode>
);
}
}
ReactTestUtils.renderIntoDocument(
<ContainsStrictModeChild ref={n => (parent = n)} />,
);
let match;
expect(() => (match = ReactDOM.findDOMNode(parent))).toWarnDev([
'Warning: findDOMNode is deprecated in StrictMode. ' +
'findDOMNode was passed an instance of ContainsStrictModeChild which renders a StrictMode subtree. ' +
'The nearest child is in StrictMode. ' +
'Use an explicit ref directly on the element you want to get a handle on.' +
'\n' +
'\n in div (at **)' +
'\n in StrictMode (at **)' +
'\n in ContainsStrictModeChild (at **)' +
'\n' +
'\nLearn more about using refs safely here:' +
'\nhttps://fb.me/react-strict-mode-find-node',
]);
expect(match).toBe(child);
});
it('findDOMNode should warn if passed a component that is inside StrictMode', () => {
let parent = undefined;
let child = undefined;
class IsInStrictMode extends React.Component {
render() {
return <div ref={n => (child = n)} />;
}
}
ReactTestUtils.renderIntoDocument(
<StrictMode>
<IsInStrictMode ref={n => (parent = n)} />
</StrictMode>,
);
let match;
expect(() => (match = ReactDOM.findDOMNode(parent))).toWarnDev([
'Warning: findDOMNode is deprecated in StrictMode. ' +
'findDOMNode was passed an instance of IsInStrictMode which is in a StrictMode subtree. ' +
'Use an explicit ref directly on the element you want to get a handle on.' +
'\n' +
'\n in IsInStrictMode (at **)' +
'\n in StrictMode (at **)' +
'\n' +
'\nLearn more about using refs safely here:' +
'\nhttps://fb.me/react-strict-mode-find-node',
]);
expect(match).toBe(child);
});
});