Skip to content

Commit 5d2b635

Browse files
committed
robustify toImage tests
- + add 'webp' test case
1 parent 9128e13 commit 5d2b635

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

test/jasmine/tests/toimage_test.js

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// move toimage to plot_api_test.js
2-
// once established and confirmed?
3-
4-
var Plotly = require('@lib/index');
1+
var Plotly = require('@lib');
2+
var Lib = require('@src/lib');
53

64
var d3 = require('d3');
75
var createGraphDiv = require('../assets/create_graph_div');
6+
var fail = require('../assets/fail_test');
87
var subplotMock = require('@mocks/multiple_subplots.json');
98

109

@@ -25,6 +24,15 @@ describe('Plotly.toImage', function() {
2524
d3.selectAll('#graph').remove();
2625
});
2726

27+
function createImage(url) {
28+
return new Promise(function(resolve, reject) {
29+
var img = document.createElement('img');
30+
img.src = url;
31+
img.onload = function() { return resolve(img); };
32+
img.onerror = function() { return reject('error during createImage'); };
33+
});
34+
}
35+
2836
it('should be attached to Plotly', function() {
2937
expect(Plotly.toImage).toBeDefined();
3038
});
@@ -71,52 +79,54 @@ describe('Plotly.toImage', function() {
7179
});
7280

7381
it('should create img with proper height and width', function(done) {
74-
var img = document.createElement('img');
82+
var fig = Lib.extendDeep({}, subplotMock);
7583

7684
// specify height and width
77-
subplotMock.layout.height = 600;
78-
subplotMock.layout.width = 700;
85+
fig.layout.height = 600;
86+
fig.layout.width = 700;
7987

80-
Plotly.plot(gd, subplotMock.data, subplotMock.layout).then(function(gd) {
88+
Plotly.plot(gd, fig.data, fig.layout).then(function(gd) {
8189
expect(gd.layout.height).toBe(600);
8290
expect(gd.layout.width).toBe(700);
8391
return Plotly.toImage(gd);
84-
}).then(function(url) {
85-
return new Promise(function(resolve) {
86-
img.src = url;
87-
img.onload = function() {
88-
expect(img.height).toBe(600);
89-
expect(img.width).toBe(700);
90-
};
91-
// now provide height and width in opts
92-
resolve(Plotly.toImage(gd, {height: 400, width: 400}));
93-
});
94-
}).then(function(url) {
95-
img.src = url;
96-
img.onload = function() {
97-
expect(img.height).toBe(400);
98-
expect(img.width).toBe(400);
99-
done();
100-
};
101-
});
92+
})
93+
.then(createImage)
94+
.then(function(img) {
95+
expect(img.height).toBe(600);
96+
expect(img.width).toBe(700);
97+
98+
return Plotly.toImage(gd, {height: 400, width: 400});
99+
})
100+
.then(createImage)
101+
.then(function(img) {
102+
expect(img.height).toBe(400);
103+
expect(img.width).toBe(400);
104+
})
105+
.catch(fail)
106+
.then(done);
102107
});
103108

104109
it('should create proper file type', function(done) {
105-
var plot = Plotly.plot(gd, subplotMock.data, subplotMock.layout);
110+
var fig = Lib.extendDeep({}, subplotMock);
106111

107-
plot.then(function(gd) {
108-
return Plotly.toImage(gd, {format: 'png'});
109-
}).then(function(url) {
112+
Plotly.plot(gd, fig.data, fig.layout)
113+
.then(function() { return Plotly.toImage(gd, {format: 'png'}); })
114+
.then(function(url) {
110115
expect(url.split('png')[0]).toBe('data:image/');
111-
// now do jpeg
112-
return Plotly.toImage(gd, {format: 'jpeg'});
113-
}).then(function(url) {
116+
})
117+
.then(function() { return Plotly.toImage(gd, {format: 'jpeg'}); })
118+
.then(function(url) {
114119
expect(url.split('jpeg')[0]).toBe('data:image/');
115-
// now do svg
116-
return Plotly.toImage(gd, {format: 'svg'});
117-
}).then(function(url) {
120+
})
121+
.then(function() { return Plotly.toImage(gd, {format: 'svg'}); })
122+
.then(function(url) {
118123
expect(url.split('svg')[0]).toBe('data:image/');
119-
done();
120-
});
124+
})
125+
.then(function() { return Plotly.toImage(gd, {format: 'webp'}); })
126+
.then(function(url) {
127+
expect(url.split('webp')[0]).toBe('data:image/');
128+
})
129+
.catch(fail)
130+
.then(done);
121131
});
122132
});

0 commit comments

Comments
 (0)