I have a component that uses FormControl. I'm trying to make sure it's accessible, and leaning on things like role
and aria-label
in the tests to help with that.
Here is the rough outline of the component:
const MySearch = ({searchText, setSearch}) => {
const inputRef = useRef();
return (<div class="some classes">
<FormControl role="search" aria-label="My Search Box" fullWidth>
<InputLabel>
<IconSearch />
Find Foo
</InputLabel>
<Input
inputRef={inputRef}
onChange={event => setSearchText(event.target.value)}
value={searchText}
endAdornment={searchText &&
<InputAdornment position="end">
<IconButton
arial-label="Clear Search"
onClick={() => {
setSearchText('');
if (inputRef.current) inputRef.current.focus();
}}
>
<IconClear />
</IconButton>
</InputAdornment>
}
/>
</FormControl>
</div>
)
When I use this test code to type 'i' into the input box, like so:
await userEvent.type(screen.getByRole('search'), 'i');
I do not get any event firing, and I have been informed by a colleague that directly selecting the input (by putting a role, label of testid) will work but none of use are sure if that is the correct place to select for accessibility purposes.
The w3 spec for role="search"
says it's for the region (and I note that role="searchbox"
exists) but I'm not clear what this means in terms of expected behaviour when navigating here, or it's accessibility.
Neither the material-ui FormControl api docs or the demos mention roles.
Putting a click or change listener on the FormControl itself does fire on the webpage, but doesn't fire at all in the test - maybe my use of await userEvent.type(screen.getByRole('search'), 'i');
is wrong.
Ultimately, while I will direct my test to point at the search input for now, is it not the right behaviour that clicking the FormControl should mean the event is captured by the FormControl and then 'passed' down to the input, before bubbling back up (see What is event bubbling and capturing?)?
What is the correct behaviour of the FormControl so that it is accessible? I found Pass click event to nested component - React | Material-UI which seems related, but doesn't cover accessibility or FormControl which I was expecting to behave differently