feat(app): editor Général, Références and Vertèbres tabs, delete with dependents
This commit is contained in:
1 parent
6aba9711f0
commit
d163b2ce9a
24 files changed
+2239
-12
No files matched your search
@@ -0,0 +1,243 @@
|
||||
/** Lot F: Général (texts, attribution, annotation categories) and the delete dialog. */
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { cleanup, render, screen, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types'
|
||||
import DeleteDialog from '@/components/editor/DeleteDialog'
|
||||
import GeneralTab from '@/components/editor/tabs/GeneralTab'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { edit, insert } from '../helpers/editorFixtures'
|
||||
import {
|
||||
baseText,
|
||||
openReady,
|
||||
resetStore,
|
||||
store,
|
||||
} from '../helpers/editorHarness'
|
||||
|
||||
vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => ({}) }))
|
||||
vi.mock('@faunosteo/supabase', async (orig) => ({
|
||||
...(await orig<typeof import('@faunosteo/supabase')>()),
|
||||
getSheetVersion: vi.fn(),
|
||||
downloadSheetDrawing: vi.fn(),
|
||||
saveSheetDraft: vi.fn(),
|
||||
getOfficialSheetAdmin: vi.fn(),
|
||||
submitSheetReview: vi.fn(),
|
||||
}))
|
||||
|
||||
const props = { readOnly: false, onSelectTab: vi.fn(), reloadSheet: vi.fn() }
|
||||
const doc = (): AnatomicalSheetDocument =>
|
||||
JSON.parse(store.getState().documentText)
|
||||
|
||||
beforeEach(() => useLocaleStore.setState({ locale: 'fr' }))
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
resetStore()
|
||||
})
|
||||
|
||||
describe('Général: texts and attribution', () => {
|
||||
it('commits a renamed sheet on blur as one update, nothing per keystroke', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
const field = screen.getByLabelText('Nom (FR)')
|
||||
await user.clear(field)
|
||||
await user.type(field, 'Nouvelle fiche')
|
||||
expect(store.getState().undo).toHaveLength(0)
|
||||
await user.tab()
|
||||
expect(doc().name.fr).toBe('Nouvelle fiche')
|
||||
expect(store.getState().undo).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('refuses a blank name without reaching the core', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
await user.clear(screen.getByLabelText('Nom (EN)'))
|
||||
await user.tab()
|
||||
expect(screen.getByRole('alert')).toHaveTextContent(
|
||||
'Ce champ est obligatoire.',
|
||||
)
|
||||
expect(store.getState().unsaved).toBe(false)
|
||||
})
|
||||
|
||||
it('a blank optional text is omitted, never an empty string', async () => {
|
||||
const text = edit(baseText(), {
|
||||
op: 'update',
|
||||
key: { kind: 'sheet' },
|
||||
element: {
|
||||
kind: 'sheet',
|
||||
value: { ...texts(), description: { fr: 'Texte', en: 'Text' } },
|
||||
},
|
||||
})
|
||||
await openReady({ text })
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
await user.clear(screen.getByLabelText('Description (FR)'))
|
||||
await user.tab()
|
||||
expect(doc().description).toEqual({ en: 'Text' })
|
||||
})
|
||||
|
||||
it('adds and removes an author; a blank licence is null', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
await user.type(screen.getByLabelText('Nouvel auteur'), 'A. Martin')
|
||||
await user.click(screen.getByRole('button', { name: "Ajouter l'auteur" }))
|
||||
expect(doc().attribution.authors).toEqual(['A. Martin'])
|
||||
await user.type(screen.getByLabelText('Licence'), 'CC-BY')
|
||||
await user.tab()
|
||||
expect(doc().attribution.license).toBe('CC-BY')
|
||||
await user.clear(screen.getByLabelText('Licence'))
|
||||
await user.tab()
|
||||
expect(doc().attribution.license).toBeNull()
|
||||
await user.click(screen.getByRole('button', { name: 'Retirer A. Martin' }))
|
||||
expect(doc().attribution.authors).toEqual([])
|
||||
})
|
||||
|
||||
it('read-only: values as text, no control, no button', async () => {
|
||||
await openReady()
|
||||
render(<GeneralTab {...props} readOnly />)
|
||||
expect(screen.queryByRole('textbox')).toBeNull()
|
||||
expect(screen.queryByRole('button')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Général: annotation categories', () => {
|
||||
it('renames, recolours and adds a category', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
const row = screen.getByLabelText('Nom de la catégorie (FR)', {
|
||||
selector: '#category-cat-a-name-fr',
|
||||
})
|
||||
await user.clear(row)
|
||||
await user.type(row, 'Os')
|
||||
await user.tab()
|
||||
expect(doc().categories[0].name.fr).toBe('Os')
|
||||
await user.type(
|
||||
screen.getByLabelText('Nom de la catégorie (FR)', {
|
||||
selector: '#category-new-fr',
|
||||
}),
|
||||
'Muscle',
|
||||
)
|
||||
await user.type(
|
||||
screen.getByLabelText('Nom de la catégorie (EN)', {
|
||||
selector: '#category-new-en',
|
||||
}),
|
||||
'Muscle',
|
||||
)
|
||||
await user.click(
|
||||
screen.getByRole('button', { name: 'Ajouter la catégorie' }),
|
||||
)
|
||||
expect(doc().categories).toHaveLength(2)
|
||||
expect(doc().categories[1]).toMatchObject({
|
||||
name: { fr: 'Muscle' },
|
||||
color: null,
|
||||
})
|
||||
expect(doc().categories[1].id).toMatch(/^cat-[a-z2-7]{10}$/)
|
||||
})
|
||||
|
||||
it('a new category needs both names', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
await user.click(
|
||||
screen.getByRole('button', { name: 'Ajouter la catégorie' }),
|
||||
)
|
||||
expect(screen.getByRole('alert')).toHaveTextContent('Renseignez le nom')
|
||||
expect(doc().categories).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('a used category cannot be deleted: the dialog lists the dependents', async () => {
|
||||
await openReady()
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
await user.click(screen.getByRole('button', { name: 'Supprimer' }))
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
expect(within(dialog).getByText(/utilise cette catégorie/)).toBeTruthy()
|
||||
expect(
|
||||
within(dialog).queryByRole('button', { name: 'Supprimer' }),
|
||||
).toBeNull()
|
||||
await user.click(
|
||||
within(dialog).getAllByRole('button', { name: 'Fermer' }).at(-1)!,
|
||||
)
|
||||
expect(doc().categories).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('an unused category is deleted after the confirmation', async () => {
|
||||
const text = insert(baseText(), {
|
||||
kind: 'category',
|
||||
value: { id: 'cat-b', name: { fr: 'Libre', en: 'Free' }, color: null },
|
||||
})
|
||||
await openReady({ text })
|
||||
const user = userEvent.setup()
|
||||
render(<GeneralTab {...props} />)
|
||||
const buttons = screen.getAllByRole('button', { name: 'Supprimer' })
|
||||
await user.click(buttons[1])
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
expect(
|
||||
within(dialog).getByText('Supprimer « Libre » du brouillon ?'),
|
||||
).toBeTruthy()
|
||||
await user.click(within(dialog).getByRole('button', { name: 'Supprimer' }))
|
||||
expect(doc().categories.map((c) => c.id)).toEqual(['cat-a'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('DeleteDialog', () => {
|
||||
it('lists the descendants that go with the element and removes them', async () => {
|
||||
const withZone = insert(baseText(), {
|
||||
kind: 'zone',
|
||||
value: {
|
||||
id: 'zone-a',
|
||||
name: { fr: 'Zone A', en: 'Zone A' },
|
||||
categories: [],
|
||||
},
|
||||
})
|
||||
const text = edit(withZone, {
|
||||
op: 'insert',
|
||||
parent: { kind: 'zone', id: 'zone-a' },
|
||||
element: {
|
||||
kind: 'finding_category',
|
||||
value: {
|
||||
id: 'fcat-a',
|
||||
kind: 'dysfunction',
|
||||
name: { fr: 'Boiterie', en: 'Lameness' },
|
||||
description: {},
|
||||
reference_ids: [],
|
||||
review_status: 'to_confirm',
|
||||
children: [],
|
||||
},
|
||||
},
|
||||
})
|
||||
await openReady({ text })
|
||||
const user = userEvent.setup()
|
||||
const onClose = vi.fn()
|
||||
render(
|
||||
<DeleteDialog
|
||||
target={{ key: { kind: 'zone', id: 'zone-a' }, label: 'Zone A' }}
|
||||
onClose={onClose}
|
||||
/>,
|
||||
)
|
||||
const dialog = await screen.findByRole('dialog')
|
||||
expect(within(dialog).getByText(/Boiterie/)).toBeTruthy()
|
||||
await user.click(within(dialog).getByRole('button', { name: 'Supprimer' }))
|
||||
expect(onClose).toHaveBeenCalled()
|
||||
expect(doc().finding_zones).toEqual([])
|
||||
})
|
||||
|
||||
it('a closed dialog asks nothing and renders nothing', async () => {
|
||||
await openReady()
|
||||
render(<DeleteDialog target={null} onClose={vi.fn()} />)
|
||||
expect(screen.queryByRole('dialog')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
function texts() {
|
||||
const d: AnatomicalSheetDocument = JSON.parse(baseText())
|
||||
return {
|
||||
name: d.name,
|
||||
description: d.description,
|
||||
scope_disclaimer: d.scope_disclaimer,
|
||||
attribution: d.attribution,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user