Skip to content

Commit

Permalink
test: add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiboss committed Oct 30, 2024
1 parent dd1561d commit f2f7b43
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/react-use/src/use-key-stroke-once/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { describe, expect, it } from 'vitest'
import { act, renderHook } from '@/test'
import { describe, expect, it, vi } from 'vitest'
import { useKeyStrokeOnce } from './index'

describe('useKeyStrokeOnce', () => {
it('should defined', () => {
expect(useKeyStrokeOnce).toBeDefined()
})

it('should return initial value', () => {
const callback = vi.fn()

renderHook(() => useKeyStrokeOnce('a', callback))

expect(callback).toHaveBeenCalledTimes(0)

act(() => {
const event = new KeyboardEvent('keydown', { key: 'a' })
window.dispatchEvent(event)
})

expect(callback).toHaveBeenCalledTimes(1)

act(() => {
const event = new KeyboardEvent('keydown', { key: 'a' })
window.dispatchEvent(event)
})

expect(callback).toHaveBeenCalledTimes(1) // should not be called again
})
})

0 comments on commit f2f7b43

Please sign in to comment.