Skip to content

Commit

Permalink
Add test coverage for missing onInputChange reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongarciah committed Jul 4, 2024
1 parent d6199f9 commit 7bbe1b3
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions packages/mui-material/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2571,6 +2571,89 @@ describe('<Autocomplete />', () => {
expect(handleInputChange.args[2][1]).to.equal(options[1].name);
expect(handleInputChange.args[2][2]).to.equal('reset');
});

it('provides a reason on clear', () => {
const handleInputChange = spy();
const options = [{ name: 'foo' }];

render(
<Autocomplete
onInputChange={handleInputChange}
options={options}
getOptionLabel={(option) => option.name}
renderInput={(params) => <TextField {...params} autoFocus />}
defaultValue={options[0]}
/>,
);
fireEvent.click(screen.getByLabelText('Clear'));

expect(handleInputChange.lastCall.args[1]).to.equal('');
expect(handleInputChange.lastCall.args[2]).to.equal('clear');
});

it('provides a reason on blur', () => {
const handleInputChange = spy();
const options = [{ name: 'foo' }];

render(
<Autocomplete
onInputChange={handleInputChange}
options={options}
getOptionLabel={(option) => option.name}
renderInput={(params) => <TextField {...params} autoFocus />}
clearOnBlur
/>,
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: options[0].name } });
fireEvent.blur(textbox);

expect(handleInputChange.lastCall.args[1]).to.equal('');
expect(handleInputChange.lastCall.args[2]).to.equal('blur');
});

it('provides a reason on select option', () => {
const handleInputChange = spy();
const options = [{ name: 'foo' }];

render(
<Autocomplete
onInputChange={handleInputChange}
options={options}
getOptionLabel={(option) => option.name}
renderInput={(params) => <TextField {...params} autoFocus />}
/>,
);
fireEvent.click(screen.getByLabelText('Open'));
fireEvent.click(screen.getByRole('option', { name: options[0].name }));

expect(handleInputChange.lastCall.args[1]).to.equal(options[0].name);
expect(handleInputChange.lastCall.args[2]).to.equal('selectOption');
});

it('provides a reason on remove option', () => {
const handleInputChange = spy();
const options = [{ name: 'foo' }];

render(
<Autocomplete
onInputChange={handleInputChange}
options={options}
getOptionLabel={(option) => option.name}
renderInput={(params) => <TextField {...params} autoFocus />}
defaultValue={options}
multiple
/>,
);
const textbox = screen.getByRole('combobox');
fireEvent.change(textbox, { target: { value: options[0].name } });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'ArrowDown' });
fireEvent.keyDown(textbox, { key: 'Enter' });

expect(handleInputChange.lastCall.args[1]).to.equal('');
expect(handleInputChange.lastCall.args[2]).to.equal('removeOption');
});
});

describe('prop: blurOnSelect', () => {
Expand Down

0 comments on commit 7bbe1b3

Please sign in to comment.