import { describe, expect, it } from 'vitest' import { lands } from './choice' /** * Where an arrow key lands in a group of options. * * Worth a test of its own because getting it wrong is invisible: the selection * still changes, so the control looks like it works, and what breaks is a * keyboard reaching the last option — the one with the answer somebody wanted. * It wraps in both directions, which is what a radio group does and what * separates it from a list. */ describe('where an arrow key lands in a choice', () => { it('goes forward on down and right, and wraps at the end', () => { expect(lands('ArrowDown', 0, 4)).toBe(1) expect(lands('ArrowRight', 2, 4)).toBe(3) expect(lands('ArrowDown', 3, 4)).toBe(0) }) it('goes back on up and left, and wraps at the start', () => { expect(lands('ArrowUp', 3, 4)).toBe(2) expect(lands('ArrowLeft', 1, 4)).toBe(0) expect(lands('ArrowUp', 0, 4)).toBe(3) }) it('takes Home and End to the ends', () => { expect(lands('Home', 2, 4)).toBe(0) expect(lands('End', 0, 4)).toBe(3) }) it('leaves every other key alone, so typing and tabbing still work', () => { for (const key of ['Tab', 'Enter', ' ', 'a', 'Escape', 'PageDown']) { expect(lands(key, 1, 4)).toBeNull() } }) it('stays put where there is one option, rather than dividing by nothing', () => { expect(lands('ArrowDown', 0, 1)).toBe(0) expect(lands('ArrowUp', 0, 1)).toBe(0) }) })