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:Ensure alert message triggers only once when onDropdownClose in select widget #34812

Merged
Show file tree
Hide file tree
Changes from 9 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
36 changes: 36 additions & 0 deletions app/client/src/widgets/SelectWidget/component/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from "react";
import { render, screen } from "@testing-library/react";
import SelectComponent, { type SelectComponentProps } from "./index";
import userEvent from "@testing-library/user-event";

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(<SelectComponent {...mockProps} />);
const dropdownButton = screen.getByRole("button");
rahulbarwal marked this conversation as resolved.
Show resolved Hide resolved
userEvent.click(dropdownButton);

expect(mockProps.onDropdownOpen).toHaveBeenCalledTimes(1);
userEvent.click(dropdownButton);

expect(mockProps.onDropdownClose).toHaveBeenCalledTimes(1);
});
});
13 changes: 10 additions & 3 deletions app/client/src/widgets/SelectWidget/component/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
prasad-madine marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -189,7 +197,6 @@ class SelectComponent extends React.Component<
handleCloseList = () => {
if (this.state.isOpen) {
this.togglePopoverVisibility();

if (!this.props.selectedIndex) return;

return this.handleActiveItemChange(
Expand Down Expand Up @@ -456,7 +463,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()}
/>
Expand Down