forked from Cloud-Pipelines/pipeline-editor
-
Notifications
You must be signed in to change notification settings - Fork 5
Add created by me filter #1713
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
Mbeaulne
wants to merge
1
commit into
master
Choose a base branch
from
01-27-add_created_by_me_filter
base: master
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.
+237
−0
Open
Add created by me filter #1713
Changes from all commits
Commits
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
143 changes: 143 additions & 0 deletions
143
src/components/shared/CreatedByFilter/CreatedByFilter.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,143 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { DEFAULT_CREATED_BY_ME_FILTER_VALUE } from "@/utils/constants"; | ||
|
|
||
| import { CreatedByFilter } from "./CreatedByFilter"; | ||
|
|
||
| describe("CreatedByFilter", () => { | ||
| describe("rendering", () => { | ||
| it("should render with toggle unchecked and search input when no value", () => { | ||
| render(<CreatedByFilter value={undefined} onChange={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole("switch")).not.toBeChecked(); | ||
| expect(screen.getByLabelText("Created by me")).toBeInTheDocument(); | ||
| expect(screen.getByPlaceholderText("Search by user")).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole("button", { name: "Search" }), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("should show 'Created by {user}' and checked switch when value is set", () => { | ||
| render(<CreatedByFilter value="john.doe" onChange={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole("switch")).toBeChecked(); | ||
| expect(screen.getByLabelText("Created by john.doe")).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("toggle behavior", () => { | ||
| it("should call onChange with 'me' when toggle is turned on", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value={undefined} onChange={onChange} />); | ||
|
|
||
| await user.click(screen.getByRole("switch")); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(DEFAULT_CREATED_BY_ME_FILTER_VALUE); | ||
| }); | ||
|
|
||
| it("should call onChange with undefined when toggle is turned off", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render( | ||
| <CreatedByFilter | ||
| value={DEFAULT_CREATED_BY_ME_FILTER_VALUE} | ||
| onChange={onChange} | ||
| />, | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole("switch")); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(undefined); | ||
| }); | ||
|
|
||
| it("should clear filter when toggling off with existing value", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value="john.doe" onChange={onChange} />); | ||
|
|
||
| await user.click(screen.getByRole("switch")); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith(undefined); | ||
| }); | ||
| }); | ||
|
|
||
| describe("search behavior", () => { | ||
| it("should have search button disabled when input is empty", () => { | ||
| render(<CreatedByFilter value={undefined} onChange={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole("button", { name: "Search" })).toBeDisabled(); | ||
| }); | ||
|
|
||
| it("should enable search button when input has text", async () => { | ||
| const user = userEvent.setup(); | ||
| render(<CreatedByFilter value={undefined} onChange={vi.fn()} />); | ||
|
|
||
| await user.type(screen.getByPlaceholderText("Search by user"), "jane"); | ||
|
|
||
| expect(screen.getByRole("button", { name: "Search" })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it("should call onChange with typed user when search clicked", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value={undefined} onChange={onChange} />); | ||
|
|
||
| await user.type( | ||
| screen.getByPlaceholderText("Search by user"), | ||
| "jane.doe", | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "Search" })); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith("jane.doe"); | ||
| }); | ||
|
|
||
| it("should call onChange when Enter is pressed in input", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value={undefined} onChange={onChange} />); | ||
|
|
||
| const input = screen.getByPlaceholderText("Search by user"); | ||
| await user.type(input, "jane.doe{Enter}"); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith("jane.doe"); | ||
| }); | ||
|
|
||
| it("should trim whitespace from search input", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value={undefined} onChange={onChange} />); | ||
|
|
||
| await user.type( | ||
| screen.getByPlaceholderText("Search by user"), | ||
| " jane ", | ||
| ); | ||
| await user.click(screen.getByRole("button", { name: "Search" })); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith("jane"); | ||
| }); | ||
|
|
||
| it("should not call onChange when searching with only whitespace", async () => { | ||
| const user = userEvent.setup(); | ||
| const onChange = vi.fn(); | ||
| render(<CreatedByFilter value={undefined} onChange={onChange} />); | ||
|
|
||
| await user.type(screen.getByPlaceholderText("Search by user"), " "); | ||
|
|
||
| // Button should still be disabled | ||
| expect(screen.getByRole("button", { name: "Search" })).toBeDisabled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("initial state", () => { | ||
| it("should populate search input with current value", () => { | ||
| render(<CreatedByFilter value="existing-user" onChange={vi.fn()} />); | ||
|
|
||
| expect(screen.getByPlaceholderText("Search by user")).toHaveValue( | ||
| "existing-user", | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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,84 @@ | ||
| import type { KeyboardEvent } from "react"; | ||
| import { useState } from "react"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Label } from "@/components/ui/label"; | ||
| import { InlineStack } from "@/components/ui/layout"; | ||
| import { Switch } from "@/components/ui/switch"; | ||
| import { DEFAULT_CREATED_BY_ME_FILTER_VALUE } from "@/utils/constants"; | ||
|
|
||
| interface CreatedByFilterProps { | ||
| /** Current filter value. undefined means no filter, "me" means current user. */ | ||
| value: string | undefined; | ||
| /** Called when the filter value changes. undefined clears the filter. */ | ||
| onChange: (value: string | undefined) => void; | ||
| } | ||
|
|
||
| /** | ||
| * Filter component for filtering by creator/initiator. | ||
| * Provides a toggle for "Created by me" and an input for searching by specific user. | ||
| */ | ||
| export function CreatedByFilter({ value, onChange }: CreatedByFilterProps) { | ||
| const [searchUser, setSearchUser] = useState(value ?? ""); | ||
|
|
||
| const isFilterActive = value !== undefined; | ||
| const toggleText = value ? `Created by ${value}` : "Created by me"; | ||
|
|
||
| const handleToggleChange = (checked: boolean) => { | ||
| if (checked) { | ||
| // Enable filter - if no specific user set, default to "me" | ||
| if (!value) { | ||
| onChange(DEFAULT_CREATED_BY_ME_FILTER_VALUE); | ||
| setSearchUser(""); | ||
| } | ||
| } else { | ||
| // Disable filter | ||
| onChange(undefined); | ||
| } | ||
| }; | ||
|
|
||
| const handleUserSearch = () => { | ||
| const trimmedUser = searchUser.trim(); | ||
| if (trimmedUser) { | ||
| onChange(trimmedUser); | ||
| } | ||
| }; | ||
|
|
||
| const handleKeyDown = (e: KeyboardEvent<HTMLInputElement>) => { | ||
| if (e.key === "Enter" && searchUser.trim()) { | ||
| e.preventDefault(); | ||
| handleUserSearch(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <InlineStack gap="4" blockAlign="center"> | ||
| <InlineStack gap="2" blockAlign="center"> | ||
| <Switch | ||
| id="created-by-filter" | ||
| checked={isFilterActive} | ||
| onCheckedChange={handleToggleChange} | ||
| /> | ||
| <Label htmlFor="created-by-filter">{toggleText}</Label> | ||
| </InlineStack> | ||
| <InlineStack gap="1" blockAlign="center" wrap="nowrap"> | ||
| <Input | ||
| placeholder="Search by user" | ||
| value={searchUser} | ||
| onChange={(e) => setSearchUser(e.target.value)} | ||
| onKeyDown={handleKeyDown} | ||
| className="w-40" | ||
| /> | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| onClick={handleUserSearch} | ||
| disabled={!searchUser.trim()} | ||
| > | ||
| Search | ||
| </Button> | ||
| </InlineStack> | ||
| </InlineStack> | ||
| ); | ||
| } |
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
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.