|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import { fireEvent } from '@testing-library/dom'; |
| 3 | +import { useInputModality } from './useInputModality'; |
| 4 | + |
| 5 | +function TestContainer() { |
| 6 | + const props = useInputModality(); |
| 7 | + return ( |
| 8 | + <div |
| 9 | + data-testid="container" |
| 10 | + {...props} |
| 11 | + /> |
| 12 | + ); |
| 13 | +} |
| 14 | + |
| 15 | +describe('useInputModality', () => { |
| 16 | + function setup() { |
| 17 | + const { getByTestId } = render(<TestContainer />); |
| 18 | + return getByTestId('container') as HTMLElement; |
| 19 | + } |
| 20 | + |
| 21 | + it('sets keyboard modality for navigation keys', () => { |
| 22 | + const el = setup(); |
| 23 | + for (const key of ['ArrowUp', 'ArrowDown', 'Home', 'End', 'Enter', 'Tab']) { |
| 24 | + fireEvent.keyDown(el, { key }); |
| 25 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 26 | + delete el.dataset.inputModality; |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + it('sets keyboard modality for typeahead characters', () => { |
| 31 | + const el = setup(); |
| 32 | + for (const key of ['d', 'a', 'z']) { |
| 33 | + fireEvent.keyDown(el, { key }); |
| 34 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 35 | + delete el.dataset.inputModality; |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('sets keyboard modality for Space', () => { |
| 40 | + const el = setup(); |
| 41 | + fireEvent.keyDown(el, { key: ' ' }); |
| 42 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not set keyboard modality for bare modifier keys', () => { |
| 46 | + const el = setup(); |
| 47 | + for (const key of ['Meta', 'Control', 'Alt', 'Shift', 'CapsLock']) { |
| 48 | + fireEvent.keyDown(el, { key }); |
| 49 | + expect(el.dataset.inputModality).toBeUndefined(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + it('sets pointer modality on pointer move', () => { |
| 54 | + const el = setup(); |
| 55 | + fireEvent.keyDown(el, { key: 'ArrowDown' }); |
| 56 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 57 | + |
| 58 | + fireEvent.pointerMove(el); |
| 59 | + expect(el.dataset.inputModality).toBe('pointer'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('sets pointer modality on pointer down', () => { |
| 63 | + const el = setup(); |
| 64 | + fireEvent.keyDown(el, { key: 'ArrowDown' }); |
| 65 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 66 | + |
| 67 | + fireEvent.pointerDown(el); |
| 68 | + expect(el.dataset.inputModality).toBe('pointer'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('seeds modality from global state on focus after keyboard', () => { |
| 72 | + fireEvent.keyDown(document, { key: 'Enter' }); |
| 73 | + |
| 74 | + const el = setup(); |
| 75 | + fireEvent.focusIn(el); |
| 76 | + |
| 77 | + expect(el.dataset.inputModality).toBe('keyboard'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('seeds pointer modality from global state on focus after pointer', () => { |
| 81 | + fireEvent.pointerDown(document); |
| 82 | + |
| 83 | + const el = setup(); |
| 84 | + fireEvent.focusIn(el); |
| 85 | + |
| 86 | + expect(el.dataset.inputModality).toBe('pointer'); |
| 87 | + }); |
| 88 | +}); |
0 commit comments