`
expect(firstTextField(panel)?.id).toBe('target')
})
it('FE-SHARED-DIALOGFOCUS-002: a dialog with no field has none', () => {
const panel = document.createElement('div')
panel.innerHTML = ''
expect(firstTextField(panel)).toBeNull()
})
})
describe('DialogShell focus (#1302)', () => {
it('FE-SHARED-DIALOGFOCUS-003: on a desktop the first text field is ready to type into', () => {
restore = asDesktop()
render()
expect(screen.getByLabelText('Name')).toHaveFocus()
})
it('FE-SHARED-DIALOGFOCUS-004: on a touch screen the panel takes the focus, so no keyboard pops up', () => {
render()
expect(screen.getByRole('dialog')).toHaveFocus()
})
it('FE-SHARED-DIALOGFOCUS-005: a field that took the focus itself keeps it', () => {
restore = asDesktop()
render()
expect(screen.getByLabelText('Search')).toHaveFocus()
})
it('FE-SHARED-DIALOGFOCUS-006: Tab wraps from the last control to the first and back', () => {
render()
const one = screen.getByRole('button', { name: 'One' })
const two = screen.getByRole('button', { name: 'Two' })
two.focus()
fireEvent.keyDown(two, { key: 'Tab' })
expect(one).toHaveFocus()
fireEvent.keyDown(one, { key: 'Tab', shiftKey: true })
expect(two).toHaveFocus()
// From the panel itself, Shift+Tab goes to the last control rather than the page.
const panel = screen.getByRole('dialog')
panel.focus()
fireEvent.keyDown(panel, { key: 'Tab', shiftKey: true })
expect(two).toHaveFocus()
})
it('FE-SHARED-DIALOGFOCUS-007: Tab in the middle is left to the browser', () => {
render()
const two = screen.getByRole('button', { name: 'Two' })
two.focus()
const event = new KeyboardEvent('keydown', { key: 'Tab', bubbles: true, cancelable: true })
two.dispatchEvent(event)
expect(event.defaultPrevented).toBe(false)
})
})
describe('Modal focus (#1302)', () => {
function Harness() {
const [open, setOpen] = useState(false)
return (
<>
setOpen(false)} title="Edit">
>
)
}
it('FE-SHARED-DIALOGFOCUS-008: the focus moves in on open and back to the opener on close', () => {
restore = asDesktop()
render()
const opener = screen.getByRole('button', { name: 'Open' })
opener.focus()
fireEvent.click(opener)
expect(screen.getByLabelText('Title')).toHaveFocus()
fireEvent.click(screen.getByRole('button', { name: 'Close' }))
expect(screen.queryByLabelText('Title')).toBeNull()
expect(opener).toHaveFocus()
})
it('FE-SHARED-DIALOGFOCUS-009: Tab stays inside the modal', () => {
render()
fireEvent.click(screen.getByRole('button', { name: 'Open' }))
const save = screen.getByRole('button', { name: 'Save' })
save.focus()
fireEvent.keyDown(save, { key: 'Tab' })
expect(screen.getByRole('button', { name: 'Close' })).toHaveFocus()
})
})