|
| 1 | +import type { KeyDown, KeyEvent } from '@bomb.sh/tty'; |
| 2 | +import type { Node } from './core.ts'; |
| 3 | +import { describe, expect, test } from 'vitest'; |
| 4 | +import { DispatchApi, useDispatch } from './dispatch.ts'; |
| 5 | +import { setFocus, setFocusable, useFocus } from './focus.ts'; |
| 6 | +import { createHost, type Host } from './host.ts'; |
| 7 | +import { KeyboardApi, useKeyboard } from './keyboard.ts'; |
| 8 | + |
| 9 | +const { dispatch } = DispatchApi.methods; |
| 10 | + |
| 11 | +describe('keyboard dispatch', () => { |
| 12 | + test('routes keydown, keyup, and keyrepeat to the currently focused node', () => { |
| 13 | + const host = setupKeyboardHost(); |
| 14 | + const first = createFocusable(host); |
| 15 | + const second = createFocusable(host); |
| 16 | + const firstEvents: RecordedKeyEvent[] = []; |
| 17 | + const secondEvents: RecordedKeyEvent[] = []; |
| 18 | + recordKeyboard(first, firstEvents); |
| 19 | + recordKeyboard(second, secondEvents); |
| 20 | + setFocus(second); |
| 21 | + |
| 22 | + const events: KeyEvent[] = [ |
| 23 | + { type: 'keydown', key: 'a', code: 'a', text: 'a' }, |
| 24 | + { type: 'keyup', key: 'a', code: 'a' }, |
| 25 | + { type: 'keyrepeat', key: 'a', code: 'a', text: 'a' }, |
| 26 | + ]; |
| 27 | + for (const event of events) { |
| 28 | + expect(dispatch(host.root, event).ok).toBe(true); |
| 29 | + } |
| 30 | + |
| 31 | + expect(firstEvents).toEqual([]); |
| 32 | + expect(secondEvents.map(({ node }) => node)).toEqual([second, second, second]); |
| 33 | + for (const [index, event] of events.entries()) { |
| 34 | + expect(secondEvents[index]?.event).toBe(event); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + test('returns ordinary dispatch success without a handled payload', () => { |
| 39 | + const host = setupKeyboardHost(); |
| 40 | + const target = createFocusable(host); |
| 41 | + recordKeyboard(target, []); |
| 42 | + |
| 43 | + expect(dispatch(host.root, keydown('a', 'a'))).toEqual({ ok: true }); |
| 44 | + }); |
| 45 | + |
| 46 | + test('delegates non-key and malformed key-like events unchanged', () => { |
| 47 | + const host = setupKeyboardHost(); |
| 48 | + const delegated: unknown[] = []; |
| 49 | + DispatchApi.around(host.root, { |
| 50 | + dispatch([node, event], next) { |
| 51 | + delegated.push(event); |
| 52 | + return next(node, event); |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + dispatch(host.root, keydown('a', 'a')); |
| 57 | + const others: unknown[] = [ |
| 58 | + { type: 'resize', width: 80, height: 24 }, |
| 59 | + { type: 'keydown', code: 'a' }, |
| 60 | + { type: 'keydown', key: 'a' }, |
| 61 | + { type: 'keypress', key: 'a', code: 'a' }, |
| 62 | + ]; |
| 63 | + for (const event of others) { |
| 64 | + dispatch(host.root, event); |
| 65 | + } |
| 66 | + |
| 67 | + expect(delegated).toEqual(others); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +interface RecordedKeyEvent { |
| 72 | + node: Node; |
| 73 | + event: KeyEvent; |
| 74 | +} |
| 75 | + |
| 76 | +function setupKeyboardHost(): Host { |
| 77 | + const host = createHost(); |
| 78 | + useDispatch(host); |
| 79 | + useFocus(host); |
| 80 | + useKeyboard(host); |
| 81 | + return host; |
| 82 | +} |
| 83 | + |
| 84 | +function createFocusable(host: Host): Node { |
| 85 | + const element = host.createElement('box'); |
| 86 | + host.insertBefore(host.element, element); |
| 87 | + setFocusable(element.node!); |
| 88 | + return element.node!; |
| 89 | +} |
| 90 | + |
| 91 | +function keydown(code: KeyDown['code'], text?: string): KeyDown { |
| 92 | + return { |
| 93 | + type: 'keydown', |
| 94 | + key: text ?? code, |
| 95 | + code, |
| 96 | + ...(text === undefined ? {} : { text }), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +function recordKeyboard(node: Node, events: RecordedKeyEvent[]): void { |
| 101 | + KeyboardApi.around(node, { |
| 102 | + keydown([target, event]) { |
| 103 | + events.push({ node: target, event }); |
| 104 | + }, |
| 105 | + keyup([target, event]) { |
| 106 | + events.push({ node: target, event }); |
| 107 | + }, |
| 108 | + keyrepeat([target, event]) { |
| 109 | + events.push({ node: target, event }); |
| 110 | + }, |
| 111 | + }); |
| 112 | +} |
0 commit comments