-
Couldn't load subscription status.
- Fork 1
31 design and implement shared reusable search bar component #37
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
Open
nancymuyeh
wants to merge
32
commits into
main
Choose a base branch
from
31-design-and-implement-shared-reusable-search-bar-component
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 21 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
33c23f3
feat(ui): implement reusable SearchBar component
nancymuyeh dadff60
feat(ui): implement reusable SearchBar component
nancymuyeh 02fdf91
feat(ui): implement reusable SearchBar component
nancymuyeh 07f3c4a
feat(ui): implement reusable SearchBar component
nancymuyeh 581d387
Merge branch 'main' into 31-design-and-implement-shared-reusable-sear…
nancymuyeh 62a576d
fix(ui): mark SearchBar component prop as read-only
nancymuyeh 0f3de59
Merge branch '31-design-and-implement-shared-reusable-search-bar-comp…
nancymuyeh d886154
fix(ui): increase test coverage
nancymuyeh b198f93
Merge branch 'main' into 31-design-and-implement-shared-reusable-sear…
nancymuyeh 190f0b3
fix(ui): increase test coverage
nancymuyeh a24a3d9
fix(ui): refactor useSearchBar hook with extracted helper functions f…
nancymuyeh a74f693
fix(ui): refactor useSearchBar hook with extracted helper functions f…
nancymuyeh 390b514
Update packages/ui/src/components/SearchBar/SearchBar.view.tsx
nancymuyeh 7f2e481
fix(ui): refactor useSearchBar component for SonarQube compliance
nancymuyeh 5f5396c
fix(ui): refactor useSearchBar component for SonarQube compliance
nancymuyeh 216d993
fix(ui): refactor SearchBar component for SonarQube compliance
nancymuyeh e04b8bd
fix(ui): refactor SearchBar component for SonarQube compliance
nancymuyeh 3328a72
chore(ui): merge main
nancymuyeh 89b66c6
fix(ui): refactor SearchBar component for SonarQube compliance
nancymuyeh 1161ec1
fix(ui): refactor SearchBar component with more simplifies functionality
nancymuyeh cd8acd7
fix(ui): update searchbar variants and styling
nancymuyeh 1e6e858
fix(ui): remove unused dependencies
nancymuyeh 103e6c8
fix(ui): remove unused dependencies
nancymuyeh 0cd4017
fix(ui): update searchbar componenet to use global styling from style…
nancymuyeh 8ba9f4b
Merge branch 'main' into 31-design-and-implement-shared-reusable-sear…
nancymuyeh e885bf6
fix(ui): fix minor lint issue
nancymuyeh 9d2d22f
fix(ui): merge main
nancymuyeh 632273e
Merge remote-tracking branch 'origin' into 31-design-and-implement-sh…
nancymuyeh eafd972
Merge branch 'main' into 31-design-and-implement-shared-reusable-sear…
nancymuyeh 0fffcc2
Merge branch '31-design-and-implement-shared-reusable-search-bar-comp…
nancymuyeh 9999bf2
fix(ui): fix minor lint issue
nancymuyeh f87027b
fix(ui): fix minor lint issue
nancymuyeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
packages/ui/src/components/SearchBar/SearchBar.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| /** | ||
| * @jest-environment jsdom | ||
| */ | ||
nancymuyeh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { render, screen, waitFor } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { SearchBar } from "./SearchBar"; | ||
|
|
||
| describe("SearchBar", () => { | ||
| const mockOnValueChange = jest.fn(); | ||
| const mockOnSearch = jest.fn(); | ||
|
|
||
| const defaultProps = { | ||
| onValueChange: mockOnValueChange, | ||
| onSearch: mockOnSearch, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("Basic Rendering", () => { | ||
| it("renders with default props", () => { | ||
| render(<SearchBar />); | ||
| expect(screen.getByRole("textbox")).toBeInTheDocument(); | ||
| expect(screen.getByPlaceholderText("Search...")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders with custom placeholder", () => { | ||
| render(<SearchBar placeholder="Find items..." />); | ||
| expect(screen.getByPlaceholderText("Find items...")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("applies custom className", () => { | ||
| const { container } = render(<SearchBar className="custom-class" />); | ||
| expect(container.firstChild).toHaveClass("custom-class"); | ||
| }); | ||
|
|
||
| it("renders disabled state", () => { | ||
| render(<SearchBar disabled />); | ||
| expect(screen.getByRole("textbox")).toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Value Management", () => { | ||
| it("displays controlled value", () => { | ||
| render(<SearchBar value="test value" {...defaultProps} />); | ||
| expect(screen.getByDisplayValue("test value")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("calls onValueChange when typing", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar {...defaultProps} />); | ||
|
|
||
| const input = screen.getByRole("textbox"); | ||
| await user.type(input, "hello"); | ||
|
|
||
| expect(mockOnValueChange).toHaveBeenCalledTimes(5); // Each character | ||
| expect(mockOnValueChange).toHaveBeenNthCalledWith(1, "h"); | ||
| expect(mockOnValueChange).toHaveBeenNthCalledWith(2, "e"); | ||
| expect(mockOnValueChange).toHaveBeenNthCalledWith(3, "l"); | ||
| expect(mockOnValueChange).toHaveBeenNthCalledWith(4, "l"); | ||
| expect(mockOnValueChange).toHaveBeenNthCalledWith(5, "o"); | ||
| }); | ||
|
|
||
| it("handles empty initial value", () => { | ||
| render(<SearchBar value="" {...defaultProps} />); | ||
| expect(screen.getByRole("textbox")).toHaveValue(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Search Functionality", () => { | ||
| it("triggers onSearch when Enter is pressed", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar value="search term" {...defaultProps} />); | ||
|
|
||
| const input = screen.getByRole("textbox"); | ||
| await user.type(input, "{Enter}"); | ||
|
|
||
| expect(mockOnSearch).toHaveBeenCalledWith("search term"); | ||
| }); | ||
|
|
||
| it("does not trigger onSearch on other keys", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar value="test" {...defaultProps} />); | ||
|
|
||
| const input = screen.getByRole("textbox"); | ||
| await user.type(input, "{Space}{Tab}"); | ||
|
|
||
| expect(mockOnSearch).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Clear Functionality", () => { | ||
| it("shows clear button when showClear is true and has value", () => { | ||
| render(<SearchBar value="test" showClear {...defaultProps} />); | ||
| expect(screen.getByLabelText("Clear input")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides clear button when showClear is false", () => { | ||
| render(<SearchBar value="test" showClear={false} {...defaultProps} />); | ||
| expect(screen.queryByLabelText("Clear input")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides clear button when no value", () => { | ||
| render(<SearchBar value="" showClear {...defaultProps} />); | ||
| expect(screen.queryByLabelText("Clear input")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("clears input when clear button is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar value="test" showClear {...defaultProps} />); | ||
|
|
||
| const clearButton = screen.getByLabelText("Clear input"); | ||
| await user.click(clearButton); | ||
|
|
||
| expect(mockOnValueChange).toHaveBeenCalledWith(""); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Loading State", () => { | ||
| it("shows loading spinner when isLoading is true", () => { | ||
| render(<SearchBar isLoading {...defaultProps} />); | ||
| // Look for the loading icon by class | ||
| const loadingIcon = document.querySelector(".animate-spin"); | ||
| expect(loadingIcon).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("hides loading spinner when isLoading is false", () => { | ||
| render(<SearchBar isLoading={false} {...defaultProps} />); | ||
| const loadingIcon = document.querySelector(".animate-spin"); | ||
| expect(loadingIcon).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Variants", () => { | ||
| it("renders default variant", () => { | ||
| const { container } = render(<SearchBar variant="default" />); | ||
| expect(container.firstChild?.firstChild).toHaveClass( | ||
| "flex items-center gap-2", | ||
| ); | ||
| }); | ||
|
|
||
| it("renders withButton variant", () => { | ||
| render(<SearchBar variant="withButton" {...defaultProps} />); | ||
| expect( | ||
| screen.getByRole("button", { name: /search/i }), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("triggers onSearch when search button is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <SearchBar | ||
| variant="withButton" | ||
| value="button search" | ||
| {...defaultProps} | ||
| />, | ||
| ); | ||
|
|
||
| const searchButton = screen.getByRole("button", { name: /search/i }); | ||
| await user.click(searchButton); | ||
|
|
||
| expect(mockOnSearch).toHaveBeenCalledWith("button search"); | ||
| }); | ||
|
|
||
| it("renders expandable variant collapsed by default", () => { | ||
| render(<SearchBar variant="expandable" />); | ||
| expect(screen.getByLabelText("Open search")).toBeInTheDocument(); | ||
| expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("expands when expandable search button is clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar variant="expandable" {...defaultProps} />); | ||
|
|
||
| const expandButton = screen.getByLabelText("Open search"); | ||
| await user.click(expandButton); | ||
|
|
||
| expect(screen.getByRole("textbox")).toBeInTheDocument(); | ||
| expect(screen.queryByLabelText("Open search")).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("collapses expandable on Enter key", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar variant="expandable" {...defaultProps} />); | ||
|
|
||
| // First expand | ||
| await user.click(screen.getByLabelText("Open search")); | ||
| expect(screen.getByRole("textbox")).toBeInTheDocument(); | ||
|
|
||
| // Then press Enter | ||
| await user.type(screen.getByRole("textbox"), "{Enter}"); | ||
|
|
||
| // Should be collapsed again | ||
| await waitFor(() => { | ||
| expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
| expect(screen.getByLabelText("Open search")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it("collapses expandable on Escape key", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<SearchBar variant="expandable" {...defaultProps} />); | ||
|
|
||
| // First expand | ||
| await user.click(screen.getByLabelText("Open search")); | ||
| expect(screen.getByRole("textbox")).toBeInTheDocument(); | ||
|
|
||
| // Then press Escape | ||
| await user.type(screen.getByRole("textbox"), "{Escape}"); | ||
|
|
||
| // Should be collapsed | ||
| await waitFor(() => { | ||
| expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); | ||
| expect(screen.getByLabelText("Open search")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Size Variants", () => { | ||
| it("applies small size class", () => { | ||
| const { container } = render(<SearchBar size="sm" />); | ||
| expect(container.querySelector(".h-8")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("applies medium size class (default)", () => { | ||
| const { container } = render(<SearchBar size="md" />); | ||
| expect(container.querySelector(".h-10")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("applies large size class", () => { | ||
| const { container } = render(<SearchBar size="lg" />); | ||
| expect(container.querySelector(".h-12")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Accessibility", () => { | ||
| it("has proper ARIA attributes", () => { | ||
| render(<SearchBar />); | ||
| const input = screen.getByRole("textbox"); | ||
| expect(input).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("has accessible button labels", () => { | ||
| render(<SearchBar value="test" showClear variant="withButton" />); | ||
| expect(screen.getByLabelText("Clear input")).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole("button", { name: /search/i }), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("has accessible expandable button", () => { | ||
| render(<SearchBar variant="expandable" />); | ||
| expect(screen.getByLabelText("Open search")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.