feat(app): application on the canonical sheet — report v3 store with v2 draft migration, bundled SheetProvider (SHEET_BUNDLE), annotation layer rendered from the document, zone interaction service, pickers and cards on the sheet index, PDF v3 export with v2 archive import; legacy registry, matchers, editors and assets removed
This commit is contained in:
1 parent
0a17e390cf
commit
ce2ec26112
114 files changed
+3560
-14637
No files matched your search
@@ -1,39 +1,110 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen, waitFor } from '@testing-library/react'
|
||||
import { beforeAll, describe, expect, it, vi, afterEach } from 'vitest'
|
||||
import { cleanup, render, waitFor } from '@testing-library/react'
|
||||
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
|
||||
import { buildViewSvg, parseDrawing, sanitizeSvg } from '@/anatomy/drawing'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { loadTestSheet } from '../helpers/sheet'
|
||||
|
||||
vi.mock('@/anatomy/svgLoader', () => ({
|
||||
loadSvg: vi.fn(async () => {
|
||||
const doc = new DOMParser().parseFromString(
|
||||
`<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<circle data-anno-id="epaule" data-anno-name-fr="Épaule" data-anno-name-en="Shoulder"
|
||||
data-anno-category="articulation" class="bone osteo articulation hitbox bone-epaule" />
|
||||
<circle data-anno-id="epaule" data-anno-name-fr="Épaule" data-anno-name-en="Shoulder"
|
||||
data-anno-category="articulation" class="bone osteo articulation visible bone-epaule" />
|
||||
</svg>`,
|
||||
'image/svg+xml',
|
||||
)
|
||||
return doc.documentElement as unknown as SVGSVGElement
|
||||
}),
|
||||
}))
|
||||
let sheet: SheetIndex
|
||||
beforeAll(async () => {
|
||||
sheet = await loadTestSheet()
|
||||
})
|
||||
afterEach(cleanup)
|
||||
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
describe('AnatomyCanvas', () => {
|
||||
it('renders the loaded SVG inline', async () => {
|
||||
render(<AnatomyCanvas src="/x.svg" boneStates={new Map()} onZoneClick={() => {}} />)
|
||||
await waitFor(() =>
|
||||
expect(document.querySelector('[data-anno-id="epaule"]')).not.toBeNull(),
|
||||
describe('buildViewSvg', () => {
|
||||
it('frames the main view on the asset box and the head view on its crop of the same drawing', () => {
|
||||
const main = buildViewSvg(sheet, 'main', new Map())
|
||||
const head = buildViewSvg(sheet, 'head', new Map())
|
||||
expect(main.getAttribute('viewBox')).toBe('0 0 2073.4 1629.4')
|
||||
expect(head.getAttribute('viewBox')).toBe('60 100 500 380')
|
||||
expect(main.querySelectorAll('path').length).toBe(
|
||||
head.querySelectorAll('path').length,
|
||||
)
|
||||
expect(main.querySelectorAll('[data-layer="annotations"]')).toHaveLength(1)
|
||||
expect(main.querySelectorAll('[data-anno-id="hip"]')).toHaveLength(2)
|
||||
expect(head.querySelectorAll('[data-anno-id="hip"]')).toHaveLength(0)
|
||||
expect(head.querySelectorAll('[data-anno-id="atm"]')).toHaveLength(2)
|
||||
expect(main.hasAttribute('width')).toBe(false)
|
||||
})
|
||||
|
||||
it('calls onZoneClick with the clicked bone id', async () => {
|
||||
const onZoneClick = vi.fn()
|
||||
render(<AnatomyCanvas src="/x.svg" boneStates={new Map()} onZoneClick={onZoneClick} />)
|
||||
await waitFor(() => screen.getByTestId('anatomy-canvas'))
|
||||
;(document.querySelector('[data-anno-id="epaule"]') as Element).dispatchEvent(
|
||||
new Event('click', { bubbles: true }),
|
||||
it('applies states as data-state on the generated markers', () => {
|
||||
const svg = buildViewSvg(sheet, 'main', new Map([['stifle', 'current']]))
|
||||
const marked = Array.from(
|
||||
svg.querySelectorAll('[data-state="current"]'),
|
||||
).map((el) => el.getAttribute('data-anno-id'))
|
||||
expect(marked).toEqual(['stifle', 'stifle'])
|
||||
})
|
||||
|
||||
it('sanitises scripts, embedded documents and handlers out of an asset', () => {
|
||||
const root = parseDrawing(
|
||||
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><script>alert(1)</script><g onclick="x()"><a href="javascript:void(0)"><path d="M0 0"/></a></g><foreignObject/></svg>',
|
||||
)
|
||||
await waitFor(() => expect(onZoneClick).toHaveBeenCalledWith('epaule'))
|
||||
sanitizeSvg(root)
|
||||
expect(root.querySelector('script')).toBeNull()
|
||||
expect(root.querySelector('foreignObject')).toBeNull()
|
||||
expect(root.querySelector('g')!.hasAttribute('onclick')).toBe(false)
|
||||
expect(root.querySelector('a')!.hasAttribute('href')).toBe(false)
|
||||
expect(root.querySelector('path')).not.toBeNull()
|
||||
expect(() => parseDrawing('<not svg')).toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
describe('AnatomyCanvas', () => {
|
||||
it('renders the view and delegates clicks on the generated markers to onZoneClick with the view id', async () => {
|
||||
const onZoneClick = vi.fn()
|
||||
const { container } = render(
|
||||
<AnatomyCanvas
|
||||
index={sheet}
|
||||
viewId="main"
|
||||
states={new Map()}
|
||||
onZoneClick={onZoneClick}
|
||||
/>,
|
||||
)
|
||||
await waitFor(() =>
|
||||
expect(
|
||||
container.querySelector('[data-anno-id="head-trigger"]'),
|
||||
).not.toBeNull(),
|
||||
)
|
||||
const hip = container.querySelector('circle[data-anno-id="hip"]')!
|
||||
hip.dispatchEvent(new Event('click', { bubbles: true }))
|
||||
expect(onZoneClick).toHaveBeenCalledWith('hip', 'main')
|
||||
container
|
||||
.querySelector('path')!
|
||||
.dispatchEvent(new Event('click', { bubbles: true }))
|
||||
expect(onZoneClick).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('restyles in place when the states change', async () => {
|
||||
const { container, rerender } = render(
|
||||
<AnatomyCanvas
|
||||
index={sheet}
|
||||
viewId="main"
|
||||
states={new Map()}
|
||||
onZoneClick={() => undefined}
|
||||
/>,
|
||||
)
|
||||
await waitFor(() =>
|
||||
expect(container.querySelector('[data-anno-id="hip"]')).not.toBeNull(),
|
||||
)
|
||||
rerender(
|
||||
<AnatomyCanvas
|
||||
index={sheet}
|
||||
viewId="main"
|
||||
states={new Map([['hip', 'current']])}
|
||||
onZoneClick={() => undefined}
|
||||
/>,
|
||||
)
|
||||
expect(
|
||||
container.querySelectorAll('[data-anno-id="hip"][data-state="current"]'),
|
||||
).toHaveLength(2)
|
||||
rerender(
|
||||
<AnatomyCanvas
|
||||
index={sheet}
|
||||
viewId="main"
|
||||
states={new Map()}
|
||||
onZoneClick={() => undefined}
|
||||
/>,
|
||||
)
|
||||
expect(container.querySelectorAll('[data-state="current"]')).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user