-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): add event listener for ESC key handler (#404)
* feat(SHAPE-2151): add event listener for ESC key handler * lint: add eslint fixes * test: include main functionalities testing * fix type error * fix lint error --------- Co-authored-by: Eunjae Lee <[email protected]> Co-authored-by: Demetrius Feijóo <[email protected]>
- Loading branch information
1 parent
230ff70
commit 4279218
Showing
4 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
72 changes: 72 additions & 0 deletions
72
packages/field-plugin/src/createFieldPlugin/createKeydownEscListener.test.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { createKeydownEscListener } from './createKeydownEscListener' | ||
import { MockInstance } from 'vitest' | ||
|
||
describe('createKeydownEscListener', () => { | ||
let addEventListenerSpy: MockInstance | ||
let removeEventListenerSpy: MockInstance | ||
const escapeEvent = new KeyboardEvent('keydown', { key: 'Escape' }) | ||
|
||
beforeEach(() => { | ||
addEventListenerSpy = vi.spyOn(document, 'addEventListener') | ||
removeEventListenerSpy = vi.spyOn(document, 'removeEventListener') | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('should call addEventListener with keydown and handleEsc', () => { | ||
const mockOnPressed = vi.fn() | ||
|
||
createKeydownEscListener(mockOnPressed) | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith( | ||
'keydown', | ||
expect.any(Function), | ||
) | ||
}) | ||
|
||
it('should trigger onPressed when Escape key is pressed', () => { | ||
const mockOnPressed = vi.fn() | ||
|
||
const removeListener = createKeydownEscListener(mockOnPressed) | ||
|
||
document.dispatchEvent(escapeEvent) | ||
|
||
expect(mockOnPressed).toHaveBeenCalled() | ||
|
||
removeListener() | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith( | ||
'keydown', | ||
expect.any(Function), | ||
) | ||
}) | ||
|
||
it('should not trigger onPressed when a non-Escape key is pressed', () => { | ||
const mockOnPressed = vi.fn() | ||
|
||
createKeydownEscListener(mockOnPressed) | ||
|
||
const enterEvent = new KeyboardEvent('keydown', { key: 'Enter' }) | ||
document.dispatchEvent(enterEvent) | ||
|
||
expect(mockOnPressed).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should not trigger onPressed when the cleanup function is called', () => { | ||
const mockOnPressed = vi.fn() | ||
const removeListener = createKeydownEscListener(mockOnPressed) | ||
|
||
removeListener() | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith( | ||
'keydown', | ||
expect.any(Function), | ||
) | ||
|
||
document.dispatchEvent(escapeEvent) | ||
|
||
expect(mockOnPressed).not.toHaveBeenCalled() | ||
}) | ||
}) |
15 changes: 15 additions & 0 deletions
15
packages/field-plugin/src/createFieldPlugin/createKeydownEscListener.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @returns function for cleaning up side effects | ||
*/ | ||
|
||
export const createKeydownEscListener = (onPressed: () => void) => { | ||
const handleEsc = (event: KeyboardEvent) => { | ||
const key = event.key | ||
if (key === 'Escape') { | ||
onPressed() | ||
} | ||
} | ||
|
||
document.addEventListener('keydown', handleEsc) | ||
return () => document.removeEventListener('keydown', handleEsc) | ||
} |
This file contains 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