-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest-utils.js
143 lines (128 loc) · 3.55 KB
/
test-utils.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
import plotly from 'plotly.js/dist/plotly-cartesian';
import {extendDeep} from 'plotly.js/src/lib/extend';
import EditorControls from '../EditorControls';
import {configure, mount, shallow} from 'enzyme';
import {dereference} from '../lib';
import Adapter from 'enzyme-adapter-react-16';
configure({adapter: new Adapter()});
/* eslint-disable no-magic-numbers */
const fixtures = {
scatter(config) {
return applyConfig(config, {
dataSources: {
x1: [1, 2, 3],
y1: [2, 3, 4],
y2: [20, 30, 40],
},
dataSourceOptions: [
{label: 'xCol', value: 'x1'},
{label: 'yCol', value: 'y1'},
{label: 'yCol2', value: 'y2'},
],
graphDiv: {
data: [
{
xsrc: 'x1',
ysrc: 'y1',
name: 'yaxis data',
type: 'scatter',
},
{
xsrc: 'x1',
ysrc: 'y2',
name: 'yaxis2 data',
yaxis: 'y2',
type: 'scatter',
},
],
layout: {
title: 'Double Y Axis Example',
yaxis: {},
yaxis2: {
title: 'yaxis2 title',
titlefont: {color: 'rgb(148, 103, 189)'},
tickfont: {color: 'rgb(148, 103, 189)'},
overlaying: 'y',
side: 'right',
},
},
},
});
},
area(config) {
return applyConfig(config, {
dataSources: {
x1: [1, 2, 3],
y1: [2, 3, 4],
},
dataSourceOptions: [{label: 'xCol', value: 'x1'}, {label: 'yCol', value: 'y1'}],
graphDiv: {
data: [
{
type: 'scatter',
mode: 'markers+lines',
stackgroup: 1,
xsrc: 'x1',
ysrc: 'y1',
},
],
layout: {},
},
});
},
pie(config) {
return applyConfig(config, {
dataSources: {
x1: [1, 2, 3],
y1: [2, 3, 4],
},
dataSourceOptions: [{label: 'xCol', value: 'x1'}, {label: 'yCol', value: 'y1'}],
graphDiv: {
data: [{type: 'pie', mode: 'markers', labelssrc: 'x1', valuessrc: 'y1'}],
layout: {},
},
});
},
};
function applyConfig(config = {}, {graphDiv: {data, layout}, dataSourceOptions, dataSources}) {
if (config.layout) {
extendDeep(layout, config.layout);
}
if (config.data) {
extendDeep(data, config.data);
}
if (config.deref !== false) {
dereference(data, dataSources);
}
// replace simple graphDiv with properly mocked GD including fullData/fullLayout
const graphDiv = setupGraphDiv({data, layout});
return {dataSources, dataSourceOptions, graphDiv};
}
/*
* JSDOM does not implement full SVG spec. Mock out necessary methods here.
* https://github.com/tmpvar/jsdom/issues/1330
* Hardcoded return values have been "good enough" for now but feel free to
* extend the API if necessary.
*/
function mockMissingSvgApis() {
const p = document.createElementNS('http://www.w3.org/2000/svg', 'path');
const proto = Object.getPrototypeOf(p);
if (typeof proto.getTotalLength !== 'function') {
proto.getTotalLength = () => 100;
}
if (typeof proto.getPointAtLength !== 'function') {
proto.getPointAtLength = () => ({x: 0, y: 0});
}
}
function newGraphDiv() {
const graphDiv = window.document.createElement('div');
graphDiv.id = 'graphDiv';
return graphDiv;
}
function setupGraphDiv(figure) {
const gd = newGraphDiv();
mockMissingSvgApis();
plotly.newPlot(gd, figure);
return gd;
}
export {fixtures, plotly, EditorControls as TestEditor, mount, shallow, setupGraphDiv};