-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathreact-plotly.test.js
183 lines (166 loc) Β· 5.31 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
import React from 'react';
import Adapter from 'enzyme-adapter-react-16';
import {mount, configure} from 'enzyme';
import createComponent from '../factory';
import once from 'onetime';
describe('<Plotly/>', () => {
let Plotly, PlotComponent;
configure({adapter: new Adapter()});
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, // eslint-disable-line no-undefined
layout: undefined, // eslint-disable-line no-undefined
frames: undefined, // eslint-disable-line no-undefined
},
props || {}
)
);
}
describe('with mocked plotly.js', () => {
beforeEach(() => {
Plotly = jest.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.react on instantiation', (done) => {
createPlot({})
.then(() => {
expect(Plotly.react).toHaveBeenCalled();
})
.catch((err) => {
done.fail(err);
})
.then(done);
});
test('passes data', (done) => {
createPlot({
data: [{x: [1, 2, 3]}],
layout: {title: 'foo'},
})
.then(() => {
expectPlotlyAPICall(Plotly.react, {
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.react, {
layout: {width: 320, height: 240},
});
})
.catch((err) => done.fail(err))
.then(done);
});
});
describe('plot updates', () => {
test('updates data', (done) => {
createPlot({
layout: {width: 123, height: 456},
onUpdate: once(() => {
expectPlotlyAPICall(Plotly.react, {
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('updates data when revision is defined but not changed', (done) => {
createPlot({
revision: 1,
layout: {width: 123, height: 456},
onUpdate: once(() => {
expectPlotlyAPICall(Plotly.react, {
data: [{x: [1, 2, 3]}],
layout: {width: 123, height: 456},
});
done();
}),
})
.then((plot) => {
plot.setProps({revision: 1, data: [{x: [1, 2, 3]}]});
})
.catch((err) => done.fail(err));
});
test('sets the title', (done) => {
createPlot({
onUpdate: once(() => {
expectPlotlyAPICall(Plotly.react, {
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({
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.react).not.toHaveBeenCalledTimes(2);
done();
}, 100);
}
},
})
.then((plot) => {
// Update with and without revision bumps:
/* eslint-disable no-magic-numbers */
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('manging event handlers', () => {
test('should add an event handler when one does not already exist', (done) => {
const onRelayout = () => {};
createPlot({onRelayout}).then((plot) => {
const {handlers} = plot.instance();
expect(plot.prop('onRelayout')).toBe(onRelayout);
expect(handlers.Relayout).toBe(onRelayout);
done();
});
});
});
});
});