-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Autocomplete] onChange does not fire in tests (react-testing-library) #18251
Comments
See #18113 (comment), onChange only fire when an option is selected. |
This comment has been minimized.
This comment has been minimized.
@maximeantoine1997 We don't provide support here, please ask on StackOverflow. Alternatively, you can check our tests. |
@SladeRun14 We ran into the same issue recently and ended up doing the following, create setupTests.js in your root project and add: document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
commonAncestorContainer: {
nodeName: "BODY",
ownerDocument: document,
},
}) Update: since Jest v26.0.0 (jestjs/jest#9606), the workaround is no longer required. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
You can refer to the tests of the component to learn the idiomatic way to test it: https://github.com/mui-org/material-ui/blob/32d647cbf72fb3661638d88c428ba37b804b6f15/packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js. |
Thanks @oliviertassinari To all the googlers who get here and don't want to dig around, I was able to execute tests by following examples above and doing this: const autocomplete = getByRole('textbox')
// click into the component
autocomplete.focus()
// type "a"
fireEvent.change(document.activeElement, { target: { value: 'a' } })
// arrow down to first option
fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' })
// select element
fireEvent.keyDown(document.activeElement, { key: 'Enter' })
expect(autocomplete.value).toEqual('Arkansas')
expect(someChangeHandler).toHaveBeenCalledTimes(1) |
Update, since Jest v26.0.0 (jestjs/jest#9606), the workaround in #18251 (comment) is no longer required. |
I'm still not able to select Trying to do steps in #18251 (comment) but can't see any result from I tired to add This is my autocomplete component import React from 'react';
import styled, {createGlobalStyle} from 'styled-components';
import Autocomplete from '@material-ui/lab/Autocomplete';
import {FormControl, TextField} from './Forms';
const StyledAutocomplete = styled(Autocomplete)`
& .MuiInputBase-root {
height: 4.1rem;
}
& label: {
font-size: 2rem;
}
`;
const StyleAutoCompleteOptions = createGlobalStyle`
.MuiAutocomplete-popper div {
font-size: 1.6rem;
}
`;
export interface AutocompleteOptionData {
value: string;
label: string;
}
interface AutocompleteFieldProps {
currentValue: AutocompleteOptionData | null;
error?: string | React.ReactElement;
id: string;
label: string;
name: string;
options: AutocompleteOptionData[];
onChange?: (value: AutocompleteOptionData) => void;
className?: string;
}
const AutocompleteField: React.FC<AutocompleteFieldProps> = ({
id,
label,
name,
options,
currentValue,
error,
onChange,
className,
}) => {
console.log('currentValue', currentValue);
return (
<FormControl htmlFor={id} error={error}>
<StyleAutoCompleteOptions />
<StyledAutocomplete
id={id}
options={options}
data-testid="autocomplete"
onChange={(_event: React.ChangeEvent<{}>, value) =>
onChange &&
onChange(
(value as AutocompleteOptionData | null) || {
label: '',
value: '',
}
)
}
value={currentValue}
getOptionLabel={data => (data as AutocompleteOptionData).label}
getOptionSelected={option =>
(option as AutocompleteOptionData).label === currentValue?.label
}
className={className}
renderInput={params => (
<TextField
{...params}
error={!!error}
name={name}
label={label}
variant="standard"
data-testid="select-input-field"
/>
)}
/>
</FormControl>
);
};
export default AutocompleteField; Any idea how to test it ? |
Have you found a way to test this yet? |
MUI now changes the text field to a "combobox" role so you have to do |
While Autocomplete's
onChange
event is called correctly during normal usage, when testing with the react-testing-library it is not. The following test fails:Current Behavior 😯
expect(handleChange).toHaveBeenCalledTimes(1)
failsExpected Behavior 🤔
expect(handleChange).toHaveBeenCalledTimes(1)
passesSteps to Reproduce 🕹
Steps:
Context 🔦
Your Environment 🌎
The text was updated successfully, but these errors were encountered: