forked from pushtell/react-ab-test
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcore.test.jsx
More file actions
138 lines (137 loc) · 4.58 KB
/
core.test.jsx
File metadata and controls
138 lines (137 loc) · 4.58 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
import React from "react";
import ReactDOM from "react-dom";
import createReactClass from 'create-react-class';
import Experiment from "../../src/CoreExperiment.jsx";
import Variant from "../../src/Variant.jsx";
import emitter from "../../src/emitter.jsx";
import assert from "assert";
import co from "co";
import UUID from "node-uuid";
import ES6Promise from 'es6-promise';
ES6Promise.polyfill();
describe("Core Experiment", function() {
let container;
before(function(){
container = document.createElement("div");
container.id = "react";
document.getElementsByTagName('body')[0].appendChild(container);
});
after(function(){
document.getElementsByTagName('body')[0].removeChild(container);
emitter._reset();
});
it("should render the correct variant.", co.wrap(function *(){
let experimentName = UUID.v4();
let App = createReactClass({
render: function(){
return <Experiment name={experimentName} value="A">
<Variant name="A"><div id="variant-a" /></Variant>
<Variant name="B"><div id="variant-b" /></Variant>
</Experiment>;
}
});
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
let elementA = document.getElementById('variant-a');
let elementB = document.getElementById('variant-b');
assert.notEqual(elementA, null);
assert.equal(elementB, null);
ReactDOM.unmountComponentAtNode(container);
}));
it("should error if invalid children exist.", co.wrap(function *(){
let experimentName = UUID.v4();
let App = createReactClass({
render: function(){
return <Experiment name={experimentName} value="A">
<Variant name="A"><div id="variant-a" /></Variant>
<div />
</Experiment>;
}
});
try {
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
} catch(error) {
if(error.type !== "PUSHTELL_INVALID_CHILD") {
throw error;
}
ReactDOM.unmountComponentAtNode(container);
return;
}
throw new Error("Experiment has invalid children.");
}));
it("should update on componentWillReceiveProps.", co.wrap(function *(){
let experimentName = UUID.v4();
let setState;
let getValueA = function(){
return "A";
}
let getValueB = function() {
return "B";
}
let App = createReactClass({
getInitialState: function(){
return {
value: getValueA
}
},
componentWillMount: function(){
setState = this.setState.bind(this);
},
render: function(){
return <Experiment name={experimentName} value={this.state.value}>
<Variant name="A"><div id="variant-a" /></Variant>
<Variant name="B"><div id="variant-b" /></Variant>
</Experiment>;
}
});
yield new Promise(function(resolve, reject){
ReactDOM.render(<App />, container, resolve);
});
let elementA = document.getElementById('variant-a');
let elementB = document.getElementById('variant-b');
assert.notEqual(elementA, null);
assert.equal(elementB, null);
setState({
value: getValueB
});
elementA = document.getElementById('variant-a');
elementB = document.getElementById('variant-b');
assert.equal(elementA, null);
assert.notEqual(elementB, null);
ReactDOM.unmountComponentAtNode(container);
}));
it("should update the children when props change.", co.wrap(function *(){
let experimentName = UUID.v4();
let SubComponent = createReactClass({
render(){
return (
<div id="variant-a">
<span id="variant-a-text">{this.props.text}</span>
</div>
)
}
});
let App = createReactClass({
render: function(){
return <Experiment name={experimentName} value="A">
<Variant name="A"><SubComponent text={this.props.text}/></Variant>
<Variant name="B"><div id="variant-b" /></Variant>
</Experiment>;
}
});
yield new Promise(function(resolve, reject){
let component = ReactDOM.render(<App text='original text'/>,container, resolve);
});
let elementAText = document.getElementById('variant-a-text');
assert.equal(elementAText.textContent, "original text");
yield new Promise(function(resolve, reject){
component = ReactDOM.render(<App text='New text'/>,container, resolve);
});
elementAText = document.getElementById('variant-a-text');
assert.equal(elementAText.textContent, "New text");
ReactDOM.unmountComponentAtNode(container);
}));
});