/** Lot E: the shared field components: commit once per change, text when read-only. */
import { describe, expect, it, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import ActionButton from '@/components/editor/fields/ActionButton'
import SelectField from '@/components/editor/fields/SelectField'
import TextField from '@/components/editor/fields/TextField'
const OPTIONS = [
{ value: 'a', label: 'Alpha' },
{ value: 'b', label: 'Beta' },
]
describe('TextField', () => {
it('commits on blur with a changed value, never per keystroke', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
render()
await user.type(screen.getByLabelText('Nom'), 's')
expect(onCommit).not.toHaveBeenCalled()
await user.tab()
expect(onCommit).toHaveBeenCalledOnce()
expect(onCommit).toHaveBeenCalledWith('Chevals')
})
it('Enter commits a single line; an unchanged value commits nothing', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
render()
const input = screen.getByLabelText('Nom')
await user.click(input)
await user.keyboard('{Enter}')
await user.tab()
expect(onCommit).not.toHaveBeenCalled()
await user.type(input, 'x{Enter}')
expect(onCommit).toHaveBeenCalledWith('Chevalx')
})
it('a value still pending when the field unmounts is committed', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
const view = render(
,
)
await user.type(screen.getByLabelText('Nom'), 'z')
view.unmount()
expect(onCommit).toHaveBeenCalledWith('Chevalz')
})
it('trim commits a padded name without its edge spaces', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
render(
,
)
await user.type(screen.getByLabelText('Nom'), ' {Enter}')
expect(onCommit).not.toHaveBeenCalled()
await user.type(screen.getByLabelText('Nom'), 'x ')
await user.tab()
expect(onCommit).toHaveBeenCalledWith('Chevalx')
})
it('trim: a blank value commits as empty; unmount trims too', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
const view = render(
,
)
await user.type(screen.getByLabelText('Nom'), ' ')
await user.tab()
expect(onCommit).not.toHaveBeenCalled()
await user.type(screen.getByLabelText('Nom'), ' a ')
view.unmount()
expect(onCommit).toHaveBeenCalledWith('a')
})
it('without trim a padded description commits unchanged', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
render()
await user.type(screen.getByLabelText('Texte'), ' a ')
await user.tab()
expect(onCommit).toHaveBeenCalledWith(' a ')
})
it('a new committed value replaces what was typed; the refusal shows next to the control', () => {
const view = render(
,
)
expect(screen.getByRole('alert')).toHaveTextContent('Refusé')
expect(screen.getByLabelText('Nom')).toHaveAttribute('aria-invalid', 'true')
view.rerender()
expect(screen.getByLabelText('Nom')).toHaveValue('b')
})
it('read-only: the value as text, no control; empty reads as a dash', () => {
const view = render(
,
)
expect(screen.queryByRole('textbox')).toBeNull()
expect(screen.getByText('Cheval')).toBeInTheDocument()
view.rerender(
,
)
expect(screen.getByText('—')).toBeInTheDocument()
})
})
describe('SelectField and ActionButton', () => {
it('a choice commits at once; read-only shows the option label', async () => {
const user = userEvent.setup()
const onCommit = vi.fn()
const view = render(
,
)
await user.selectOptions(screen.getByLabelText('Type'), 'b')
expect(onCommit).toHaveBeenCalledWith('b')
view.rerender(
,
)
expect(screen.queryByRole('combobox')).toBeNull()
expect(screen.getByText('Beta')).toBeInTheDocument()
})
it('the action is absent while read-only', () => {
const view = render(Supprimer)
expect(
screen.getByRole('button', { name: 'Supprimer' }),
).toBeInTheDocument()
view.rerender(Supprimer)
expect(screen.queryByRole('button')).toBeNull()
})
})