]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/commitdiff
test: wait for debounce for slow browsers in Interval debounce test
authorNicolas Coden <nicolas@ncoden.fr>
Fri, 18 May 2018 20:13:04 +0000 (22:13 +0200)
committerNicolas Coden <nicolas@ncoden.fr>
Fri, 18 May 2018 20:17:38 +0000 (22:17 +0200)
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

index 5ef9fd9a357a954cc9a6fa2b42245d43f45423e3..a08b2bca5a372cfb2a539eb8f922bd0dd10f6042 100755 (executable)
@@ -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);
         });
       });
     });