curated2: !46
This commit is contained in:
commit
8e7320d400
6 files changed
+130
-11
No files matched your search
@@ -27,8 +27,13 @@ function useBodyPaths() {
|
||||
}
|
||||
|
||||
function View({ view, levels, onMuscle, selected }) {
|
||||
const activate = (event, slug) => {
|
||||
if (event.key !== 'Enter' && event.key !== ' ') return
|
||||
if (event.key === ' ') event.preventDefault()
|
||||
onMuscle(slug)
|
||||
}
|
||||
return (
|
||||
<svg className="bm-v" viewBox={view.vb} role="img">
|
||||
<svg className="bm-v" viewBox={view.vb} role={onMuscle ? 'group' : 'img'}>
|
||||
{INERT.map(slug => (view.p[slug] || []).map((d, i) =>
|
||||
<path key={slug + i} className="bm-sil" d={d} />))}
|
||||
{MUSCLES.map(slug => (view.p[slug] || []).map((d, i) =>
|
||||
@@ -37,6 +42,11 @@ function View({ view, levels, onMuscle, selected }) {
|
||||
className={'bm-m l' + (levels[slug] || 0) + (selected === slug ? ' sel' : '')}
|
||||
d={d}
|
||||
onClick={onMuscle ? () => onMuscle(slug) : undefined}
|
||||
onKeyDown={onMuscle ? event => activate(event, slug) : undefined}
|
||||
role={onMuscle ? 'button' : undefined}
|
||||
tabIndex={onMuscle ? 0 : undefined}
|
||||
aria-label={onMuscle ? t(MUSCLE_NAME[slug]) : undefined}
|
||||
aria-pressed={onMuscle ? selected === slug : undefined}
|
||||
>
|
||||
<title>{t(MUSCLE_NAME[slug])}</title>
|
||||
</path>))}
|
||||
@@ -66,7 +76,7 @@ export default function BodyMap({ load = {}, thresholds, body = 'male', onMuscle
|
||||
}
|
||||
|
||||
export function BodyMapLegend() {
|
||||
return <div className="hm-legend">
|
||||
return <div className="hm-legend" aria-label={`${t('Less')} ${t('More')}`}>
|
||||
{t('Less')} <div className="hm-c l0" /><div className="hm-c l1" /><div className="hm-c l2" />
|
||||
<div className="hm-c l3" /><div className="hm-c l4" /> {t('More')}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// @vitest-environment happy-dom
|
||||
import React, { act } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import BodyMap, { BodyMapLegend } from './BodyMap.jsx'
|
||||
|
||||
globalThis.IS_REACT_ACT_ENVIRONMENT = true
|
||||
|
||||
let container
|
||||
let root
|
||||
|
||||
beforeEach(() => {
|
||||
container = document.createElement('div')
|
||||
document.body.appendChild(container)
|
||||
root = createRoot(container)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
act(() => root.unmount())
|
||||
container.remove()
|
||||
})
|
||||
|
||||
async function renderMap(props = {}) {
|
||||
await act(async () => {
|
||||
root.render(<BodyMap load={{ chest: 2 }} {...props} />)
|
||||
})
|
||||
await vi.waitFor(() => expect(container.querySelectorAll('.bm-v')).toHaveLength(2))
|
||||
return container.querySelector('.bm-m[aria-label="Chest"]')
|
||||
}
|
||||
|
||||
describe('BodyMap interaction semantics', () => {
|
||||
it('keeps noninteractive maps as images without focusable muscle controls', async () => {
|
||||
await renderMap()
|
||||
expect(container.querySelectorAll('svg[role="img"]')).toHaveLength(2)
|
||||
expect(container.querySelector('[role="button"]')).toBeNull()
|
||||
})
|
||||
|
||||
it('exposes interactive muscles as translated pressed buttons inside groups', async () => {
|
||||
const path = await renderMap({ selected: 'chest', onMuscle: vi.fn() })
|
||||
expect(container.querySelectorAll('svg[role="group"]')).toHaveLength(2)
|
||||
expect(path).toBeTruthy()
|
||||
expect(path.tabIndex).toBe(0)
|
||||
expect(path.getAttribute('aria-pressed')).toBe('true')
|
||||
})
|
||||
|
||||
it.each(['Enter', ' '])('activates a muscle once with %s', async key => {
|
||||
const onMuscle = vi.fn()
|
||||
const path = await renderMap({ onMuscle })
|
||||
const event = new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true })
|
||||
|
||||
act(() => path.dispatchEvent(event))
|
||||
|
||||
expect(onMuscle).toHaveBeenCalledTimes(1)
|
||||
expect(onMuscle).toHaveBeenCalledWith('chest')
|
||||
expect(event.defaultPrevented).toBe(key === ' ')
|
||||
})
|
||||
|
||||
it('labels the existing legend direction for assistive technology', () => {
|
||||
act(() => root.render(<BodyMapLegend />))
|
||||
expect(container.querySelector('.hm-legend').getAttribute('aria-label')).toBe('Less More')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user