diff --git a/tests/array.spec.ts b/tests/array.spec.ts index 7c54bc6..66803df 100644 --- a/tests/array.spec.ts +++ b/tests/array.spec.ts @@ -37,8 +37,12 @@ it('normalizeToArray', () => { it('removeItem', () => { const arr = [1, 2, 3, 4] - removeItem(arr, 2) + const removed = removeItem(arr, 2) expect(arr).toEqual([1, 3, 4]) + expect(removed).toEqual([2]) + + expect(removeItem([], undefined)).toEqual(undefined) + expect(removeItem([1], 2)).toEqual(undefined) }) it('toggleItem', () => { @@ -72,11 +76,17 @@ describe('find', () => { expect(index).toBe(4) }) - it('not found', () => { + it('not found from start', () => { const [item, index] = find([1, 2, 3, 4, 3], (item) => item === 5) expect(item).toBe(null) expect(index).toBe(-1) }) + + it('not found from end', () => { + const [item, index] = find([1, 2, 3, 4, 3], (item) => item === 5, 'end') + expect(item).toBe(null) + expect(index).toBe(-1) + }) }) it('at', () => { diff --git a/tests/function.spec.ts b/tests/function.spec.ts index 8008d6a..3acced9 100644 --- a/tests/function.spec.ts +++ b/tests/function.spec.ts @@ -3,6 +3,21 @@ import { debounce, throttle, call, once } from '../src' describe('Utility Functions', () => { describe('debounce', () => { + it('should call the function with the default delay', async () => { + const fn = vi.fn() + const debouncedFn = debounce(fn) + + debouncedFn() + debouncedFn() + debouncedFn() + + await new Promise((resolve) => { + setTimeout(resolve, 250) + }) + + expect(fn).toHaveBeenCalledTimes(1) + }) + it('should call the function after the specified delay', async () => { const fn = vi.fn() const debouncedFn = debounce(fn, 100) @@ -34,6 +49,24 @@ describe('Utility Functions', () => { }) describe('throttle', () => { + it('should call the function immediately and then at default intervals', async () => { + const fn = vi.fn() + const throttledFn = throttle(fn) + + throttledFn() + throttledFn() + await new Promise((resolve) => { + setTimeout(resolve, 50) + }) + throttledFn() + await new Promise((resolve) => { + setTimeout(resolve, 200) + }) + throttledFn() + + expect(fn).toHaveBeenCalledTimes(2) + }) + it('should call the function immediately and then at specified intervals', async () => { const fn = vi.fn() const throttledFn = throttle(fn, 100) diff --git a/vitest.config.ts b/vitest.config.ts index 207cc7b..4520702 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,6 +6,7 @@ export default defineConfig({ coverage: { provider: 'istanbul', include: ['src/**/*.ts'], + exclude: ['src/function/NOOP.ts'], }, }, })