Skip to content

Commit

Permalink
update the test, fix discovered issue
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Mar 1, 2020
1 parent 93c3e04 commit a33ae0f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
19 changes: 5 additions & 14 deletions packages/material-ui-lab/src/Autocomplete/Autocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,27 @@ describe('<Autocomplete />', () => {
checkHighlightIs('one');
});

it.only('should set the focus on selected item when dropdown is expanded', () => {
const options = ['one', 'two', 'three'];
it('should set the focus on selected item when dropdown is expanded', () => {
const { getByRole, setProps } = render(
<Autocomplete
{...defaultProps}
value="one"
options={options}
options={['one', 'two', 'three']}
renderInput={params => <TextField autoFocus {...params} />}
size="small"
/>,
);

function checkHighlightIs(expected) {
expect(getByRole('listbox').querySelector('li[data-focus]')).to.have.text(expected);
}

fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
checkHighlightIs('one');

// setProps({ value: 'two' })
// checkHighlightIs('two');

// fireEvent.change(document.activeElement, { target: { value: 'two' } });
// fireEvent.keyDown(document.activeElement, { key: 'Enter' });
// fireEvent.keyDown(document.activeElement, { key: 'ArrowDown' });
// checkHighlightIs('two');
setProps({ value: 'two' });
checkHighlightIs('two');
});
});

describe.only('prop: filterSelectedOptions', () => {
describe('prop: filterSelectedOptions', () => {
it('when the last item is selected, highlights the new last item', () => {
const { getByRole } = render(
<Autocomplete
Expand Down
24 changes: 15 additions & 9 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default function useAutocomplete(props) {
const defaultHighlighted = autoHighlight ? 0 : -1;
const highlightedIndexRef = React.useRef(defaultHighlighted);

function setHighlightedIndex(index, mouse = false) {
const setHighlightedIndex = useEventCallback((index, mouse = false) => {
highlightedIndexRef.current = index;
// does the index exist?
if (index === -1) {
Expand Down Expand Up @@ -190,7 +190,7 @@ export default function useAutocomplete(props) {
listboxNode.scrollTop = element.offsetTop - element.offsetHeight * (groupBy ? 1.3 : 0);
}
}
}
});

const [value, setValue] = useControlled({
controlled: valueProp,
Expand Down Expand Up @@ -323,7 +323,7 @@ export default function useAutocomplete(props) {
}
}

const changeHighlightedIndex = (diff, direction) => {
const changeHighlightedIndex = useEventCallback((diff, direction) => {
if (!popupOpen) {
return;
}
Expand Down Expand Up @@ -390,11 +390,7 @@ export default function useAutocomplete(props) {
}
}
}
};

React.useEffect(() => {
changeHighlightedIndex('reset', 'next');
}, [inputValue]); // eslint-disable-line react-hooks/exhaustive-deps
});

React.useEffect(() => {
if (!open) {
Expand Down Expand Up @@ -423,8 +419,18 @@ export default function useAutocomplete(props) {
if (highlightedIndexRef.current >= filteredOptions.length - 1) {
setHighlightedIndex(filteredOptions.length - 1);
}

// Ignore filterOptions => options, getOptionSelected, getOptionLabel)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value, open, filterSelectedOptions, multiple]);
}, [
value,
open,
filterSelectedOptions,
changeHighlightedIndex,
setHighlightedIndex,
inputValue,
multiple,
]);

const handleOpen = event => {
if (open) {
Expand Down

0 comments on commit a33ae0f

Please sign in to comment.