<commit_before>/**
 * NOTE:
 *
 * as all these tests depend on timings, they are quite fragile.
 */

QUnit.test(
    "only call debounced function once if called in short order",
    function (assert)
    {
        var done = assert.async();
        var called = 0;
        var debounced = mojave.timing.debounce(function () { called += 1; }, 100);

        debounced();
        debounced();
        debounced();

        window.setTimeout(function () {
            assert.equal(called, 1, "the debounced function should only been called once");
            done();
        }, 1000);
    }
);


QUnit.test(
    "debounced functions can be called multiple times if the time between the calls is long enough",
    function (assert)
    {
        var done = assert.async();
        var called = 0;
        var debounced = mojave.timing.debounce(function () { called += 1; }, 50);

        debounced();
        window.setTimeout(debounced, 200);
        window.setTimeout(debounced, 400);

        window.setTimeout( function () {
            assert.equal(called, 3, "the debounced function should have been called three times");
            done();
        }, 1000);
    }
);


QUnit.test(
    "the debounce delay can be changed",
    function (assert)
    {
        var done = assert.async();
        var called = 0;
        var debounced = mojave.timing.debounce(function () { called += 1; }, 300);

        debounced();
        window.setTimeout(debounced, 100);
        window.setTimeout(debounced, 500);

        window.setTimeout(function () {
            assert.equal(called, 2, "the debounced function should have been called two times");
            done();
        }, 1000);
    }
);<commit_msg>Make debounce() tests more robust (and don’t collide with the global timeout)<commit_after>QUnit.module("timing/debounce");

import {debounce} from "../../../timing/timing";

/**
 * NOTE:
 *
 * as all these tests depend on timings, they are quite fragile.
 */

QUnit.test(
    "only call debounced function once if called in short order",
    function (assert)
    {
        const done = assert.async();
        let called = 0;
        const debounced = debounce(
        () => {
            assert.ok(true, "debounce called");
            called += 1;

            if (called === 3)
            {
                done();
            }
        },
        100
    );

        debounced();
        debounced();
        debounced();

        window.setTimeout(function () {
            assert.equal(called, 1, "the debounced function should only been called once");
            done();
        }, 600);
    }
);


QUnit.test(
    "debounced functions can be called multiple times if the time between the calls is long enough",
    function (assert)
    {
        assert.expect(3);
        const done = assert.async();
        let called = 0;

        const debounced = debounce(
            () => {
                assert.ok(true, "debounce called");
                called += 1;

                if (called === 3)
                {
                    done();
                }
            },
            50
        );

        debounced();
        window.setTimeout(debounced, 200);
        window.setTimeout(debounced, 400);
    }
);


QUnit.test(
    "the debounce delay can be changed",
    function (assert)
    {
        const done = assert.async();
        let called = 0;
        const debounced = debounce(
            () => {
                assert.ok(true, "debounce called");
                called += 1;

                if (called === 2)
                {
                    done();
                }
            },
            300
        );

        debounced();
        window.setTimeout(debounced, 100);
        window.setTimeout(debounced, 500);
    }
);
{'fraction_non_alphanumeric': 0.1197668256491786, 'fraction_numerical': 0.01987281399046105, 'mean_word_length': 1.6454099509460407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '903020', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1003, 'n_words': 298}