diff --git a/packages/beeq/src/shared/utils/__tests__/debounce.spec.ts b/packages/beeq/src/shared/utils/__tests__/debounce.spec.ts index 49123c257..e7a8b0df3 100644 --- a/packages/beeq/src/shared/utils/__tests__/debounce.spec.ts +++ b/packages/beeq/src/shared/utils/__tests__/debounce.spec.ts @@ -69,4 +69,13 @@ describe(debounce.name, () => { expect(spy).toHaveBeenNthCalledWith(1, '0'); expect(spy).toHaveBeenLastCalledWith('test value'); }); + + it('should not fail if cancel is called but the function is not initialised yet', () => { + const spy = jest.fn(); + + const fn = debounce(spy, 250, true); + + expect(fn.cancel).not.toThrow(); + expect(fn.cancel).not.toThrowError(new TypeError('cancel is not a function')); + }); }); diff --git a/packages/beeq/src/shared/utils/debounce.ts b/packages/beeq/src/shared/utils/debounce.ts index 1596599f7..085da633a 100644 --- a/packages/beeq/src/shared/utils/debounce.ts +++ b/packages/beeq/src/shared/utils/debounce.ts @@ -38,7 +38,7 @@ export const debounce = (func: TFunc, wait = 0, immedia return Object.assign(debounceHandler, { cancel: () => { - cancel(); + cancel?.(); }, }); };