Skip to content

Commit

Permalink
add regression unit test for useAutocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 committed Feb 10, 2023
1 parent d78cb1a commit 3dd9626
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/mui-base/src/AutocompleteUnstyled/useAutocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,48 @@ describe('useAutocomplete', () => {
fireEvent.click(button);
}).not.to.throw();
});

it('should call getOptionLabel only with existing options on filtered options change', () => {
function Test(props) {
const { options, getOptionLabel, filtered } = props;
const {
groupedOptions,
getRootProps,
getInputLabelProps,
getInputProps,
getListboxProps,
getOptionProps,
} = useAutocomplete({
options,
getOptionLabel,
filterOptions: (list) => list.filter((option) => filtered.includes(option)),
filterSelectedOptions: true,
autoHighlight: true,
open: true,
});

return (
<div>
<div {...getRootProps()}>
<label {...getInputLabelProps()}>useAutocomplete</label>
<input {...getInputProps()} />
</div>
<ul {...getListboxProps()}>
{groupedOptions.map((option, index) => {
return <li {...getOptionProps({ option, index })}>{option}</li>;
})}
</ul>
</div>
);
}

const getOptionLabel = spy((x) => x);
const options = [ 'foo', 'bar' ];

const { setProps } = render(<Test {...{ getOptionLabel, options }} filtered={options} />);
setProps({ filtered: options.slice(1) }); // filter out some options

const calledOptions = getOptionLabel.getCalls().map(({ args: [ option ]}) => option);
expect(options).to.include.members(calledOptions);
});
});

0 comments on commit 3dd9626

Please sign in to comment.