-
-
Notifications
You must be signed in to change notification settings - Fork 2
refactor(useKeydown): optimize keydown event handling #8
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
Open
AndreyYolkin
wants to merge
14
commits into
master
Choose a base branch
from
refactor/ae-keydown-with-registrar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2d01f84
refactor(useKeydown): optimize keydown event handling with registrar …
AndreyYolkin dd63e72
refactor: drop registrar
AndreyYolkin 789ed35
refactor: remove side-effect from map
AndreyYolkin e43f89f
Merge branch 'master' into refactor/ae-keydown-with-registrar
AndreyYolkin 4d41b31
perf: use shallowReadonly instead of readonly
AndreyYolkin f4d8bfb
Merge branch 'master' into refactor/ae-keydown-with-registrar
AndreyYolkin fb0aef3
Merge remote-tracking branch 'origin/master' into refactor/ae-keydown…
AndreyYolkin 9023613
chore: use dedicated vitest configs
AndreyYolkin edd8854
test: port tests from prev file
AndreyYolkin 86551cf
Merge branch 'master' into refactor/ae-keydown-with-registrar
AndreyYolkin 5ef5ef4
fix: use genId
AndreyYolkin 1dc6fce
chore: use named function
AndreyYolkin 3513489
Merge branch 'master' into refactor/ae-keydown-with-registrar
AndreyYolkin 242bb48
refactor(useKeydown): rename autoStart option to immediate
AndreyYolkin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,89 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' | ||
import { useKeydown } from './index' | ||
|
||
// Mock Vue's lifecycle hooks | ||
vi.mock('vue', async () => { | ||
const actual = await vi.importActual('vue') | ||
return { | ||
...actual, | ||
onMounted: vi.fn(), | ||
getCurrentScope: vi.fn(() => true), | ||
onScopeDispose: vi.fn(), | ||
} | ||
}) | ||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' | ||
import { useKeydown, handlerMap } from './index' | ||
|
||
describe('useKeydown', () => { | ||
let addEventListenerSpy: ReturnType<typeof vi.spyOn> | ||
let removeEventListenerSpy: ReturnType<typeof vi.spyOn> | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks() | ||
// Mock document.addEventListener and removeEventListener | ||
vi.spyOn(document, 'addEventListener') | ||
vi.spyOn(document, 'removeEventListener') | ||
addEventListenerSpy = vi.spyOn(document, 'addEventListener') | ||
removeEventListenerSpy = vi.spyOn(document, 'removeEventListener') | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
handlerMap.clear() | ||
}) | ||
|
||
describe('basic functionality', () => { | ||
it('should return startListening and stopListening functions', () => { | ||
const handler = { key: 'Enter', handler: vi.fn() } | ||
const result = useKeydown(handler) | ||
|
||
expect(result).toHaveProperty('startListening') | ||
expect(result).toHaveProperty('stopListening') | ||
expect(typeof result.startListening).toBe('function') | ||
expect(typeof result.stopListening).toBe('function') | ||
}) | ||
|
||
it('should accept single handler', () => { | ||
const handler = { key: 'Enter', handler: vi.fn() } | ||
|
||
expect(() => useKeydown(handler)).not.toThrow() | ||
}) | ||
|
||
it('should accept array of handlers', () => { | ||
const handlers = [ | ||
{ key: 'Enter', handler: vi.fn() }, | ||
{ key: 'Escape', handler: vi.fn() }, | ||
] | ||
|
||
expect(() => useKeydown(handlers)).not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('event handling', () => { | ||
it('should call handler when matching key is pressed', () => { | ||
const mockHandler = vi.fn() | ||
const handler = { key: 'Enter', handler: mockHandler } | ||
const { startListening } = useKeydown(handler) | ||
it('should create only one global listener for multiple useKeydown calls', () => { | ||
const handler1 = vi.fn() | ||
const handler2 = vi.fn() | ||
const handler3 = vi.fn() | ||
|
||
startListening() | ||
const keydown1 = useKeydown({ key: 'Enter', handler: handler1 }) | ||
const keydown2 = useKeydown({ key: 'Escape', handler: handler2 }) | ||
const keydown3 = useKeydown({ key: 'Space', handler: handler3 }) | ||
|
||
// Simulate keydown event | ||
const event = new KeyboardEvent('keydown', { key: 'Enter' }) | ||
document.dispatchEvent(event) | ||
keydown1.startListening() | ||
keydown2.startListening() | ||
keydown3.startListening() | ||
|
||
expect(mockHandler).toHaveBeenCalledWith(event) | ||
}) | ||
expect(addEventListenerSpy).toHaveBeenCalledTimes(1) | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('keydown', expect.any(Function)) | ||
|
||
it('should not call handler when non-matching key is pressed', () => { | ||
const mockHandler = vi.fn() | ||
const handler = { key: 'Enter', handler: mockHandler } | ||
const { startListening } = useKeydown(handler) | ||
expect(handlerMap.size).toBe(3) | ||
|
||
startListening() | ||
keydown1.stopListening() | ||
keydown2.stopListening() | ||
keydown3.stopListening() | ||
|
||
// Simulate keydown event with different key | ||
const event = new KeyboardEvent('keydown', { key: 'Escape' }) | ||
document.dispatchEvent(event) | ||
expect(removeEventListenerSpy).toHaveBeenCalledTimes(1) | ||
expect(handlerMap.size).toBe(0) | ||
}) | ||
|
||
expect(mockHandler).not.toHaveBeenCalled() | ||
}) | ||
it('should handle multiple handlers for the same key', () => { | ||
const handler1 = vi.fn() | ||
const handler2 = vi.fn() | ||
|
||
it('should prevent default when preventDefault is true', () => { | ||
const mockHandler = vi.fn() | ||
const handler = { key: 'Enter', handler: mockHandler, preventDefault: true } | ||
const { startListening } = useKeydown(handler) | ||
const keydown1 = useKeydown({ key: 'Enter', handler: handler1 }) | ||
const keydown2 = useKeydown({ key: 'Enter', handler: handler2 }) | ||
|
||
startListening() | ||
keydown1.startListening() | ||
keydown2.startListening() | ||
|
||
const event = new KeyboardEvent('keydown', { key: 'Enter' }) | ||
const preventDefaultSpy = vi.spyOn(event, 'preventDefault') | ||
document.dispatchEvent(event) | ||
const event = { key: 'Enter' } as KeyboardEvent | ||
|
||
expect(preventDefaultSpy).toHaveBeenCalled() | ||
}) | ||
const registeredHandler = addEventListenerSpy.mock.calls[0][1] as (event: KeyboardEvent) => void | ||
registeredHandler(event) | ||
|
||
it('should stop propagation when stopPropagation is true', () => { | ||
const mockHandler = vi.fn() | ||
const handler = { key: 'Enter', handler: mockHandler, stopPropagation: true } | ||
const { startListening } = useKeydown(handler) | ||
expect(handler1).toHaveBeenCalledWith(event) | ||
expect(handler2).toHaveBeenCalledWith(event) | ||
|
||
startListening() | ||
keydown1.stopListening() | ||
keydown2.stopListening() | ||
}) | ||
|
||
const event = new KeyboardEvent('keydown', { key: 'Enter' }) | ||
const stopPropagationSpy = vi.spyOn(event, 'stopPropagation') | ||
document.dispatchEvent(event) | ||
it('should not remove global listener if other handlers are still active', () => { | ||
const handler1 = vi.fn() | ||
const handler2 = vi.fn() | ||
|
||
expect(stopPropagationSpy).toHaveBeenCalled() | ||
}) | ||
}) | ||
const keydown1 = useKeydown({ key: 'Enter', handler: handler1 }) | ||
const keydown2 = useKeydown({ key: 'Escape', handler: handler2 }) | ||
|
||
describe('lifecycle management', () => { | ||
it('should add event listener when startListening is called', () => { | ||
const handler = { key: 'Enter', handler: vi.fn() } | ||
const { startListening } = useKeydown(handler) | ||
keydown1.startListening() | ||
keydown2.startListening() | ||
|
||
startListening() | ||
expect(addEventListenerSpy).toHaveBeenCalledTimes(1) | ||
|
||
expect(document.addEventListener).toHaveBeenCalledWith('keydown', expect.any(Function)) | ||
}) | ||
keydown1.stopListening() | ||
|
||
it('should remove event listener when stopListening is called', () => { | ||
const handler = { key: 'Enter', handler: vi.fn() } | ||
const { stopListening } = useKeydown(handler) | ||
expect(removeEventListenerSpy).not.toHaveBeenCalled() | ||
expect(handlerMap.size).toBe(1) | ||
|
||
stopListening() | ||
keydown2.stopListening() | ||
|
||
expect(document.removeEventListener).toHaveBeenCalledWith('keydown', expect.any(Function)) | ||
}) | ||
expect(removeEventListenerSpy).toHaveBeenCalledTimes(1) | ||
expect(handlerMap.size).toBe(0) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whenever
if { ... }
guards most of the logic within a function it should be replaced with early exitif (!..) return
, so we avoid unnecessary indentation and the inner logic is easier to read/scan-through.There are 3 methods here that would benefit from this quick refactoring.