forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex7.html
More file actions
140 lines (117 loc) · 3.59 KB
/
index7.html
File metadata and controls
140 lines (117 loc) · 3.59 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<!-- <script src="./test/react.js"></script>
<script src="./test/react-dom.js"></script>-->
<script type='text/javascript' src="./lib/ReactTestUtils.js"></script>
<script type='text/javascript' src="./lib/babel.js"></script>
<script type='text/babel'>
var s
window.onload = function(){
class ClickCounter extends React.Component {
state = {count: this.props.initialCount};
triggerReset = () => {
this.setState({count: this.props.initialCount});
};
handleClick = () => {
this.setState({count: this.state.count + 1});
};
render() {
var children = [];
var i;
console.log(this.state.count)
for (i = 0; i < this.state.count; i++) {
children.push(
<div
className="clickLogDiv"
key={'clickLog' + i}
ref={'clickLog' + i}
/>,
);
}
return (
<span className="clickIncrementer" onClick={this.handleClick}>
{children}
</span>
);
}
}
/**
* Only purpose is to test that refs are tracked even when applied to a
* component that is injected down several layers. Ref systems are difficult to
* build in such a way that ownership is maintained in an airtight manner.
*/
class GeneralContainerComponent extends React.Component {
render() {
return <div>{this.props.children}</div>;
}
}
/**
* Notice how refs ownership is maintained even when injecting a component
* into a different parent.
*/
class TestRefsComponent extends React.Component {
doReset = () => {
this.refs.myCounter.triggerReset();
};
render() {
return (
<div>
<div ref="resetDiv" onClick={this.doReset}>
Reset Me By Clicking This.
</div>
<GeneralContainerComponent ref="myContainer">
<ClickCounter ref="myCounter" initialCount={1} />
</GeneralContainerComponent>
</div>
);
}
}
/**
* Render a TestRefsComponent and ensure that the main refs are wired up.
*/
var renderTestRefsComponent = function() {
var testRefsComponent = ReactTestUtils.renderIntoDocument(
<TestRefsComponent />, document.body
);
console.log(testRefsComponent instanceof TestRefsComponent);
var generalContainer = testRefsComponent.refs.myContainer;
console.log(generalContainer instanceof GeneralContainerComponent)
var counter = testRefsComponent.refs.myCounter;
console.log(counter instanceof ClickCounter)
return testRefsComponent;
};
var expectClickLogsLengthToBe = function(instance, length) {
var clickLogs = ReactTestUtils.scryRenderedDOMComponentsWithClass(
instance,
'clickLogDiv',
);
console.log(clickLogs.length === length, length);
console.log(Object.keys(instance.refs.myCounter.refs).length === length, length, '!!!',
instance.refs.myCounter.refs
);
};
var testRefsComponent = renderTestRefsComponent();
var clickIncrementer = ReactTestUtils.findRenderedDOMComponentWithClass(
testRefsComponent,
'clickIncrementer',
);
expectClickLogsLengthToBe(testRefsComponent, 1);
// After clicking the reset, there should still only be one click log ref.
ReactTestUtils.Simulate.click(testRefsComponent.refs.resetDiv);
expectClickLogsLengthToBe(testRefsComponent, 1);
ReactTestUtils.Simulate.click(clickIncrementer);
expectClickLogsLengthToBe(testRefsComponent, 2);
}
</script>
</head>
<body>
<div>开发者工具</div>
<pre>
</pre>
<div id='example'></div>
</body>
</html>