File size: 2,763 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, expect, it, vi } from 'vitest'
import { renderToStaticMarkup } from 'react-dom/server'
import { nextMapKey, useMapKeyboard, westToEast } from './useMapKeyboard'

describe('nextMapKey (#1146)', () => {
  const keys = ['WA', 'TX', 'NY']
  it('moves left and right, wrapping', () => {
    expect(nextMapKey(keys, 'WA', 'ArrowRight')).toBe('TX')
    expect(nextMapKey(keys, 'NY', 'ArrowRight')).toBe('WA')
    expect(nextMapKey(keys, 'WA', 'ArrowLeft')).toBe('NY')
  })
  it('starts at an end when nothing is shown, and Home/End jump to the ends', () => {
    expect(nextMapKey(keys, null, 'ArrowRight')).toBe('WA')
    expect(nextMapKey(keys, null, 'ArrowLeft')).toBe('NY')
    expect(nextMapKey(keys, 'TX', 'Home')).toBe('WA')
    expect(nextMapKey(keys, 'TX', 'End')).toBe('NY')
  })
  it('leaves Up/Down to the page so a focused map still scrolls', () => {
    expect(nextMapKey(keys, 'TX', 'ArrowUp')).toBeNull()
    expect(nextMapKey(keys, 'TX', 'ArrowDown')).toBeNull()
    expect(nextMapKey([], null, 'ArrowRight')).toBeNull()
  })
})

describe('westToEast', () => {
  it('orders areas by the centre of their dots', () => {
    const dots = [{ x: 9, y: 0 }, { x: 1, y: 0 }, { x: 3, y: 0 }, { x: 5, y: 0 }]
    const ids = ['NY', 'WA', 'WA', null]
    expect(westToEast(dots, (i) => ids[i] ?? null)).toEqual(['WA', 'NY'])
  })
})

describe('useMapKeyboard focus', () => {
  /** One render of the hook, its map props kept for the test to call. */
  const setup = () => {
    const setActive = vi.fn()
    let props: { onFocus: () => void; onPointerDown: () => void } | null = null
    function Probe() {
      props = useMapKeyboard({ keys: ['WA', 'AL', 'NY'], active: null, setActive, initial: 'AL', describe: (k) => k, noun: 'state' }).svgProps
      return null
    }
    renderToStaticMarkup(<Probe />)
    return { setActive, props: props! }
  }
  it('starts keyboard focus on the page’s own area', () => {
    const { setActive, props } = setup()
    props.onFocus()
    expect(setActive).toHaveBeenCalledWith('AL')
  })
  it('does not pick the page’s area when a tap or click focuses the map (the phone button bug)', () => {
    const { setActive, props } = setup()
    props.onPointerDown()
    props.onFocus()
    expect(setActive).not.toHaveBeenCalled()
    // The next keyboard focus still starts on it.
    props.onFocus()
    expect(setActive).toHaveBeenCalledWith('AL')
  })
  it('a press that focused nothing does not skip a later keyboard focus', () => {
    vi.useFakeTimers()
    try {
      const { setActive, props } = setup()
      props.onPointerDown()
      vi.advanceTimersByTime(5000)
      props.onFocus()
      expect(setActive).toHaveBeenCalledWith('AL')
    } finally {
      vi.useRealTimers()
    }
  })
})