Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix debounce function bug when immediate is true #243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1656,12 +1656,12 @@ Create a new function that calls _func_ with _thisArg_ and _args_.
var timeout;
return function() {
var context = this, args = arguments;
if (immediate && !timeout) func.apply(context, args);
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate) func.apply(context, args);
}, wait);
if (immediate && !timeout) func.apply(context, args);
};
}

Expand Down
53 changes: 52 additions & 1 deletion tests/unit/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,5 +752,56 @@ describe('code snippet example', () => {

assert.equal(callCount, 1);
});
})
});

describe('debounce', () => {
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
if (immediate && !timeout) func.apply(context, args);
clearTimeout(timeout);
timeout = setTimeout(function() {
timeout = null;
if (!immediate) func.apply(context, args);
}, wait);
};
}

it('debouncedFn is called once per beat when immediate is false', (done) => {
let callCount = 0;
let intervalCount = 0;
const fn = debounce(() => callCount++, 50, false);

const beat1 = setInterval(() => {
if (++intervalCount >= 3) clearInterval(beat1);
fn();
}, 10);

const beat2 = setTimeout(fn, 100);

setTimeout(() => {
if (callCount === 2 && intervalCount === 3) done();
else done(`callCount = ${callCount}`);
}, 200);
});

it('debouncedFn is called once per beat when immediate is true', (done) => {
let callCount = 0;
let intervalCount = 0;
const fn = debounce(() => callCount++, 50, true);

const beat1 = setInterval(() => {
if (++intervalCount >= 3) clearInterval(beat1);
fn();
}, 10);

const beat2 = setTimeout(fn, 100);

setTimeout(() => {
if (callCount === 2 && intervalCount === 3) done();
else done(`callCount = ${callCount}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is assertion? Honestly i don't understand this test, you should stub () => callCount++ and check how many calls debounce called this method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it's my first test unit, but I think it does work. It's true I didn't write any "assert" keyword to do the assertion, but I did use "done" which I think did the job well. Although, stub may be a good way to finish the job but I think stub is not available in mochaJs or assert module. It's not wise to import extra modules when the job can be done easily, at least for me.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ODudek Any updates?

}, 200);
});
});
});