Skip to content

Commit

Permalink
Merge branch '#15386-search_bar_returns_result_even_enable_clientside…
Browse files Browse the repository at this point in the history
…_search_is_enabled' of https://github.com/Naveen-Goud/appsmith into external-contri/#15386-search_bar_returns_result_even_enable_clientside_search_is_enabled
  • Loading branch information
rahulbarwal committed Oct 14, 2024
2 parents c6fa093 + aeb03fe commit 37a01a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import { render, fireEvent, act } from "@testing-library/react";
import SearchComponent from "../SearchComponent";
import "@testing-library/jest-dom"
import "@testing-library/jest-dom";

// Mocking the debounce function to call the function immediately
jest.mock("lodash", () => ({
Expand All @@ -23,7 +23,7 @@ describe("SearchComponent", () => {
placeholder="Search..."
value=""
{...props}
/>
/>,
);
};

Expand All @@ -33,9 +33,7 @@ describe("SearchComponent", () => {
});
const inputElement = getByPlaceholderText("Search...") as HTMLInputElement;

act(() => {
fireEvent.change(inputElement, { target: { value: "test" } });
});
fireEvent.change(inputElement, { target: { value: "test" } });

expect(inputElement.value).toBe("test");
expect(onSearchMock).toHaveBeenCalledWith("test");
Expand All @@ -49,9 +47,7 @@ describe("SearchComponent", () => {
const inputElement = getByPlaceholderText("Search...") as HTMLInputElement;
const clearButton = getByTestId("cross-icon");

act(() => {
fireEvent.click(clearButton);
});
fireEvent.click(clearButton);

expect(inputElement.value).toBe("");
expect(onSearchMock).toHaveBeenCalledWith("");
Expand All @@ -65,7 +61,13 @@ describe("SearchComponent", () => {
const inputElement = getByPlaceholderText("Search...") as HTMLInputElement;
expect(inputElement.value).toBe("initial");

rerender(<SearchComponent onSearch={onSearchMock} placeholder="Search..." value="updated" />);
rerender(
<SearchComponent
onSearch={onSearchMock}
placeholder="Search..."
value="updated"
/>,
);

expect(inputElement.value).toBe("updated");
});
Expand All @@ -76,12 +78,19 @@ describe("SearchComponent", () => {
value: "initial",
});

const inputElement = getByPlaceholderText("Search...")as HTMLInputElement;
const inputElement = getByPlaceholderText("Search...") as HTMLInputElement;
expect(inputElement.value).toBe("initial");

rerender(<SearchComponent onSearch={onSearchMock} value="" placeholder="Search..." enableClientSideSearch={false} />);
rerender(
<SearchComponent
onSearch={onSearchMock}
value=""
placeholder="Search..."
enableClientSideSearch={false}
/>,
);

expect(inputElement.value).toBe("");
expect(onSearchMock).toHaveBeenCalledWith("");
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ class SearchComponent extends React.Component<
this.setState({ localValue: this.props.value });
}

if (prevProps.enableClientSideSearch !== this.props.enableClientSideSearch) {
if (
prevProps.enableClientSideSearch !== this.props.enableClientSideSearch
) {
this.setState({ localValue: "" }, () => {
// Trigger search with an empty value to reset the table
this.props.onSearch("");
this.props.onSearch("");
});
}
}
Expand All @@ -117,8 +119,8 @@ class SearchComponent extends React.Component<

this.setState({ localValue: search });
if (this.props.enableClientSideSearch) {
this.onDebouncedSearch(search);
}
this.onDebouncedSearch(search);
}
};
clearSearch = () => {
this.setState({ localValue: "" });
Expand Down

0 comments on commit 37a01a9

Please sign in to comment.