Browse Source

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
revert-3409-performance
wimrijnders 7 years ago
committed by Yotam Berkowitz
parent
commit
046fb2784a
2 changed files with 8 additions and 2 deletions
  1. +6
    -2
      lib/network/modules/CanvasRenderer.js
  2. +2
    -0
      test/Network.test.js

+ 6
- 2
lib/network/modules/CanvasRenderer.js View File

@ -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;

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

@ -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');

Loading…
Cancel
Save