Browse Source

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.
mbroad/code-climate-coverage-develop
wimrijnders 7 years ago
committed by Yotam Berkowitz
parent
commit
1ece23740f
2 changed files with 23 additions and 0 deletions
  1. +4
    -0
      lib/network/Network.js
  2. +19
    -0
      test/Network.test.js

+ 4
- 0
lib/network/Network.js View File

@ -154,6 +154,10 @@ Emitter(Network.prototype);
* @param {Object} options * @param {Object} options
*/ */
Network.prototype.setOptions = function (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) { if (options !== undefined) {
let errorFound = Validator.validate(options, allOptions); let errorFound = Validator.validate(options, allOptions);
if (errorFound === true) { if (errorFound === true) {

+ 19
- 0
test/Network.test.js View File

@ -395,6 +395,25 @@ describe('Network', function () {
createSampleNetwork(options); 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 () { describe('Node', function () {
it('has known font options', function () { it('has known font options', function () {

Loading…
Cancel
Save