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

chore: external merge request from Contributor #36553

Closed
Closed
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
10 changes: 9 additions & 1 deletion app/client/packages/design-system/widgets-old/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
"license": "Apache-2.0",
"scripts": {
"lint": "yarn g:lint",
"prettier": "yarn g:prettier"
"prettier": "yarn g:prettier",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage"
},
"devDependencies": {
"@svgr/webpack": "5.5.0",
"@testing-library/jest-dom": "^6.5.0",
"@types/emoji-mart": "3.0.4",
"@types/jest": "^29.5.13",
"@types/loadable__component": "^5.13.4",
"@types/react-form": "^4.0.2",
"@types/react-table": "*",
Expand All @@ -20,9 +25,12 @@
"eslint-config-react": "^1.1.7",
"eslint-plugin-storybook": "^0.6.8",
"identity-obj-proxy": "3.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"playwright": "^1.25.1",
"postcss": "^8.4.31",
"postcss-import": "^14.1.0",
"ts-jest": "^29.2.5",
"tsconfig-paths-webpack-plugin": "^3.5.2"
},
"peerDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from "react";
import { render, fireEvent, screen } from "@testing-library/react";
import SearchComponent from "../SearchComponent";
import "@testing-library/jest-dom";

jest.mock("lodash", () => ({
debounce: (fn: any) => fn,
}));

jest.mock("../utils/icon-loadables", () => ({
importSvg: jest.fn().mockReturnValue(() => <svg data-testid="cross-icon" />),
}));

describe("SearchComponent", () => {
const onSearchMock = jest.fn();

const renderComponent = (props = {}) => {
return render(
<SearchComponent
onSearch={onSearchMock}
placeholder="Search..."
value=""
{...props}
/>,
);
};

it("1.should clear the search value and trigger onSearch with an empty value when the cross icon is clicked anh have focused", () => {
const { getByPlaceholderText, getByTestId } = renderComponent({
enableClientSideSearch: true,
value: "test",
});
const inputElement = screen.getByPlaceholderText(
"Search...",
) as HTMLInputElement;
const clearButton = screen.getByTestId("cross-icon");

fireEvent.click(clearButton);

expect(inputElement).toHaveFocus();
expect(inputElement.value).toBe("");
expect(onSearchMock).toHaveBeenCalledWith("");
});

it("2.should reset localValue when component receives a new value prop", () => {
const { getByPlaceholderText, rerender } = renderComponent({
value: "initial",
});

const inputElement = screen.getByPlaceholderText(
"Search...",
) as HTMLInputElement;

expect(inputElement.value).toBe("initial");

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

expect(inputElement.value).toBe("updated");
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class SearchComponent extends React.Component<
SearchProps,
{ localValue: string }
> {
private inputRef = React.createRef<HTMLInputElement>();
onDebouncedSearch = debounce(this.props.onSearch, 400);
constructor(props: SearchProps) {
super(props);
Expand Down Expand Up @@ -113,6 +114,10 @@ class SearchComponent extends React.Component<
clearSearch = () => {
this.setState({ localValue: "" });
this.onDebouncedSearch("");

if (this.inputRef.current) {
this.inputRef.current.focus();
}
};

render() {
Expand All @@ -121,6 +126,7 @@ class SearchComponent extends React.Component<
<SearchInputWrapper
autoFocus={this.props.autoFocus}
className={`${this.props.className} t--search-input`}
inputRef={this.inputRef}
leftIcon="search"
onChange={this.handleSearch}
placeholder={this.props.placeholder}
Expand Down
3 changes: 2 additions & 1 deletion app/client/packages/design-system/widgets-old/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"extends": "../../../tsconfig.json",
"compilerOptions": {
"baseUrl": "./src",
"typeRoots": ["./typings", "./node_modules/@types"]
"typeRoots": ["./typings", "./node_modules/@types"],
"types": ["jest"]
},
"include": ["./src/**/*"],
"exclude": ["node_modules"]
Expand Down
11 changes: 10 additions & 1 deletion app/client/src/widgets/MultiSelectTreeWidget/component/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,16 @@ function MultiTreeSelectComponent({
const clearButton = useMemo(
() =>
filter ? (
<Button icon="cross" minimal onClick={() => setFilter("")} />
<Button
icon="cross"
minimal
onClick={() => {
if (inputRef.current) {
inputRef.current.focus();
}
setFilter("");
}}
/>
) : null,
[filter],
);
Expand Down
11 changes: 10 additions & 1 deletion app/client/src/widgets/MultiSelectWidgetV2/component/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,16 @@ function MultiSelectComponent({
const clearButton = useMemo(
() =>
filter ? (
<Button icon="cross" minimal onClick={() => setFilter("")} />
<Button
icon="cross"
minimal
onClick={() => {
if (inputRef.current) {
inputRef.current.focus();
}
setFilter("");
}}
/>
) : null,
[filter],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,16 @@ function SingleSelectTreeComponent({
const clearButton = useMemo(
() =>
filter ? (
<Button icon="cross" minimal onClick={() => setFilter("")} />
<Button
icon="cross"
minimal
onClick={() => {
if (inputRef.current) {
inputRef.current.focus();
}
setFilter("");
}}
/>
) : null,
[filter],
);
Expand Down
Loading
Loading