From 99577988223c56c95f5efdd1cf0382a647c58be9 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Wed, 13 Sep 2017 20:32:53 +0200 Subject: [PATCH] The next fix on Travis unit test failure (#3431) This is the next escalation on the war against the Travis unit tests failing (which came into being by yours truly). By accident, I could recreate the unit test failure on my development machine. This led to a more directed effort to squash the bug. The insight here is that test `(window === undefined)` fails, but `(typeof window === 'undefined`)` succeeds. This undoubtedly has to do with the special status `window` has as a global object. Changes: - Added check on presence of `window` in `Canvas._requestNextFrame()`, fixed local source errors. - Added catch clause in `CanvasRendered._determinePixelRatio()` - small fix: raised timeout for the network `worldCup2014` unit test --- lib/network/modules/Canvas.js | 10 +++++++++- lib/network/modules/CanvasRenderer.js | 17 ++++++++++------- test/Network.test.js | 6 ++++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/network/modules/Canvas.js b/lib/network/modules/Canvas.js index ef63307a..0e53920e 100644 --- a/lib/network/modules/Canvas.js +++ b/lib/network/modules/Canvas.js @@ -373,11 +373,19 @@ class Canvas { throw new Error("Could not get canvax context"); } - return (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio || + var numerator = 1; + if(typeof window !== 'undefined') { // (window !== undefined) doesn't work here! + // Protection during unit tests, where 'window' can be missing + numerator = (window.devicePixelRatio || 1); + } + + var denominator = (ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.msBackingStorePixelRatio || ctx.oBackingStorePixelRatio || ctx.backingStorePixelRatio || 1); + + return numerator / denominator; } /** diff --git a/lib/network/modules/CanvasRenderer.js b/lib/network/modules/CanvasRenderer.js index cc9ea0fd..2a0a6b3b 100644 --- a/lib/network/modules/CanvasRenderer.js +++ b/lib/network/modules/CanvasRenderer.js @@ -149,13 +149,15 @@ class CanvasRenderer { // This is not something that will happen in normal operation, but we still need // to take it into account. // - let timer; + let timer; try { - var myWindow = window; // Grab a reference to reduce the possibility that 'window' is reset - // while running this method. - if (myWindow === undefined) return; + if(typeof window === 'undefined') { // (window === undefined) doesn't work here! + return; + } + var myWindow = window; // Grab a reference to reduce the possibility that 'window' is reset + // while running this method. if (this.requiresTimeout === true) { // wait given number of milliseconds and perform the animation step function @@ -166,9 +168,10 @@ class CanvasRenderer { } } } catch(e) { - console.warning("Got exception with message: '" + e.message() + "'"); - if (e.message() === 'window is undefined') { - console.warning("'" + e.message() + "' happened again"); + var msg = e.message; + //console.warn("Got exception with message: '" + msg + "'"); + if (msg === 'window is not defined') { + console.warn("CanvasRenderer._requestNextFrame: error '" + msg + "' happened again"); } else { throw e; } diff --git a/test/Network.test.js b/test/Network.test.js index 43a6d76f..2bfccda0 100644 --- a/test/Network.test.js +++ b/test/Network.test.js @@ -250,6 +250,7 @@ describe('Network', function () { describe('Node', function () { + /** * NOTE: choosify tests of Node and Edge are parallel * TODO: consolidate this is necessary @@ -1150,15 +1151,16 @@ describe('runs example ', function () { }); - it('WorlCup2014', function () { + it('WorlCup2014', function (done) { // This is a huge example (which is why it's tested here!), so it takes a long time to load. - this.timeout(10000); + this.timeout(15000); var network = loadExample('./examples/network/datasources/WorldCup2014.js', true); // Count in following also contains the helper nodes for dynamic edges assert.equal(Object.keys(network.body.nodes).length, 9964); assert.equal(Object.keys(network.body.edges).length, 9228); + done(); });