Files
faunosteo/apps/app/tests/unit/components.AnatomyCanvas.test.tsx
T

111 lines
4.0 KiB
TypeScript

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'
let sheet: SheetIndex
beforeAll(async () => {
sheet = await loadTestSheet()
})
afterEach(cleanup)
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('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>',
)
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)
})
})