159 lines
5.3 KiB
TypeScript
159 lines
5.3 KiB
TypeScript
/** 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(<TextField id="f" label="Nom" value="Cheval" onCommit={onCommit} />)
|
|
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(<TextField id="f" label="Nom" value="Cheval" onCommit={onCommit} />)
|
|
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(
|
|
<TextField id="f" label="Nom" value="Cheval" onCommit={onCommit} />,
|
|
)
|
|
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(
|
|
<TextField id="f" label="Nom" value="Cheval" onCommit={onCommit} trim />,
|
|
)
|
|
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(
|
|
<TextField id="f" label="Nom" value="" onCommit={onCommit} trim />,
|
|
)
|
|
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(<TextField id="f" label="Texte" value="" onCommit={onCommit} />)
|
|
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(
|
|
<TextField
|
|
id="f"
|
|
label="Nom"
|
|
value="a"
|
|
onCommit={vi.fn()}
|
|
error="Refusé"
|
|
/>,
|
|
)
|
|
expect(screen.getByRole('alert')).toHaveTextContent('Refusé')
|
|
expect(screen.getByLabelText('Nom')).toHaveAttribute('aria-invalid', 'true')
|
|
view.rerender(<TextField id="f" label="Nom" value="b" onCommit={vi.fn()} />)
|
|
expect(screen.getByLabelText('Nom')).toHaveValue('b')
|
|
})
|
|
|
|
it('read-only: the value as text, no control; empty reads as a dash', () => {
|
|
const view = render(
|
|
<TextField
|
|
id="f"
|
|
label="Nom"
|
|
value="Cheval"
|
|
onCommit={vi.fn()}
|
|
readOnly
|
|
/>,
|
|
)
|
|
expect(screen.queryByRole('textbox')).toBeNull()
|
|
expect(screen.getByText('Cheval')).toBeInTheDocument()
|
|
view.rerender(
|
|
<TextField id="f" label="Nom" value="" onCommit={vi.fn()} readOnly />,
|
|
)
|
|
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(
|
|
<SelectField
|
|
id="s"
|
|
label="Type"
|
|
value="a"
|
|
options={OPTIONS}
|
|
onCommit={onCommit}
|
|
/>,
|
|
)
|
|
await user.selectOptions(screen.getByLabelText('Type'), 'b')
|
|
expect(onCommit).toHaveBeenCalledWith('b')
|
|
view.rerender(
|
|
<SelectField
|
|
id="s"
|
|
label="Type"
|
|
value="b"
|
|
options={OPTIONS}
|
|
onCommit={onCommit}
|
|
readOnly
|
|
/>,
|
|
)
|
|
expect(screen.queryByRole('combobox')).toBeNull()
|
|
expect(screen.getByText('Beta')).toBeInTheDocument()
|
|
})
|
|
|
|
it('the action is absent while read-only', () => {
|
|
const view = render(<ActionButton readOnly={false}>Supprimer</ActionButton>)
|
|
expect(
|
|
screen.getByRole('button', { name: 'Supprimer' }),
|
|
).toBeInTheDocument()
|
|
view.rerender(<ActionButton readOnly>Supprimer</ActionButton>)
|
|
expect(screen.queryByRole('button')).toBeNull()
|
|
})
|
|
})
|