diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 38f11655f6c..78424eaab43 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -59,7 +59,7 @@ Plotly.plot = function(gd, data, layout, config) { Events.init(gd); var okToPlot = Events.triggerHandler(gd, 'plotly_beforeplot', [data, layout, config]); - if(okToPlot===false) return; + if(okToPlot===false) return Promise.reject(); // if there's no data or layout, and this isn't yet a plotly plot // container, log a warning to help plotly.js users debug @@ -112,7 +112,7 @@ Plotly.plot = function(gd, data, layout, config) { // signal to drag handler that after everything else is done // we need to replot, because something has changed gd._replotPending = true; - return; + return Promise.reject(); } else { // we're going ahead with a replot now gd._replotPending = false; @@ -1581,7 +1581,7 @@ Plotly.restyle = function restyle(gd, astr, val, traces) { } else { console.log('restyle fail',astr,val,traces); - return new Promise.reject(); + return Promise.reject(); } if(Object.keys(aobj).length) gd.changed = true; @@ -2104,7 +2104,7 @@ Plotly.relayout = function relayout(gd, astr, val) { gd = getGraphDiv(gd); if(gd.framework && gd.framework.isPolar) { - return new Promise.resolve(gd); + return Promise.resolve(gd); } var layout = gd.layout, @@ -2122,7 +2122,7 @@ Plotly.relayout = function relayout(gd, astr, val) { else if(Lib.isPlainObject(astr)) aobj = astr; else { console.log('relayout fail',astr,val); - return new Promise.reject(); + return Promise.reject(); } if(Object.keys(aobj).length) gd.changed = true; diff --git a/test/jasmine/tests/plot_promise_test.js b/test/jasmine/tests/plot_promise_test.js index 981d5c30d8d..56b822b3915 100644 --- a/test/jasmine/tests/plot_promise_test.js +++ b/test/jasmine/tests/plot_promise_test.js @@ -1,4 +1,5 @@ var Plotly = require('@src/plotly'); +var Events = require('@src/lib/events'); var createGraphDiv = require('../assets/create_graph_div'); var destroyGraphDiv = require('../assets/destroy_graph_div'); @@ -29,6 +30,64 @@ describe('Plotly.___ methods', function() { }); }); + describe('Plotly.plot promise', function() { + var gd, + promise, + promiseRejected = false; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }]; + + gd = createGraphDiv(); + + Events.init(gd); + + gd.on('plotly_beforeplot', function() { + return false; + }); + + promise = Plotly.plot(gd, data, {}); + + promise.then(null, function(){ + promiseRejected = true; + done(); + }); + }); + + afterEach(destroyGraphDiv); + + it('should be rejected when plotly_beforeplot event handlers return false', function() { + expect(promiseRejected).toBe(true); + }); + }); + + describe('Plotly.plot promise', function() { + var gd, + promise, + promiseRejected = false; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }]; + + gd = createGraphDiv(); + + gd._dragging = true; + + promise = Plotly.plot(gd, data, {}); + + promise.then(null, function(){ + promiseRejected = true; + done(); + }); + }); + + afterEach(destroyGraphDiv); + + it('should reject the promise when graph is being dragged', function() { + expect(promiseRejected).toBe(true); + }); + }); + describe('Plotly.redraw promise', function() { var promise, promiseGd; @@ -272,17 +331,42 @@ describe('Plotly.___ methods', function() { }); }); + describe('Plotly.restyle promise', function() { + var promise, + promiseRejected = false; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }], + initialDiv = createGraphDiv(); + + Plotly.plot(initialDiv, data, {}); + + promise = Plotly.restyle(initialDiv, undefined, ''); + + promise.then(null, function(){ + promiseRejected = true; + done(); + }); + }); + afterEach(destroyGraphDiv); + + it('should be rejected when the attribute is missing', function() { + expect(promiseRejected).toBe(true); + }); + }); + describe('Plotly.relayout promise', function() { var promise, promiseGd; beforeEach(function(done) { var data = [{ x: [1,2,3], y: [4,5,6] }], + layout = {hovermode:'closest'}, initialDiv = createGraphDiv(); - Plotly.plot(initialDiv, data, {}); + Plotly.plot(initialDiv, data, layout); - promise = Plotly.relayout(initialDiv, 'title', 'Promise test!'); + promise = Plotly.relayout(initialDiv, 'hovermode', false); promise.then(function(gd){ promiseGd = gd; @@ -299,4 +383,85 @@ describe('Plotly.___ methods', function() { }); }); + describe('Plotly.relayout promise', function() { + var promise, + promiseGd; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }], + layout = {hovermode:'closest'}, + initialDiv = createGraphDiv(); + + Plotly.plot(initialDiv, data, layout); + + promise = Plotly.relayout(initialDiv, 'hovermode', false); + + promise.then(function(gd){ + promiseGd = gd; + done(); + }); + }); + afterEach(destroyGraphDiv); + + it('should be returned with the graph div as an argument', function() { + expect(promiseGd).toBeDefined(); + expect(typeof promiseGd).toBe('object'); + expect(promiseGd.data).toBeDefined(); + expect(promiseGd.layout).toBeDefined(); + }); + }); + + describe('Plotly.relayout promise', function() { + var promise, + promiseGd; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }], + layout = {hovermode:'closest'}, + initialDiv = createGraphDiv(); + + Plotly.plot(initialDiv, data, layout); + + initialDiv.framework = { isPolar: true }; + promise = Plotly.relayout(initialDiv, 'hovermode', false); + + promise.then(function(gd){ + promiseGd = gd; + done(); + }); + }); + afterEach(destroyGraphDiv); + + it('should be returned with the graph div unchanged when the framework is polar', function() { + expect(promiseGd).toBeDefined(); + expect(typeof promiseGd).toBe('object'); + expect(promiseGd.changed).toBeFalsy(); + }); + }); + + describe('Plotly.relayout promise', function() { + var promise, + promiseRejected = false; + + beforeEach(function(done) { + var data = [{ x: [1,2,3], y: [4,5,6] }], + layout = {hovermode:'closest'}, + initialDiv = createGraphDiv(); + + Plotly.plot(initialDiv, data, layout); + + promise = Plotly.relayout(initialDiv, undefined, false); + + promise.then(null, function(){ + promiseRejected = true; + done(); + }); + }); + afterEach(destroyGraphDiv); + + it('should be rejected when the attribute is missing', function() { + expect(promiseRejected).toBe(true); + }); + }); + });