-
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.
test: include main functionalities testing
- Loading branch information
Showing
2 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
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 @@ | ||
/* eslint-disable functional/immutable-data */ | ||
import { createKeydownEscListener } from './createKeydownEscListener' | ||
|
||
describe('createKeydownEscListener', () => { | ||
let addEventListenerSpy: vi.SpyInstance | ||
let removeEventListenerSpy: vi.SpyInstance | ||
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() | ||
}) | ||
}) |
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