-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsnapshot_test.js
211 lines (176 loc) · 7.34 KB
/
snapshot_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
var Plotly = require('@lib/index');
var d3 = require('d3');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var subplotMock = require('../../image/mocks/multiple_subplots.json');
var annotationMock = require('../../image/mocks/annotations.json');
describe('Plotly.Snapshot', function() {
'use strict';
describe('clone', function() {
var data,
layout,
dummyTrace1, dummyTrace2,
dummyGraphObj;
dummyTrace1 = {
x: ['0', '1', '2', '3', '4', '5'],
y: ['2', '4', '6', '8', '6', '4'],
mode: 'markers',
name: 'Col2',
type: 'scatter'
};
dummyTrace2 = {
x: ['0', '1', '2', '3', '4', '5'],
y: ['4', '6', '8', '10', '8', '6'],
mode: 'markers',
name: 'Col3',
type: 'scatter'
};
data = [dummyTrace1, dummyTrace2];
layout = {
title: 'Chart Title',
showlegend: true,
autosize: true,
width: 688,
height: 460,
xaxis: {
title: 'xaxis title',
range: [-0.323374917925, 5.32337491793],
type: 'linear',
autorange: true
},
yaxis: {
title: 'yaxis title',
range: [1.41922290389, 10.5807770961],
type: 'linear',
autorange: true
}
};
dummyGraphObj = {
data: data,
layout: layout
};
it('should create a themeTile, with width certain things stripped out', function() {
var themeOptions = {
tileClass: 'themes__thumb'
};
// Defaults from clone()
var THEMETILE_DEFAULT_LAYOUT = {
autosize: true,
width: 150,
height: 150,
title: '',
showlegend: false,
margin: {'l': 5, 'r': 5, 't': 5, 'b': 5, 'pad': 0},
annotations: []
};
var config = {
staticPlot: true,
plotGlPixelRatio: 2,
displaylogo: false,
showLink: false,
showTips: false,
setBackground: 'opaque'
};
var themeTile = Plotly.Snapshot.clone(dummyGraphObj, themeOptions);
expect(themeTile.layout.height).toEqual(THEMETILE_DEFAULT_LAYOUT.height);
expect(themeTile.layout.width).toEqual(THEMETILE_DEFAULT_LAYOUT.width);
expect(themeTile.td.defaultLayout).toEqual(THEMETILE_DEFAULT_LAYOUT);
expect(themeTile.config).toEqual(config);
});
it('should create a thumbnail for image export to the filewell', function() {
var thumbnailOptions = {
tileClass: 'thumbnail'
};
var THUMBNAIL_DEFAULT_LAYOUT = {
'title': '',
'hidesources': true,
'showlegend': false,
'hovermode': false,
'dragmode': false,
'zoom': false,
'borderwidth': 0,
'bordercolor': '',
'margin': {'l': 1, 'r': 1, 't': 1, 'b': 1, 'pad': 0},
'annotations': []
};
var thumbTile = Plotly.Snapshot.clone(dummyGraphObj, thumbnailOptions);
expect(thumbTile.layout.hidesources).toEqual(THUMBNAIL_DEFAULT_LAYOUT.hidesources);
expect(thumbTile.layout.showlegend).toEqual(THUMBNAIL_DEFAULT_LAYOUT.showlegend);
expect(thumbTile.layout.borderwidth).toEqual(THUMBNAIL_DEFAULT_LAYOUT.borderwidth);
expect(thumbTile.layout.annotations).toEqual(THUMBNAIL_DEFAULT_LAYOUT.annotations);
});
it('should create a custom sized Tile based on options', function() {
var customOptions = {
tileClass: 'notarealclass',
height: 888,
width: 888
};
var customTile = Plotly.Snapshot.clone(dummyGraphObj, customOptions);
expect(customTile.layout.height).toEqual(customOptions.height);
expect(customTile.layout.width).toEqual(customOptions.width);
});
it('should not touch the data or layout if you do not specify an existing tileClass', function() {
var vanillaOptions = {
tileClass: 'notarealclass'
};
var vanillaPlotTile = Plotly.Snapshot.clone(dummyGraphObj, vanillaOptions);
expect(vanillaPlotTile.data[0].x).toEqual(data[0].x);
expect(vanillaPlotTile.layout).toEqual(layout);
expect(vanillaPlotTile.layout.height).toEqual(layout.height);
expect(vanillaPlotTile.layout.width).toEqual(layout.width);
});
it('should set the background parameter appropriately', function() {
var pt = Plotly.Snapshot.clone(dummyGraphObj, {
setBackground: 'transparent'
});
expect(pt.config.setBackground).not.toBeDefined();
pt = Plotly.Snapshot.clone(dummyGraphObj, {
setBackground: 'blue'
});
expect(pt.config.setBackground).toEqual('blue');
});
});
describe('toSVG', function() {
var parser = new DOMParser(),
gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should not return any nested svg tags of plots', function(done) {
Plotly.plot(gd, subplotMock.data, subplotMock.layout).then(function() {
return Plotly.Snapshot.toSVG(gd);
}).then(function(svg) {
var svgDOM = parser.parseFromString(svg, 'image/svg+xml'),
svgElements = svgDOM.getElementsByTagName('svg');
expect(svgElements.length).toBe(1);
}).then(done);
});
it('should not return any nested svg tags of annotations', function(done) {
Plotly.plot(gd, annotationMock.data, annotationMock.layout).then(function() {
return Plotly.Snapshot.toSVG(gd);
}).then(function(svg) {
var svgDOM = parser.parseFromString(svg, 'image/svg+xml'),
svgElements = svgDOM.getElementsByTagName('svg');
expect(svgElements.length).toBe(1);
}).then(done);
});
it('should force *visibility: visible* for text elements with *visibility: inherit*', function(done) {
d3.select(gd).style('visibility', 'inherit');
Plotly.plot(gd, subplotMock.data, subplotMock.layout).then(function() {
d3.select(gd).selectAll('text').each(function() {
expect(d3.select(this).style('visibility')).toEqual('visible');
});
return Plotly.Snapshot.toSVG(gd);
})
.then(function(svg) {
var svgDOM = parser.parseFromString(svg, 'image/svg+xml'),
textElements = svgDOM.getElementsByTagName('text');
for(var i = 0; i < textElements.length; i++) {
expect(textElements[i].style.visibility).toEqual('visible');
}
done();
});
});
});
});