From ad2a282e6e20f80dd49e399178c47a6e3efeec3c Mon Sep 17 00:00:00 2001 From: Nicolas Coden Date: Fri, 18 May 2018 22:13:04 +0200 Subject: [PATCH] test: wait for debounce for slow browsers in Interval debounce test Timeout delays are most often not respected and the differences between several timeouts running in parrallel can be huge. To prevent race conditions on slower browsers, we run the debounce test several time to wait for the debounced event to be called, which may be finally called way after the expected time. --- test/javascript/components/interchange.js | 26 +++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/test/javascript/components/interchange.js b/test/javascript/components/interchange.js index 5ef9fd9a3..a08b2bca5 100755 --- a/test/javascript/components/interchange.js +++ b/test/javascript/components/interchange.js @@ -1,4 +1,4 @@ -describe('Interchange', function() { +describe('Interchange', function () { var plugin; var $html; @@ -180,9 +180,11 @@ describe('Interchange', function() { // Trigger several window resize synchrnously and asynchronously. // --- - // Functions are nested to control when the check is made after the last resize - // more precisely (after 10ms). Timeout delays are most often not respected - // and the differences between several timeouts running in parrallel can be huge. + // Timeout delays are most often not respected and the differences between several + // timeouts running in parrallel can be huge. To prevent race conditions we: + // * nest timeout in order to make the delay between them more precise + // * run the test several time to wait for the debounce, which may be finally + // called way after the expected time. setTimeout(function () { let spy = sinon.spy(plugin, '_reflow'); $(window).trigger('resize'); @@ -192,12 +194,18 @@ describe('Interchange', function() { $(window).trigger('resize'); $(window).trigger('resize'); - setTimeout(function () { - sinon.assert.calledOnce(spy); + tryInterval({ + interval: debounce, + timeout: 1000, + try: () => { + sinon.assert.calledOnce(spy); + }, + then: () => { + $.triggersInitialized = false; + done(); + }, + }); - $.triggersInitialized = false; - done(); - }, debounce + 1); }); }); }); -- 2.47.2