From 1ede7e7e6acd961ece3efce4ff08cf1ed9814c0a Mon Sep 17 00:00:00 2001 From: Stefan de Vogelaere Date: Sun, 18 Jan 2026 14:36:31 +0100 Subject: [PATCH] refactor: extract sanitizeForTestId to shared utility Address PR review comments by: - Creating shared sanitizeForTestId utility in apps/ui/src/lib/utils.ts - Updating ProjectSwitcherItem to use the shared utility - Adding matching helper to test utils for E2E tests - Updating all E2E tests to use the sanitization helper This ensures the component and tests use identical sanitization logic, making tests robust against project names with special characters. --- .../components/project-switcher-item.tsx | 17 ++--------- apps/ui/src/lib/utils.ts | 28 +++++++++++++++++++ .../feature-manual-review-flow.spec.ts | 4 ++- .../projects/new-project-creation.spec.ts | 4 ++- .../projects/open-existing-project.spec.ts | 4 ++- apps/ui/tests/utils/core/elements.ts | 17 +++++++++++ 6 files changed, 56 insertions(+), 18 deletions(-) diff --git a/apps/ui/src/components/layout/project-switcher/components/project-switcher-item.tsx b/apps/ui/src/components/layout/project-switcher/components/project-switcher-item.tsx index d269001e..f98e05ac 100644 --- a/apps/ui/src/components/layout/project-switcher/components/project-switcher-item.tsx +++ b/apps/ui/src/components/layout/project-switcher/components/project-switcher-item.tsx @@ -1,6 +1,6 @@ import { Folder, LucideIcon } from 'lucide-react'; import * as LucideIcons from 'lucide-react'; -import { cn } from '@/lib/utils'; +import { cn, sanitizeForTestId } from '@/lib/utils'; import { getAuthenticatedImageUrl } from '@/lib/api-fetch'; import type { Project } from '@/lib/electron'; @@ -37,22 +37,9 @@ export function ProjectSwitcherItem({ const IconComponent = getIconComponent(); const hasCustomIcon = !!project.customIconPath; - // Create a sanitized project name for test ID: - // - Convert to lowercase - // - Replace spaces with hyphens - // - Remove all non-alphanumeric characters except hyphens - // - Collapse multiple hyphens into single hyphen - // - Trim leading/trailing hyphens - const sanitizedName = project.name - .toLowerCase() - .replace(/\s+/g, '-') - .replace(/[^a-z0-9-]/g, '') - .replace(/-+/g, '-') - .replace(/^-|-$/g, ''); - // Combine project.id with sanitized name for uniqueness and readability // Format: project-switcher-{id}-{sanitizedName} - const testId = `project-switcher-${project.id}-${sanitizedName}`; + const testId = `project-switcher-${project.id}-${sanitizeForTestId(project.name)}`; return (