36 lines
985 B
JavaScript
36 lines
985 B
JavaScript
import { Action } from '../../src/input.js'
|
|
import { it, assert, assertCallback } from '../test.js'
|
|
import { ActionMap, Button } from '/src/input.js'
|
|
|
|
const dispatch = (name, event) => {
|
|
document.dispatchEvent(name, event)
|
|
}
|
|
|
|
const keyEvent = (type, code, args = {}) => {
|
|
dispatch(new KeyboardEvent(type, { code, ...args }))
|
|
}
|
|
|
|
const pressKey = (code, args) => {
|
|
keyEvent('keydown', code, args)
|
|
}
|
|
|
|
const releaseKey = (code, args) => {
|
|
keyEvent('keyup', code, args)
|
|
}
|
|
|
|
export default [
|
|
it('should detect keyboard presses', () => {
|
|
const map = new ActionMap()
|
|
map.bind(Action.keyboard('KeyV'), assertCallback())
|
|
pressKey('KeyV')
|
|
|
|
map.bind(Action.keyboard('Ctrl+Shift+KeyV'), assertCallback())
|
|
pressKey('KeyV', { ctrlKey: true, shiftKey: true })
|
|
}),
|
|
|
|
it('should detect gamepad presses', () => {
|
|
const map = new ActionMap()
|
|
map.bind(Action.gamepad([Button.ButtonSouth]), () => console.log('a'))
|
|
})
|
|
]
|
|
|