Skip to content
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

Bugfix/add missing bounds check for findIndex #3565

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,28 @@ describe('Autocomplete', () => {
expect(input).toHaveValue('')
})

it('Correctly handles keypresses up/down when all options are disabled', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add it here, because of scope creep, but i suppose that a test case with a large amount (>10000) of contiguous disabled options would still fail in the same manner.

render(
<Autocomplete
options={items}
label={labelText}
optionDisabled={() => true}
/>,
)
const labeledNodes = await screen.findAllByLabelText(labelText)
const input = labeledNodes[0]
const optionsList = labeledNodes[1]

fireEvent.keyDown(input, { key: 'ArrowDown' })
const options = within(optionsList).queryAllByRole('option')
expect(options).toHaveLength(0) // since all options is disabled

// Prevent regression: key up/down when all (visible) options are disabled causes infinite loop
fireEvent.keyDown(input, { key: 'ArrowDown' })
fireEvent.blur(input)
expect(input).toHaveValue('')
})

const StyledAutocomplete = styled(Autocomplete)`
clip-path: unset;
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const findIndex: IndexFinderType = ({
availableItems,
}) => {
const nextItem = availableItems[index]
if (optionDisabled(nextItem)) {
if (optionDisabled(nextItem) && index >= 0 && index < availableItems.length) {
const nextIndex = calc(index)
return findIndex({ calc, index: nextIndex, availableItems, optionDisabled })
}
Expand Down
Loading