forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloneElement.spec.js
89 lines (84 loc) · 2.58 KB
/
cloneElement.spec.js
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
import React from "dist/React";
describe("cloneElement", function () {
it("test", () => {
var a = {
type: "div",
props: {
v: 1,
children: []
}
};
expect(React.cloneElement(a).props.v).toBe(1);
});
it("array", () => {
var a = {
type: "div",
props: {
v: 2,
children: []
}
};
expect(React.cloneElement(a).props.v).toBe(2);
});
it("should transfer the key property", ()=> {
var Component = React.createClass({
render: function() {
return null;
},
});
var clone = React.cloneElement(<Component />, {key: "xyz"});
expect(clone.key).toBe("xyz");
});
it("children", () => {
function A() { }
var b = React.cloneElement({
type: A,
tag: 2,
props: {}
}, {
children: [111, 222],
onChange: function () { },
key: "tabContent"
});
expect(b.props.children.length).toBe(2);
});
it("属性是一个虚拟DOM,被重复clone", ()=>{
class Tree extends React.Component{
constructor(props){
super(props);
this.state = {};
}
renderTreeNode(child){
var childProps = {};
childProps.checkable = this.props.checkable;
return React.cloneElement(child, childProps);
}
render() {
var props = this.props;
return React.createElement(
"ul",
{
className: "root",
role: "tree-node",
unselectable: "on"
},
React.Children.map(props.children, this.renderTreeNode, this)
);
}
}
class TreeNode extends React.Component{
render(){
return <li>{this.props.checkable}{this.props.children}</li>;
}
}
var container = document.createElement("div");
ReactDOM.render(<Tree checkable={ <input className="checked" type="radio" defaultChecked="true"/>}>
<TreeNode>1111</TreeNode>
<TreeNode>2222</TreeNode>
<TreeNode>3333</TreeNode>
</Tree>
, container);
var inputs = container.getElementsByTagName("input");
expect(inputs.length).toBe(3);
});
});