forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex8.html
More file actions
153 lines (126 loc) · 3.89 KB
/
index8.html
File metadata and controls
153 lines (126 loc) · 3.89 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
<!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="./lib/ReactTestUtils.js"></script>
<!--<script src="./test/react.js"></script>
<script src="./test/react-dom.js"></script>-->
<script src="./lib/babel.js"></script>
<script type='text/babel'>
var s, testRefsComponent
window.onload = function(){
var expect = function(a) {
return {
toBe: function(b) {
console.log(a, b, a === b)
}
}
}
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;
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 = () => {
console.log("triggerReset")
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 />,
);
expect(testRefsComponent instanceof TestRefsComponent).toBe(true);
var generalContainer = testRefsComponent.refs.myContainer;
expect(generalContainer instanceof GeneralContainerComponent).toBe(true);
var counter = testRefsComponent.refs.myCounter;
expect(counter instanceof ClickCounter).toBe(true);
return testRefsComponent;
};
var expectClickLogsLengthToBe = function(instance, length) {
var clickLogs = ReactTestUtils.scryRenderedDOMComponentsWithClass(
instance,
'clickLogDiv',
);
expect(clickLogs.length).toBe(length);
expect(Object.keys(instance.refs.myCounter.refs).length).toBe(length);
};
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);
// Begin incrementing clicks (and therefore refs).
ReactTestUtils.Simulate.click(clickIncrementer);
expectClickLogsLengthToBe(testRefsComponent, 2);
ReactTestUtils.Simulate.click(clickIncrementer);
expectClickLogsLengthToBe(testRefsComponent, 3);
// Now reset again
ReactTestUtils.Simulate.click(testRefsComponent.refs.resetDiv);
expectClickLogsLengthToBe(testRefsComponent, 1);
}
</script>
</head>
<body>
<div>这个默认会被清掉</div>
<div id='example'></div>
</body>
</html>