mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2026-05-15 01:37:37 +01:00
3c97778da5
* Searchable dropdown * format fix * Refactor SearchableDropdown with Fluent UI components, extract styles, and add tests (#2329) * Initial plan * Refactor SearchableDropdown with Fluent UI components and add tests - Replace native HTML elements with Fluent UI components (Stack, DefaultButton, Text) - Extract inline styles to SearchableDropdown.styles.ts - Add comprehensive unit tests (14 test cases) - Verify behavior consistency with AccountSwitcher tests Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> * Optimize SearchableDropdown with useMemo for filteredItems Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> * Fix text alignment to match original UI - ensure left alignment - Add flexContainer.justifyContent: "flex-start" to button styles - Add textAlign: "left" to button label, item styles, and empty message - Restore original left-aligned appearance for placeholder and selected text Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> * Fix TypeScript implicit type errors in SearchableDropdown tests (#2355) * Initial plan * Fix TypeScript compilation errors in SearchableDropdown.test.tsx Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: nishthaAhujaa <45535788+nishthaAhujaa@users.noreply.github.com> * ui fixes minor * format fix * added search icon and updated the text * removed callbacks * added mocked playwright data * fixed formatting --------- Co-authored-by: nishthaAhujaa <nishtha17354@iiittd.ac.in> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: Sakshi Gupta <sakshig@microsoft.com> Co-authored-by: sakshigupta12feb <sakshigupta12feb1@gmail.com>
201 lines
6.2 KiB
TypeScript
201 lines
6.2 KiB
TypeScript
import { fireEvent, render, screen } from "@testing-library/react";
|
|
import "@testing-library/jest-dom";
|
|
import React from "react";
|
|
import { SearchableDropdown } from "./SearchableDropdown";
|
|
|
|
interface TestItem {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
describe("SearchableDropdown", () => {
|
|
const mockItems: TestItem[] = [
|
|
{ id: "1", name: "Item One" },
|
|
{ id: "2", name: "Item Two" },
|
|
{ id: "3", name: "Item Three" },
|
|
];
|
|
|
|
const defaultProps = {
|
|
label: "Test Label",
|
|
items: mockItems,
|
|
selectedItem: null as TestItem | null,
|
|
onSelect: jest.fn(),
|
|
getKey: (item: TestItem) => item.id,
|
|
getDisplayText: (item: TestItem) => item.name,
|
|
placeholder: "Select an item",
|
|
filterPlaceholder: "Filter items",
|
|
className: "test-dropdown",
|
|
};
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should render with label and placeholder", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
expect(screen.getByText("Test Label")).toBeInTheDocument();
|
|
expect(screen.getByText("Select an item")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should display selected item", () => {
|
|
const propsWithSelection = {
|
|
...defaultProps,
|
|
selectedItem: mockItems[0],
|
|
};
|
|
render(<SearchableDropdown {...propsWithSelection} />);
|
|
expect(screen.getByText("Item One")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should show 'No items found' when items array is empty", () => {
|
|
const propsWithEmptyItems = {
|
|
...defaultProps,
|
|
items: [] as TestItem[],
|
|
};
|
|
render(<SearchableDropdown {...propsWithEmptyItems} />);
|
|
expect(screen.getByText("No Test Labels Found")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should open dropdown when button is clicked", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
expect(screen.getByPlaceholderText("Filter items")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should filter items based on search text", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
const searchBox = screen.getByPlaceholderText("Filter items");
|
|
fireEvent.change(searchBox, { target: { value: "Two" } });
|
|
|
|
expect(screen.getByText("Item Two")).toBeInTheDocument();
|
|
expect(screen.queryByText("Item One")).not.toBeInTheDocument();
|
|
expect(screen.queryByText("Item Three")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("should call onSelect when an item is clicked", () => {
|
|
const onSelectMock = jest.fn();
|
|
const propsWithMock = {
|
|
...defaultProps,
|
|
onSelect: onSelectMock,
|
|
};
|
|
render(<SearchableDropdown {...propsWithMock} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
const item = screen.getByText("Item Two");
|
|
fireEvent.click(item);
|
|
|
|
expect(onSelectMock).toHaveBeenCalledWith(mockItems[1]);
|
|
});
|
|
|
|
it("should close dropdown after selecting an item", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
expect(screen.getByPlaceholderText("Filter items")).toBeInTheDocument();
|
|
|
|
const item = screen.getByText("Item One");
|
|
fireEvent.click(item);
|
|
|
|
expect(screen.queryByPlaceholderText("Filter items")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("should disable button when disabled prop is true", () => {
|
|
const propsWithDisabled = {
|
|
...defaultProps,
|
|
disabled: true,
|
|
};
|
|
render(<SearchableDropdown {...propsWithDisabled} />);
|
|
|
|
const button = screen.getByRole("button");
|
|
expect(button).toBeDisabled();
|
|
});
|
|
|
|
it("should not open dropdown when disabled", () => {
|
|
const propsWithDisabled = {
|
|
...defaultProps,
|
|
disabled: true,
|
|
};
|
|
render(<SearchableDropdown {...propsWithDisabled} />);
|
|
|
|
const button = screen.getByRole("button");
|
|
fireEvent.click(button);
|
|
|
|
expect(screen.queryByPlaceholderText("Filter items")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("should show 'No items found' when search yields no results", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
const searchBox = screen.getByPlaceholderText("Filter items");
|
|
fireEvent.change(searchBox, { target: { value: "Nonexistent" } });
|
|
|
|
expect(screen.getByText("No items found")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should handle case-insensitive filtering", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
const searchBox = screen.getByPlaceholderText("Filter items");
|
|
fireEvent.change(searchBox, { target: { value: "two" } });
|
|
|
|
expect(screen.getByText("Item Two")).toBeInTheDocument();
|
|
expect(screen.queryByText("Item One")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("should clear filter text when dropdown is closed and reopened", () => {
|
|
render(<SearchableDropdown {...defaultProps} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
const searchBox = screen.getByPlaceholderText("Filter items");
|
|
fireEvent.change(searchBox, { target: { value: "Two" } });
|
|
|
|
// Close dropdown by selecting an item
|
|
const item = screen.getByText("Item Two");
|
|
fireEvent.click(item);
|
|
|
|
// Reopen dropdown
|
|
fireEvent.click(button);
|
|
|
|
// Filter text should be cleared
|
|
const reopenedSearchBox = screen.getByPlaceholderText("Filter items");
|
|
expect(reopenedSearchBox).toHaveValue("");
|
|
});
|
|
|
|
it("should use custom placeholder text", () => {
|
|
const propsWithCustomPlaceholder = {
|
|
...defaultProps,
|
|
placeholder: "Choose an option",
|
|
};
|
|
render(<SearchableDropdown {...propsWithCustomPlaceholder} />);
|
|
expect(screen.getByText("Choose an option")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should use custom filter placeholder text", () => {
|
|
const propsWithCustomFilterPlaceholder = {
|
|
...defaultProps,
|
|
filterPlaceholder: "Search here",
|
|
};
|
|
render(<SearchableDropdown {...propsWithCustomFilterPlaceholder} />);
|
|
|
|
const button = screen.getByText("Select an item");
|
|
fireEvent.click(button);
|
|
|
|
expect(screen.getByPlaceholderText("Search here")).toBeInTheDocument();
|
|
});
|
|
});
|