diff --git a/app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx b/app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx new file mode 100644 index 00000000000..cc67b8b7361 --- /dev/null +++ b/app/client/src/widgets/SelectWidget/component/SelectWidgetComponent.test.tsx @@ -0,0 +1,36 @@ +import React from "react"; +import { fireEvent, render, screen } from "@testing-library/react"; +import SelectComponent, { type SelectComponentProps } from "./index"; + +const mockProps: SelectComponentProps = { + borderRadius: "", + compactMode: false, + dropDownWidth: 0, + height: 0, + isFilterable: false, + isLoading: false, + isValid: false, + labelText: "", + onFilterChange: jest.fn(), + onOptionSelected: jest.fn(), + onDropdownClose: jest.fn(), + onDropdownOpen: jest.fn(), + options: [], + serverSideFiltering: false, + width: 0, + widgetId: "", +}; + +describe("SelectComponent", () => { + it("should call onDropdownClose only once when select button is clicked twice", () => { + render(); + const dropdownButton = screen.getByTestId("selectbutton.btn.main"); + + fireEvent.click(dropdownButton); + + expect(mockProps.onDropdownOpen).toHaveBeenCalledTimes(1); + fireEvent.click(dropdownButton); + + expect(mockProps.onDropdownClose).toHaveBeenCalledTimes(1); + }); +}); diff --git a/app/client/src/widgets/SelectWidget/component/index.tsx b/app/client/src/widgets/SelectWidget/component/index.tsx index 862934a2e9c..127edda7b35 100644 --- a/app/client/src/widgets/SelectWidget/component/index.tsx +++ b/app/client/src/widgets/SelectWidget/component/index.tsx @@ -75,7 +75,15 @@ class SelectComponent extends React.Component< } }; - togglePopoverVisibility = () => { + togglePopoverVisibilityFromButton = () => { + this.togglePopoverVisibility(true); + }; + + togglePopoverVisibility = (isButtonClick = false) => { + // This is an edge case, this method gets called twice if user closes it by clicking on the `SelectButton` + // which in turn triggers handleOnDropdownClose twice, to solve we have this exception to tell if click event is from button + if (isButtonClick && this.state.isOpen) return; + if (this.state.isOpen) { this.handleOnDropdownClose(); } else { @@ -456,7 +464,7 @@ class SelectComponent extends React.Component< hideCancelIcon={this.props.hideCancelIcon} isRequired={this.props.isRequired} spanRef={this.spanRef} - togglePopoverVisibility={this.togglePopoverVisibility} + togglePopoverVisibility={this.togglePopoverVisibilityFromButton} tooltipText={tooltipText} value={this.props.value?.toString()} />