diff --git a/src/plots/plots.js b/src/plots/plots.js index 4b5e632463e..3b1f9ad9d0a 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -233,9 +233,11 @@ plots.redrawText = function(gd) { // resize plot about the container size plots.resize = function(gd) { - if (!gd || d3.select(gd).style('display') === 'none') return; + return new Promise(function(resolve, reject) { - return new Promise(function(resolve) { + if (!gd || d3.select(gd).style('display') === 'none'){ + reject(new Error('Resize must be passed a plot div element.')); + } if (gd._redrawTimer) clearTimeout(gd._redrawTimer); @@ -249,7 +251,7 @@ plots.resize = function(gd) { Plotly.relayout(gd, { autosize: true }); gd.changed = oldchanged; - resolve(); + resolve(gd); } }, 100); }); diff --git a/test/jasmine/tests/plot_promise_test.js b/test/jasmine/tests/plot_promise_test.js index 1cd4348077f..8efe48ab920 100644 --- a/test/jasmine/tests/plot_promise_test.js +++ b/test/jasmine/tests/plot_promise_test.js @@ -464,4 +464,31 @@ describe('Plotly.___ methods', function() { }); }); + describe('Plotly.Plots.resize promise', function() { + var initialDiv; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }]; + + initialDiv = createGraphDiv(); + + Plotly.plot(initialDiv, data, {}).then(done); + }); + + it('should return a resolved promise of the gd', function(done) { + Plotly.Plots.resize(initialDiv).then(function(gd) { + expect(gd).toBeDefined(); + expect(typeof gd).toBe('object'); + expect(gd.layout).toBeDefined(); + }).then(done); + }); + + it('should return a rejected promise with no argument', function(done) { + Plotly.Plots.resize().then(null, function(err) { + expect(err).toBeDefined(); + expect(err.message).toBe('Resize must be passed a plot div element.'); + }).then(done); + }); + }); + });