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

test: improve tests of array and function #32

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
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'],
},
},
})
Loading