Skip to content
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
24 changes: 24 additions & 0 deletions src/compat/function/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@
expect(actual).toEqual([false, true]);
});

// FIXME: flaky test

Check warning on line 362 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected 'FIXME' comment: 'FIXME: flaky test'
it.skip('should queue a trailing call for subsequent debounced calls after `maxWait`', async () => {

Check warning on line 363 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Disabled test - if you want to skip a test temporarily, use .todo() instead
let callCount = 0;

const debounced = debounce(
Expand Down Expand Up @@ -405,16 +405,16 @@
});

it('should invoke the trailing call with the correct arguments and `this` binding', async () => {
let actual: any;

Check warning on line 408 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
let callCount = 0;
const object = {};

const debounced = debounce(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function (this: any, _: any) {

Check warning on line 414 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

Check warning on line 414 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
actual = [this];
// eslint-disable-next-line prefer-rest-params
Array.prototype.push.apply(actual, arguments as any);

Check warning on line 417 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return ++callCount !== 2;
},
32,
Expand All @@ -439,7 +439,7 @@
const methodName = 'debounce';

it(`\`_.${methodName}\` should not error for non-object \`options\` values`, () => {
expect(() => func(noop, 32, 1 as any)).not.toThrow();

Check warning on line 442 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
});

it(`\`_.${methodName}\` should use a default \`wait\` of \`0\``, async () => {
Expand All @@ -457,9 +457,9 @@
});

it(`\`_.${methodName}\` should invoke \`func\` with the correct \`this\` binding`, async () => {
const actual: any[] = [];

Check warning on line 460 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const object = {
funced: func(function (this: any) {

Check warning on line 462 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
actual.push(this);
}, 32),
};
Expand All @@ -472,7 +472,7 @@
});

it(`\`_.${methodName}\` supports recursive calls`, async () => {
const actual: any[] = [];

Check warning on line 475 in src/compat/function/debounce.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const args = ['a', 'b', 'c'].map(chr => [{}, chr]);
const expected = args.slice();
const queue: any[] = args.slice();
Expand Down Expand Up @@ -559,6 +559,30 @@
expect(callCount).toBe(0);
});

it('should not invoke the function with `maxWait` when both `leading` and `trailing` are `false`', async () => {
let callCount = 0;

const debounced = debounce(
() => {
callCount++;
},
64,
{ leading: false, trailing: false, maxWait: 64 }
);

debounced();
debounced();

await delay(96);

debounced();
debounced();

await delay(96);

expect(callCount).toBe(0);
});

it('should match the type of lodash', () => {
expectTypeOf(debounce).toEqualTypeOf<typeof debounceLodash>();
});
Expand Down
5 changes: 4 additions & 1 deletion src/compat/function/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export function debounce<F extends (...args: any[]) => any>(
}

if (Date.now() - pendingAt >= maxWait) {
result = func.apply(this, args);
if (leading || trailing) {
result = func.apply(this, args);
}

pendingAt = Date.now();

_debounced.cancel();
Expand Down
24 changes: 24 additions & 0 deletions src/compat/function/throttle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,30 @@ describe('throttle', () => {
expectTypeOf(throttle).toEqualTypeOf<typeof throttleLodash>();
});

it('should not invoke the function when both `leading` and `trailing` are `false`', async () => {
let callCount = 0;

const throttled = throttle(
() => {
callCount++;
},
64,
{ leading: false, trailing: false }
);

throttled();
throttled();

await delay(96);

throttled();
throttled();

await delay(96);

expect(callCount).toBe(0);
});

it('should not invoke the function even after flush is called if timer is going', async () => {
let callCount = 0;
const throttled = throttle(() => ++callCount, 32);
Expand Down