From 046fb2784a592f08de216e29944009c24b595314 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Wed, 16 Aug 2017 22:39:17 +0200 Subject: [PATCH] Enable unit testing for module Network (#3313) * Enable unit testing for module Network Adds mocks for certain components, in order to let module `Network` be run in unit tests. Changes: - Create a mock object for `hammer.js` when running browserless. The inspiration is taken from [here](https://github.com/uber/deck.gl/pull/658) - Create a mock function for `window.requestAnimationFrame()` when running browserless in `network/modules/CanvasRenderer.js` - Added unit test for `Network` to show that unit testing for it now works - Fixed naming of container in `test/Graph3d.test.js` Since `hammer.js` is also used in other modules, this fix is potentially an enabler for full-module unit tests for those as well. * Cleanup unit test Network * Fix for unit test * Added example networks to unit test * Fix error in loading disassemblerExample * Network unit test final fixes * Adjusted comments --- lib/network/modules/CanvasRenderer.js | 8 ++++++-- test/Network.test.js | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/network/modules/CanvasRenderer.js b/lib/network/modules/CanvasRenderer.js index 16d39a0b..4771687e 100644 --- a/lib/network/modules/CanvasRenderer.js +++ b/lib/network/modules/CanvasRenderer.js @@ -13,8 +13,9 @@ * * During unit testing, it happens that the window object is reset during execution, causing * a runtime error due to missing `requestAnimationFrame()`. This needs to be compensated for, * see `_requestNextFrame()`. - * * Since this is a global object, it may affect other modules besides `Network`! This has not - * caused any problems yet. The method is only used within `Network`. + * * Since this is a global object, it may affect other modules besides `Network`. With normal + * usage, this does not cause any problems. During unit testing, errors may occur. These have + * been compensated for, see comment block in _requestNextFrame(). * * @private */ @@ -127,6 +128,9 @@ class CanvasRenderer { // 'requestAnimationFrame()' is not present because it is not defined on the // mock window object. // + // As a consequence, unrelated unit tests may appear to fail, even if the problem + // described happens in the current unit test. + // // This is not something that will happen in normal operation, but we still need // to take it into account. if (window === undefined) return; diff --git a/test/Network.test.js b/test/Network.test.js index fc476b63..082d8e65 100644 --- a/test/Network.test.js +++ b/test/Network.test.js @@ -236,6 +236,7 @@ describe('Network', function () { }, clusterNodeProperties: {id:"c1", label:'c1'} } + network.cluster(clusterOptionsByData); numNodes += 1; // new cluster node assertNumNodes(network, numNodes, numNodes - 3); // 3 clustered nodes @@ -273,6 +274,7 @@ describe('on node.js', function () { describe('runs example ', function () { + function loadExample(path, noPhysics) { include(path, this); var container = document.getElementById('mynetwork');