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

FIX: ListboxButton - use most recent index instead of state variable #251

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions src/ebay-listbox-button/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ describe("<EbayListboxButton>", () => {

expect(spy).toBeCalledWith(anySyntheticEvent, { index, selected: ['AA'], wasClicked: true })
})

it(`should pass the current selected value`, () => {
const spy = jest.fn()
render(
<EbayListboxButton onChange={spy}>
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
<EbayListboxButtonOption value="CC">Option 3</EbayListboxButtonOption>
</EbayListboxButton>
)
fireEvent.click(getByRole('button'))

fireEvent.mouseDown(getAllByRole('option')[0])
fireEvent.click(getAllByRole('option')[0])
expect(spy).toBeCalledWith(anySyntheticEvent, { index: 0, selected: ['AA'], wasClicked: true })

fireEvent.mouseDown(getAllByRole('option')[1])
fireEvent.click(getAllByRole('option')[1])
expect(spy).toBeCalledWith(anySyntheticEvent, { index: 1, selected: ['BB'], wasClicked: true })

fireEvent.mouseDown(getAllByRole('option')[2])
fireEvent.click(getAllByRole('option')[2])
expect(spy).toBeCalledWith(anySyntheticEvent, { index: 2, selected: ['CC'], wasClicked: true })
})
})
describe('on expand', () => {
it('should fire onExpand event', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/ebay-listbox-button/listbox-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
collapseListbox()
setActiveDescendant(index)
buttonRef.current.focus()
onChange(e, { index, selected: [getSelectedValueByIndex(selectedIndex)], wasClicked })
onChange(e, { index, selected: [getSelectedValueByIndex(index)], wasClicked })
setWasClicked(false)
}

Expand Down
Loading