forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(onKeyStroke): support capture all handler (vueuse#2197)
- Loading branch information
Showing
4 changed files
with
119 additions
and
11 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
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
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,76 @@ | ||
import { vi } from 'vitest' | ||
import type { Ref } from 'vue-demi' | ||
import { ref } from 'vue-demi' | ||
import type { KeyStrokeEventName } from '.' | ||
import { onKeyStroke } from '.' | ||
|
||
describe('onKeyStroke', () => { | ||
let element: Ref<HTMLElement> | ||
let callBackFn: any | ||
|
||
beforeEach(() => { | ||
element = ref(document.createElement('div')) | ||
callBackFn = vi.fn() | ||
}) | ||
|
||
function createKeyEvent(key: string, type: KeyStrokeEventName) { | ||
const ev = new KeyboardEvent(type, { key }) | ||
element.value.dispatchEvent(ev) | ||
} | ||
|
||
it('listen to single key', () => { | ||
onKeyStroke('A', callBackFn, { target: element }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
expect(callBackFn).toBeCalledTimes(1) | ||
}) | ||
|
||
it('listen to multi keys', () => { | ||
onKeyStroke(['A', 'B', 'C'], callBackFn, { target: element }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
createKeyEvent('C', 'keydown') | ||
createKeyEvent('D', 'keydown') | ||
expect(callBackFn).toBeCalledTimes(3) | ||
}) | ||
|
||
it('use function filter', () => { | ||
const filter = (event: KeyboardEvent) => { | ||
return event.key === 'A' | ||
} | ||
onKeyStroke(filter, callBackFn, { target: element }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
createKeyEvent('C', 'keydown') | ||
expect(callBackFn).toBeCalledTimes(1) | ||
}) | ||
|
||
it('listen to all keys by boolean', () => { | ||
onKeyStroke(true, callBackFn, { target: element }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
createKeyEvent('C', 'keydown') | ||
createKeyEvent('D', 'keydown') | ||
createKeyEvent('E', 'keydown') | ||
expect(callBackFn).toBeCalledTimes(5) | ||
}) | ||
|
||
it('listen to all keys by constructor', () => { | ||
onKeyStroke(callBackFn, { target: element }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
createKeyEvent('C', 'keydown') | ||
createKeyEvent('D', 'keydown') | ||
createKeyEvent('E', 'keydown') | ||
expect(callBackFn).toBeCalledTimes(5) | ||
}) | ||
|
||
it('listen to keypress', () => { | ||
onKeyStroke('A', callBackFn, { target: element, eventName: 'keypress' }) | ||
createKeyEvent('A', 'keydown') | ||
createKeyEvent('B', 'keydown') | ||
createKeyEvent('A', 'keypress') | ||
createKeyEvent('B', 'keypress') | ||
expect(callBackFn).toBeCalledTimes(1) | ||
}) | ||
}) |
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