149 lines
5.5 KiB
TypeScript
149 lines
5.5 KiB
TypeScript
import { beforeAll, describe, expect, it, vi, afterEach } from 'vitest'
|
|
import { cleanup, render, screen, waitFor } from '@testing-library/react'
|
|
import AnatomyCanvas from '@/components/canvas/AnatomyCanvas'
|
|
import { buildViewSvg } from '@/anatomy/drawing'
|
|
import type { SheetIndex } from '@/services/sheetIndex'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
import { bundledSheet, loadTestSheet, sheetFromBundle } from '../helpers/sheet'
|
|
|
|
let sheet: SheetIndex
|
|
beforeAll(async () => {
|
|
sheet = await loadTestSheet()
|
|
})
|
|
afterEach(cleanup)
|
|
|
|
/** The bundled sheet with its drawing replaced by `svg` (a hostile asset). */
|
|
async function sheetDrawing(svg: string): Promise<SheetIndex> {
|
|
const bundle = await bundledSheet()
|
|
const assets = Object.fromEntries(Object.keys(bundle.assets).map((id) => [id, svg]))
|
|
return sheetFromBundle({ ...bundle, assets })
|
|
}
|
|
|
|
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')!
|
|
const head = buildViewSvg(sheet, 'head')!
|
|
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('composes the view once per sheet (a constant text) and carries no state', () => {
|
|
expect(sheet.canvasSvg('main')).toEqual(sheet.canvasSvg('main'))
|
|
expect(buildViewSvg(sheet, 'main')!.querySelector('[data-state]')).toBeNull()
|
|
})
|
|
|
|
it('refuses a drawing the core would have to alter: nothing of it reaches the page (LRN-025)', async () => {
|
|
const hostile = await sheetDrawing(
|
|
'<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>',
|
|
)
|
|
expect(hostile.canvasSvg('main')).toMatchObject({ ok: false, kind: 'svg_rejected' })
|
|
expect(buildViewSvg(hostile, 'main')).toBeNull()
|
|
const malformed = await sheetDrawing('<not svg')
|
|
expect(malformed.canvasSvg('main')).toMatchObject({
|
|
ok: false,
|
|
kind: 'svg_rejected',
|
|
detail: 'malformed',
|
|
})
|
|
})
|
|
})
|
|
|
|
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('applies the states right after the build, on both elements of a point', async () => {
|
|
const { container } = render(
|
|
<AnatomyCanvas
|
|
index={sheet}
|
|
viewId="main"
|
|
states={new Map([['stifle', 'current']])}
|
|
onZoneClick={() => undefined}
|
|
/>,
|
|
)
|
|
await waitFor(() =>
|
|
expect(container.querySelector('[data-anno-id="stifle"]')).not.toBeNull(),
|
|
)
|
|
const marked = Array.from(
|
|
container.querySelectorAll('[data-state="current"]'),
|
|
).map((el) => el.getAttribute('data-anno-id'))
|
|
expect(marked).toEqual(['stifle', 'stifle'])
|
|
})
|
|
|
|
it('shows the degraded notice for a drawing it cannot draw, and nothing of the drawing', async () => {
|
|
useLocaleStore.setState({ locale: 'fr' })
|
|
const hostile = await sheetDrawing(
|
|
'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
|
|
)
|
|
const { container } = render(
|
|
<AnatomyCanvas index={hostile} viewId="main" states={new Map()} onZoneClick={() => undefined} />,
|
|
)
|
|
await screen.findByText(fr['sheet.core_unavailable']!)
|
|
expect(screen.getByRole('button', { name: fr['sheet.core_retry'] })).toBeInTheDocument()
|
|
expect(container.querySelector('script, [data-layer], [data-anno-id]')).toBeNull()
|
|
})
|
|
|
|
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)
|
|
})
|
|
})
|