-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathreact-plotly.test.js
234 lines (217 loc) Β· 6.32 KB
/
react-plotly.test.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import React from 'react';
import {mount} from 'enzyme';
import createComponent from '../factory';
import once from 'onetime';
describe('<Plotly/>', () => {
let Plotly, PlotComponent;
function createPlot(props) {
return new Promise((resolve, reject) => {
const plot = mount(
<PlotComponent
{...props}
onInitialized={() => resolve(plot)}
onError={reject}
/>
);
});
}
function expectPlotlyAPICall(method, props, defaultArgs) {
expect(method).toHaveBeenCalledWith(
expect.anything(),
Object.assign(
defaultArgs || {
data: [],
config: undefined,
layout: undefined,
frames: undefined,
},
props || {}
)
);
}
describe('with mocked plotly.js', () => {
beforeEach(() => {
Plotly = require.requireMock('../__mocks__/plotly.js').default;
PlotComponent = createComponent(Plotly);
// Override the parent element size:
PlotComponent.prototype.getParentSize = () => ({
width: 123,
height: 456,
});
});
describe('initialization', function() {
test('calls Plotly.newPlot on instantiation', done => {
createPlot({})
.then(() => {
expect(Plotly.newPlot).toHaveBeenCalled();
})
.catch(err => {
done.fail(err);
})
.then(done);
});
test('passes data', done => {
createPlot({
data: [{x: [1, 2, 3]}],
layout: {title: 'foo'},
})
.then(() => {
expectPlotlyAPICall(Plotly.newPlot, {
data: [{x: [1, 2, 3]}],
layout: {title: 'foo'},
});
})
.catch(err => done.fail(err))
.then(done);
});
test('accepts width and height', done => {
createPlot({
layout: {width: 320, height: 240},
})
.then(() => {
expectPlotlyAPICall(Plotly.newPlot, {
layout: {width: 320, height: 240},
});
})
.catch(err => done.fail(err))
.then(done);
});
test('overrides the height when fit: true', done => {
createPlot({
layout: {width: 320, height: 240},
fit: true,
})
.then(() => {
expectPlotlyAPICall(Plotly.newPlot, {
layout: {width: 320, height: 240},
});
})
.catch(err => done.fail(err))
.then(done);
});
});
describe('plot updates', () => {
test('updates data', done => {
createPlot({
fit: true,
layout: {width: 123, height: 456},
onUpdate: once(() => {
expectPlotlyAPICall(Plotly.newPlot, {
data: [{x: [1, 2, 3]}],
layout: {width: 123, height: 456},
});
done();
}),
})
.then(plot => {
plot.setProps({data: [{x: [1, 2, 3]}]});
})
.catch(err => done.fail(err));
});
test('sets the title', done => {
createPlot({
fit: false,
onUpdate: once(() => {
expectPlotlyAPICall(Plotly.newPlot, {
layout: {title: 'test test'},
});
done();
}),
})
.then(plot => {
plot.setProps({layout: {title: 'test test'}});
})
.catch(err => done.fail(err));
});
test('revision counter', done => {
var callCnt = 0;
createPlot({
fit: false,
revision: 0,
onUpdate: () => {
callCnt++;
// Should only get one update. Make it asynchronous in order
// to make sure we haven't accidentally ended the test before
// we've checked the third and fourth calls:
if (callCnt === 2) {
setTimeout(() => {
expect(Plotly.newPlot).not.toHaveBeenCalledTimes(2);
done();
}, 100);
}
},
})
.then(plot => {
// Update with and without revision bumps:
setTimeout(() => plot.setProps({layout: {title: 'test test'}}), 10);
setTimeout(
() => plot.setProps({revision: 1, layout: {title: 'test test'}}),
20
);
setTimeout(
() => plot.setProps({revision: 1, layout: {title: 'test test'}}),
30
);
setTimeout(
() => plot.setProps({revision: 2, layout: {title: 'test test'}}),
40
);
})
.catch(err => done.fail(err));
});
});
describe('responding to window events', () => {
describe('with fit: true', () => {
test('does not call relayout on initialization', done => {
createPlot({
fit: true,
onRelayout: () => done.fail('Unexpected relayout event'),
})
.then(() => {
setTimeout(done, 20);
})
.catch(err => done.fail(err));
});
test('calls relayout on window resize when fit: true', done => {
let relayoutCnt = 0;
createPlot({
fit: true,
onRelayout: () => {
relayoutCnt++;
if (relayoutCnt === 1) {
setTimeout(done, 20);
}
},
})
.then(() => {
window.dispatchEvent(new Event('resize'));
})
.catch(err => done.fail(err));
});
});
describe('with fit: false', () => {
test('does not call relayout on init', done => {
createPlot({
fit: false,
onRelayout: () => done.fail('Unexpected relayout event'),
})
.then(() => {
setTimeout(done, 20);
})
.catch(err => done.fail(err));
});
test('does not call relayout on window resize', done => {
createPlot({
fit: false,
onRelayout: () => done.fail('Unexpected relayout event'),
})
.then(() => {
window.dispatchEvent(new Event('resize'));
setTimeout(done, 20);
})
.catch(err => done.fail(err));
});
});
});
});
});