我如何测试材料用户界面的 useAutocomplete 挂钩 onChange 事件

pru*_*hvi 5 reactjs material-ui react-testing-library

这是我获取 useAutocomplete 钩子用法的代码。我想测试 onChange 的代码覆盖率。有没有办法通过使用反应测试库来做到这一点。

 const {
    getRootProps,
    getInputProps,
    getListboxProps,
    getOptionProps,
    groupedOptions,
    getClearProps
} = useAutocomplete({
    id: 'use-autocomplete-demo',
    options: routeInformation ? routeInformation : [],
    getOptionLabel: (option) => option.routeName,
    clearOnEscape: true,
    disableClearable: false,
    onChange: (event: React.ChangeEvent<{}>, value: any) => {
        if (setPreviousRoute && setSelectedRoute) {
            setPreviousRoute({...selectedRoute} as RouteInformation);
            if ( value ) {
                setSelectedRoute({...value} as RouteInformation);
            } else {
                setSelectedRoute(undefined);
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我的渲染元素看起来像这样,当我从 react-testing-library 进行测试时,groupedOptions 没有任何数据。

<div>
                        <div {...getRootProps()} >
                            <InputBase
                                data-testid="SearchInput"
                                {...getInputProps()}
                                placeholder="Search Route Name"
                                classes={{
                                    root: classes.inputRoot,
                                    input: classes.inputInput,
                                }}
                                endAdornment={
                                    <InputAdornment position="end">
                                        <IconButton  {...getClearProps()}>
                                            <CancelRoundedIcon fontSize="small"/>
                                        </IconButton>
                                    </InputAdornment>
                                }
                                inputProps={{ 'aria-label': 'search' }}
                            />
                        </div>
                        <ul data-testid="SuggestionsList" className={classes.suggestionContainer} {...getListboxProps()}>
                            {groupedOptions.map((option, index) => (
                                <li className={classes.suggestionItem} {...getOptionProps({ option, index })}>
                                    {option.routeName} | ID: {option.routeId}
                                </li>
                            ))}
                        </ul>
                    </div>
Run Code Online (Sandbox Code Playgroud)