Skip to content

Commit b11c365

Browse files
committed
Merge pull request plotly#262 from plotly/promise-from-resize
Promise from resize (tests and returning gd)
2 parents 8b6344b + 085b0c7 commit b11c365

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/plots/plots.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ plots.redrawText = function(gd) {
233233

234234
// resize plot about the container size
235235
plots.resize = function(gd) {
236-
if (!gd || d3.select(gd).style('display') === 'none') return;
236+
return new Promise(function(resolve, reject) {
237237

238-
return new Promise(function(resolve) {
238+
if (!gd || d3.select(gd).style('display') === 'none'){
239+
reject(new Error('Resize must be passed a plot div element.'));
240+
}
239241

240242
if (gd._redrawTimer) clearTimeout(gd._redrawTimer);
241243

@@ -249,7 +251,7 @@ plots.resize = function(gd) {
249251

250252
Plotly.relayout(gd, { autosize: true });
251253
gd.changed = oldchanged;
252-
resolve();
254+
resolve(gd);
253255
}
254256
}, 100);
255257
});

test/jasmine/tests/plot_promise_test.js

+27
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,31 @@ describe('Plotly.___ methods', function() {
464464
});
465465
});
466466

467+
describe('Plotly.Plots.resize promise', function() {
468+
var initialDiv;
469+
470+
beforeEach(function(done) {
471+
var data = [{ x: [1,2,3], y: [4,5,6] }];
472+
473+
initialDiv = createGraphDiv();
474+
475+
Plotly.plot(initialDiv, data, {}).then(done);
476+
});
477+
478+
it('should return a resolved promise of the gd', function(done) {
479+
Plotly.Plots.resize(initialDiv).then(function(gd) {
480+
expect(gd).toBeDefined();
481+
expect(typeof gd).toBe('object');
482+
expect(gd.layout).toBeDefined();
483+
}).then(done);
484+
});
485+
486+
it('should return a rejected promise with no argument', function(done) {
487+
Plotly.Plots.resize().then(null, function(err) {
488+
expect(err).toBeDefined();
489+
expect(err.message).toBe('Resize must be passed a plot div element.');
490+
}).then(done);
491+
});
492+
});
493+
467494
});

0 commit comments

Comments
 (0)