-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect-meteor-data-tests.jsx
136 lines (106 loc) · 3.24 KB
/
connect-meteor-data-tests.jsx
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
getInnerHtml = function (elem) {
// clean up elem.innerHTML and strip data-reactid attributes too
return canonicalizeHtml(elem.innerHTML).replace(/ data-reactid=".*?"/g, '');
};
Tinytest.add('react-meteor-data - basic track', function (test) {
var div = document.createElement("DIV");
var x = new ReactiveVar('aaa');
var Foo = React.createClass({
render() {
return <span>{this.props.x}</span>;
}
});
var MeteorizedFoo = connectMeteorData(function() {
return {
x: x.get()
}
})(Foo);
React.render(<MeteorizedFoo />, div);
test.equal(getInnerHtml(div), '<span>aaa</span>');
x.set('bbb');
Tracker.flush({_throwFirstError: true});
test.equal(getInnerHtml(div), '<span>bbb</span>');
test.equal(x._numListeners(), 1);
React.unmountComponentAtNode(div);
test.equal(x._numListeners(), 0);
});
Tinytest.add('react-meteor-data - track based on props', function (test) {
var div = document.createElement("DIV");
var xs = [new ReactiveVar('aaa'),
new ReactiveVar('bbb'),
new ReactiveVar('ccc')];
var Foo = React.createClass({
render() {
return <span>{this.props.x}</span>;
}
});
var MeteorizedFoo = connectMeteorData(function(props) {
return {
x: xs[props.n].get()
};
})(Foo);
var comp = React.render(<MeteorizedFoo n={0}/>, div);
test.equal(getInnerHtml(div), '<span>aaa</span>');
xs[0].set('AAA');
test.equal(getInnerHtml(div), '<span>aaa</span>');
Tracker.flush({_throwFirstError: true});
test.equal(getInnerHtml(div), '<span>AAA</span>');
{
let comp2 = React.render(<MeteorizedFoo n={1}/>, div);
test.isTrue(comp === comp2);
}
test.equal(getInnerHtml(div), '<span>bbb</span>');
xs[1].set('BBB');
Tracker.flush({_throwFirstError: true});
test.equal(getInnerHtml(div), '<span>BBB</span>');
React.render(<MeteorizedFoo n={2}/>, div);
test.equal(getInnerHtml(div), '<span>ccc</span>');
xs[2].set('CCC');
Tracker.flush({_throwFirstError: true});
test.equal(getInnerHtml(div), '<span>CCC</span>');
React.render(<MeteorizedFoo n={0}/>, div);
test.equal(getInnerHtml(div), '<span>AAA</span>');
React.unmountComponentAtNode(div);
});
Tinytest.add('react-meteor-data - track based on props and context', function (test) {
var div = document.createElement("DIV");
var x = new ReactiveVar('aaa');
var Foo = React.createClass({
render() {
return (
<span><span>{this.props.x}</span><span>{this.props.y}</span></span>
);
}
});
var MeteorizedFoo = connectMeteorData(function(props, context) {
return {
y: context.x,
x: x.get()
}
})(Foo);
MeteorizedFoo.contextTypes = {
x: React.PropTypes.string
};
var Bar = React.createClass({
childContextTypes: {
x: React.PropTypes.string
},
getChildContext() {
return {
x: 'bbb'
}
},
render() {
return (
<MeteorizedFoo />
)
}
});
React.render((<Bar />), div);
test.equal(getInnerHtml(div), '<span><span>aaa</span><span>bbb</span></span>');
x.set('CCC');
Tracker.flush({_throwFirstError: true});
test.equal(getInnerHtml(div), '<span><span>CCC</span><span>bbb</span></span>');
React.unmountComponentAtNode(div);
test.equal(x._numListeners(), 0);
});