-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathtoimage_test.js
213 lines (182 loc) · 7.3 KB
/
toimage_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
var Plotly = require('@lib');
var Lib = require('@src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var fail = require('../assets/fail_test');
var subplotMock = require('@mocks/multiple_subplots.json');
var FORMATS = ['png', 'jpeg', 'webp', 'svg'];
describe('Plotly.toImage', function() {
'use strict';
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
function createImage(url) {
return new Promise(function(resolve, reject) {
var img = document.createElement('img');
img.src = url;
img.onload = function() { return resolve(img); };
img.onerror = function() { return reject('error during createImage'); };
});
}
function assertSize(url, width, height) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.onload = function() {
expect(img.width).toBe(width, 'image width');
expect(img.height).toBe(height, 'image height');
resolve(url);
};
img.onerror = reject;
img.src = url;
});
}
it('should be attached to Plotly', function() {
expect(Plotly.toImage).toBeDefined();
});
it('should return a promise', function(done) {
function isPromise(x) {
return !!x.then && typeof x.then === 'function';
}
var returnValue = Plotly.plot(gd, subplotMock.data, subplotMock.layout)
.then(Plotly.toImage);
expect(isPromise(returnValue)).toBe(true);
returnValue.then(done);
});
it('should throw error with unsupported file type', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig.data, fig.layout)
.then(function(gd) {
expect(function() { Plotly.toImage(gd, {format: 'x'}); })
.toThrow(new Error('Image format is not jpeg, png, svg or webp.'));
})
.catch(fail)
.then(done);
});
it('should throw error with height and/or width < 1', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig.data, fig.layout)
.then(function() {
expect(function() { Plotly.toImage(gd, {height: 0.5}); })
.toThrow(new Error('Height and width should be pixel values.'));
})
.then(function() {
expect(function() { Plotly.toImage(gd, {width: 0.5}); })
.toThrow(new Error('Height and width should be pixel values.'));
})
.catch(fail)
.then(done);
});
it('should create img with proper height and width', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
// specify height and width
fig.layout.height = 600;
fig.layout.width = 700;
Plotly.plot(gd, fig.data, fig.layout).then(function(gd) {
expect(gd.layout.height).toBe(600);
expect(gd.layout.width).toBe(700);
return Plotly.toImage(gd);
})
.then(createImage)
.then(function(img) {
expect(img.height).toBe(600);
expect(img.width).toBe(700);
return Plotly.toImage(gd, {height: 400, width: 400});
})
.then(createImage)
.then(function(img) {
expect(img.height).toBe(400);
expect(img.width).toBe(400);
})
.catch(fail)
.then(done);
});
it('should create proper file type', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig.data, fig.layout)
.then(function() { return Plotly.toImage(gd, {format: 'png'}); })
.then(function(url) { return assertSize(url, 700, 450); })
.then(function(url) {
expect(url.split('png')[0]).toBe('data:image/');
})
.then(function() { return Plotly.toImage(gd, {format: 'jpeg'}); })
.then(function(url) { return assertSize(url, 700, 450); })
.then(function(url) {
expect(url.split('jpeg')[0]).toBe('data:image/');
})
.then(function() { return Plotly.toImage(gd, {format: 'svg'}); })
.then(function(url) { return assertSize(url, 700, 450); })
.then(function(url) {
expect(url.split('svg')[0]).toBe('data:image/');
})
.then(function() { return Plotly.toImage(gd, {format: 'webp'}); })
.then(function(url) { return assertSize(url, 700, 450); })
.then(function(url) {
expect(url.split('webp')[0]).toBe('data:image/');
})
.catch(fail)
.then(done);
});
it('should strip *data:image* prefix when *imageDataOnly* is turned on', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig.data, fig.layout)
.then(function() { return Plotly.toImage(gd, {format: 'png', imageDataOnly: true}); })
.then(function(d) {
expect(d.indexOf('data:image/')).toBe(-1);
expect(d.length).toBeWithin(50000, 5e3, 'png image length');
})
.then(function() { return Plotly.toImage(gd, {format: 'jpeg', imageDataOnly: true}); })
.then(function(d) {
expect(d.indexOf('data:image/')).toBe(-1);
expect(d.length).toBeWithin(43251, 5e3, 'jpeg image length');
})
.then(function() { return Plotly.toImage(gd, {format: 'svg', imageDataOnly: true}); })
.then(function(d) {
expect(d.indexOf('data:image/')).toBe(-1);
expect(d.length).toBeWithin(32062, 1e3, 'svg image length');
})
.then(function() { return Plotly.toImage(gd, {format: 'webp', imageDataOnly: true}); })
.then(function(d) {
expect(d.indexOf('data:image/')).toBe(-1);
expect(d.length).toBeWithin(15831, 1e3, 'webp image length');
})
.catch(fail)
.then(done);
});
FORMATS.forEach(function(f) {
it('should respond to *scale* option ( format ' + f + ')', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig.data, fig.layout)
.then(function() { return Plotly.toImage(gd, {format: f, scale: 2}); })
.then(function(url) { return assertSize(url, 1400, 900); })
.then(function() { return Plotly.toImage(gd, {format: f, scale: 0.5}); })
.then(function(url) { return assertSize(url, 350, 225); })
.catch(fail)
.then(done);
});
});
it('should accept data/layout/config figure object as input', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.toImage(fig)
.then(createImage)
.then(function(img) {
expect(img.width).toBe(700);
expect(img.height).toBe(450);
})
.catch(fail)
.then(done);
});
it('should accept graph div id as input', function(done) {
var fig = Lib.extendDeep({}, subplotMock);
Plotly.plot(gd, fig)
.then(function() { return Plotly.toImage('graph'); })
.then(createImage)
.then(function(img) {
expect(img.width).toBe(700);
expect(img.height).toBe(450);
})
.catch(fail)
.then(done);
});
});