1
|
|
|
import React from 'react' |
2
|
|
|
import { configure, shallow } from 'enzyme' |
3
|
|
|
import Experiment from '../Experiment' |
4
|
|
|
import Variant from '../Variant' |
5
|
|
|
import Adapter from 'enzyme-adapter-react-16' |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
configure({ adapter: new Adapter() }) |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
it('test variantName', () => { |
12
|
|
|
let name = '' |
13
|
|
|
|
14
|
|
|
const wrapper = shallow( |
15
|
|
|
<Experiment |
16
|
|
|
name="Test" |
17
|
|
|
variantName={'B'} |
18
|
|
|
onChoice={(a, b) => name = `${a} ${b}`} |
19
|
|
|
> |
20
|
|
|
<Variant name="A"> |
21
|
|
|
<div>test1</div> |
22
|
|
|
</Variant> |
23
|
|
|
<Variant name="B"><span>test2</span></Variant> |
24
|
|
|
</Experiment>, |
25
|
|
|
) |
26
|
|
|
|
27
|
|
|
expect(name).toEqual('Test B') |
28
|
|
|
expect(wrapper.contains(<div>test1</div>)).toEqual(false) |
29
|
|
|
expect(wrapper.contains(<span>test2</span>)).toEqual(true) |
30
|
|
|
}) |
31
|
|
|
|
32
|
|
|
it('test onChoice', () => { |
33
|
|
|
let name = '' |
34
|
|
|
|
35
|
|
|
const wrapper = shallow( |
36
|
|
|
<Experiment |
37
|
|
|
name="Test" |
38
|
|
|
onChoice={(a, b) => name = `${a} ${b}`} |
39
|
|
|
userIdentifier={0} |
40
|
|
|
> |
41
|
|
|
<Variant name="A"> |
42
|
|
|
<div>Test A</div> |
43
|
|
|
</Variant> |
44
|
|
|
<Variant name="B"> |
45
|
|
|
<div>Test B</div> |
46
|
|
|
</Variant> |
47
|
|
|
</Experiment>, |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
expect(wrapper.contains(<div>{name}</div>)).toEqual(true) |
51
|
|
|
}) |
52
|
|
|
|
53
|
|
|
it('test onRawChoice', () => { |
54
|
|
|
let name = '' |
55
|
|
|
|
56
|
|
|
const wrapper = shallow( |
57
|
|
|
<Experiment |
58
|
|
|
name="Test" |
59
|
|
|
onRawChoice={(a, b) => name = `${a.props.name} ${b.props.name}`} |
60
|
|
|
userIdentifier={0} |
61
|
|
|
> |
62
|
|
|
<Variant name="A"> |
63
|
|
|
<div>Test A</div> |
64
|
|
|
</Variant> |
65
|
|
|
<Variant name="B"> |
66
|
|
|
<div>Test B</div> |
67
|
|
|
</Variant> |
68
|
|
|
</Experiment>, |
69
|
|
|
) |
70
|
|
|
|
71
|
|
|
expect(wrapper.contains(<div>{name}</div>)).toEqual(true) |
72
|
|
|
}) |
73
|
|
|
|
74
|
|
|
it('test componentDidUpdate', () => { |
75
|
|
|
let name = '' |
76
|
|
|
const onChoice = (a, b) => name = `${a} ${b}` |
77
|
|
|
|
78
|
|
|
const wrapper = shallow( |
79
|
|
|
<Experiment name="Test" onChoice={onChoice} userIdentifier={24}> |
80
|
|
|
<Variant name="A"> |
81
|
|
|
<div>A</div> |
82
|
|
|
</Variant> |
83
|
|
|
<Variant name="B"> |
84
|
|
|
<div>B</div> |
85
|
|
|
</Variant> |
86
|
|
|
</Experiment>, |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
// First render |
90
|
|
|
expect(name).toEqual('Test A') |
91
|
|
|
expect(wrapper.contains(<div>A</div>)).toEqual(true) |
92
|
|
|
|
93
|
|
|
wrapper.setProps({ |
94
|
|
|
name: 'Test', |
95
|
|
|
onChoice, |
96
|
|
|
userIdentifier: 25, |
97
|
|
|
}) |
98
|
|
|
|
99
|
|
|
// With changed userIdentifier |
100
|
|
|
expect(name).toEqual('Test B') |
101
|
|
|
expect(wrapper.contains(<div>B</div>)).toEqual(true) |
102
|
|
|
|
103
|
|
|
wrapper.setProps({ |
104
|
|
|
name: 'Test2', |
105
|
|
|
onChoice, |
106
|
|
|
userIdentifier: 25, |
107
|
|
|
}) |
108
|
|
|
|
109
|
|
|
// With changed name |
110
|
|
|
expect(name).toEqual('Test2 B') |
111
|
|
|
expect(wrapper.contains(<div>B</div>)).toEqual(true) |
112
|
|
|
|
113
|
|
|
let flag = false |
114
|
|
|
wrapper.setProps({ |
115
|
|
|
name: 'Test2', |
116
|
|
|
onChoice: () => flag = true, |
117
|
|
|
userIdentifier: 25, |
118
|
|
|
}) |
119
|
|
|
|
120
|
|
|
// Not be called |
121
|
|
|
expect(flag).toEqual(false) |
122
|
|
|
expect(wrapper.contains(<div>B</div>)).toEqual(true) |
123
|
|
|
}) |
124
|
|
|
|
125
|
|
|
it('test weights', () => { |
126
|
|
|
let name = '' |
127
|
|
|
|
128
|
|
|
const wrapper = shallow( |
129
|
|
|
<Experiment |
130
|
|
|
name="Test" |
131
|
|
|
onChoice={(a, b) => name = `${a} ${b}`} |
132
|
|
|
userIdentifier={24} |
133
|
|
|
> |
134
|
|
|
<Variant name="A" weight={0}> |
135
|
|
|
<div>A</div> |
136
|
|
|
</Variant> |
137
|
|
|
<Variant name="B"> |
138
|
|
|
<div>B</div> |
139
|
|
|
</Variant> |
140
|
|
|
</Experiment>, |
141
|
|
|
) |
142
|
|
|
|
143
|
|
|
expect(wrapper.contains(<div>B</div>)).toEqual(true) |
144
|
|
|
}) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
it('test random', () => { |
148
|
|
|
const stats = { 'A': 0, 'B': 0, 'C': 0 } |
149
|
|
|
const onChoice = (a, b) => ++stats[b] |
150
|
|
|
|
151
|
|
|
for (let i = 0; i < 100; i++) { |
152
|
|
|
shallow( |
153
|
|
|
<Experiment name="Test" onChoice={onChoice} userIdentifier={i}> |
154
|
|
|
<Variant name="A" weight={1}> |
155
|
|
|
<div>A</div> |
156
|
|
|
</Variant> |
157
|
|
|
<Variant name="B" weight={2}> |
158
|
|
|
<div>B</div> |
159
|
|
|
</Variant> |
160
|
|
|
<Variant name="C" weight={1}> |
161
|
|
|
<div>C</div> |
162
|
|
|
</Variant> |
163
|
|
|
</Experiment>, |
164
|
|
|
) |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
expect(stats).toEqual({ A: 28, B: 55, C: 17 }) |
168
|
|
|
}) |
169
|
|
|
|
170
|
|
|
it('test empty experiment', () => { |
171
|
|
|
shallow(<Experiment name="Test" />) |
172
|
|
|
}) |
173
|
|
|
|
174
|
|
|
it('test empty variant', () => { |
175
|
|
|
shallow(<Experiment name="Test"><Variant name="A" /></Experiment>) |
176
|
|
|
}) |
177
|
|
|
|
178
|
|
|
it('test wrong variantName', () => { |
179
|
|
|
const wrapper = shallow( |
180
|
|
|
<Experiment name="Test" variantName={'C'}> |
181
|
|
|
<Variant name="A">test1</Variant> |
182
|
|
|
<Variant name="B">test2</Variant> |
183
|
|
|
</Experiment>, |
184
|
|
|
) |
185
|
|
|
|
186
|
|
|
expect(wrapper.contains('test1')).toEqual(false) |
187
|
|
|
expect(wrapper.contains('test2')).toEqual(false) |
188
|
|
|
}) |
189
|
|
|
|