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

Listbox not checking index access on GoToOption action #3519

Open
themagickoala opened this issue Oct 10, 2024 · 1 comment
Open

Listbox not checking index access on GoToOption action #3519

themagickoala opened this issue Oct 10, 2024 · 1 comment
Assignees

Comments

@themagickoala
Copy link

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v2.1.9

What browser are you using?

Vitest (vitest-dom v0.1.1)

Reproduction URL

I've not been able to reproduce my issue in a minimal reproduction outside of my project. Moreover, I can't even reproduce it in a browser, it's only happening in my tests. I'm not sure exactly why it's happening, but I know if I tweak this one section in the listbox.js file, it fixes my issue.

Describe your issue

When running a certain component in my Vitest tests with React Testing Library, listbox.js is throwing an error stating Cannot read property of undefined (reading 'id'). This is thrown from https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-react/src/components/listbox/listbox.tsx#L1229, but I believe the cause to be https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-react/src/components/listbox/listbox.tsx#L243 - this is the one line of code in the file where findIndex is called without explicitly checking whether the found index is equal to -1.

As I'm not familiar enough with the codebase, I'm not comfortable submitting a PR here as I don't know what the intended behaviour is. However, if I add a check for -1 at either of the lines linked above, my tests pass just fine.

@RobinMalfait RobinMalfait self-assigned this Oct 17, 2024
@RobinMalfait
Copy link
Member

Hey! Can you somehow share the test that is failing?

While checking for -1 is not a bad idea, I also would like to understand in what scenario this can happen.

The only reasons it can happen in the code is if we internally call goToOption (which is triggered when an option is being focused or an enter or mousemove event was fired on an option) with an id (coming from the option) that somehow doesn't exist in the list of options anymore the moment we actually dispatch the action.

We do have an optimization where if you let's say hover over a lot of options, then we debounce this action and only go to the last option you hovered over.

So the only thing I can think of is that the code in your test is adding/removing items (rapidly) while you are hovering or something similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants