From 1ece23740f40f47f68910b0eaeb0ab92a0e2e114 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Sat, 14 Oct 2017 09:42:51 +0200 Subject: [PATCH] Network: Handle null data gracefully (#3571) * Network: preload images in options for all shape types Fixes #3532 If the images option is set, preload it, even if the node shape is not `image` or `circularImage`. This needs to be done because the user can switch to an image shape later, as is happening in the issue. Option `image` is only mandatory for the image shapes. There is no unit test for this, because it would need a mock object for Image and I didn't succeed in creating/finding one. * Network: Handle null data gracefully During testing I discovered that passing `null` for network data leads to a thrown error. This adds a guard to prevent it, plus unit tests for regression. --- lib/network/Network.js | 4 ++++ test/Network.test.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/network/Network.js b/lib/network/Network.js index f5980b20..486a8a21 100644 --- a/lib/network/Network.js +++ b/lib/network/Network.js @@ -154,6 +154,10 @@ Emitter(Network.prototype); * @param {Object} options */ Network.prototype.setOptions = function (options) { + if (options === null) { + options = undefined; // This ensures that options handling doesn't crash in the handling + } + if (options !== undefined) { let errorFound = Validator.validate(options, allOptions); if (errorFound === true) { diff --git a/test/Network.test.js b/test/Network.test.js index 19ea7428..b9f191c4 100644 --- a/test/Network.test.js +++ b/test/Network.test.js @@ -395,6 +395,25 @@ describe('Network', function () { createSampleNetwork(options); }); + + it('can deal with null data', function() { + // While we're at it, try out other silly values as well + // All the following are wrong, but none should lead to a crash + var awkwardData = [ + null, + [1,2,3], + 42, + 'meow' + ]; + + var container = document.getElementById('mynetwork'); + + for (var n = 0; n < awkwardData.length; ++n) { + var network = new vis.Network(container, awkwardData[n], {}); // Should not throw + } + }); + + describe('Node', function () { it('has known font options', function () {