Skip to content

Commit

Permalink
test: improve tests of array and function (#32)
Browse files Browse the repository at this point in the history
* test(array): improve tests

* test(function): improve tests
  • Loading branch information
LoTwT authored Nov 18, 2024
1 parent 269b2c7 commit 300d780
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tests/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
33 changes: 33 additions & 0 deletions tests/function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineConfig({
coverage: {
provider: 'istanbul',
include: ['src/**/*.ts'],
exclude: ['src/function/NOOP.ts'],
},
},
})

0 comments on commit 300d780

Please sign in to comment.