-describe('Interchange', function() {
+describe('Interchange', function () {
var plugin;
var $html;
// 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');
$(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);
});
});
});
--- /dev/null
+(function (global, $) {
+
+ /**
+ * Try to catch the `do` function every `interval` delay, until it succedes
+ * (does not throw an error) or a given `timout` time passed.
+ * If a `then` function is passed, it is called after `do` succeded.
+ * If a `catch` function is passed, it is called after each time `do` fails.
+ *
+ * @param {object} opts
+ * @param {object} opts
+ */
+ global.tryInterval = function (opts) {
+ var _opts = $.extend({}, opts);
+ var totalTime = 0;
+
+ var interval = setInterval(function () {
+ var succeded = false;
+ var error = null;
+
+ totalTime += _opts.time;
+
+ try {
+ _opts.try();
+ succeded = true;
+ }
+ catch (err) { error = err; }
+
+ if (succeded) {
+ clearInterval(interval);
+ if (typeof _opts.then === 'function') _opts.then();
+ }
+ else if (totalTime > _opts.timeout) {
+ clearInterval(interval);
+ throw error;
+ }
+ else {
+ if (typeof _opts.catch === 'function') _opts.catch(error);
+ }
+
+ }, _opts.interval);
+ };
+
+})(window, jQuery);