From ddeb87e293e69ef1933d9db9eb40db24dbc1fc24 Mon Sep 17 00:00:00 2001 From: hlimas Date: Mon, 2 Oct 2023 17:26:52 -0700 Subject: [PATCH] FIX: ListboxButton - use most recent index instead of state variable --- .../__tests__/index.spec.tsx | 24 +++++++++++++++++++ src/ebay-listbox-button/listbox-button.tsx | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ebay-listbox-button/__tests__/index.spec.tsx b/src/ebay-listbox-button/__tests__/index.spec.tsx index 676f6133..f2ceb5b0 100644 --- a/src/ebay-listbox-button/__tests__/index.spec.tsx +++ b/src/ebay-listbox-button/__tests__/index.spec.tsx @@ -106,6 +106,30 @@ describe("", () => { expect(spy).toBeCalledWith(anySyntheticEvent, { index, selected: ['AA'], wasClicked: true }) }) + + it(`should pass the current selected value`, () => { + const spy = jest.fn() + render( + + Option 1 + Option 2 + Option 3 + + ) + 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', () => { diff --git a/src/ebay-listbox-button/listbox-button.tsx b/src/ebay-listbox-button/listbox-button.tsx index d3ab9151..3b47dfb4 100644 --- a/src/ebay-listbox-button/listbox-button.tsx +++ b/src/ebay-listbox-button/listbox-button.tsx @@ -110,7 +110,7 @@ const ListboxButton: FC = ({ collapseListbox() setActiveDescendant(index) buttonRef.current.focus() - onChange(e, { index, selected: [getSelectedValueByIndex(selectedIndex)], wasClicked }) + onChange(e, { index, selected: [getSelectedValueByIndex(index)], wasClicked }) setWasClicked(false) }