diff --git a/frontend/application/components/IngredientWiki.tsx b/frontend/application/components/IngredientWiki.tsx index 64b6427d..d6353f7f 100644 --- a/frontend/application/components/IngredientWiki.tsx +++ b/frontend/application/components/IngredientWiki.tsx @@ -2,7 +2,7 @@ import { useState, useEffect, useMemo } from "react"; import { Product, IngredientInfo } from "../lib/data/products"; import { motion } from "motion/react"; -import { FlaskConical, CheckCircle2, ChevronRight, ChevronLeft, Beaker, Search } from "lucide-react"; +import { FlaskConical, CheckCircle2, ChevronLeft, ChevronDown, Beaker, Search } from "lucide-react"; import { productService } from "../lib/services/productService"; import Link from "next/link"; @@ -68,6 +68,7 @@ export default function IngredientWiki() { const [products, setProducts] = useState([]); const [searchQuery, setSearchQuery] = useState(""); const [visibleCount, setVisibleCount] = useState(8); + const [expandedIngredients, setExpandedIngredients] = useState>(new Set()); useEffect(() => { productService.getProducts({ limit: 999 }) @@ -115,6 +116,12 @@ export default function IngredientWiki() { useEffect(() => { if (!termParam || ingredientsWiki.length === 0) return; + setExpandedIngredients(current => { + if (current.has(termParam)) return current; + const next = new Set(current); + next.add(termParam); + return next; + }); const targetIndex = ingredientsWiki.findIndex(ing => ing.id === termParam); if (targetIndex >= visibleCount) { setVisibleCount(Math.ceil((targetIndex + 1) / 8) * 8); @@ -240,13 +247,51 @@ export default function IngredientWiki() { {visibleIngredients.map((ing, idx) => { const hasIngredient = getProductsForIngredient(ing.id, ing.name); const isTargeted = termParam === ing.id; + const isExpanded = expandedIngredients.has(ing.id); + const safeId = ing.id.replace(/[^a-zA-Z0-9_-]/g, '-'); + const triggerId = `wiki-trigger-${safeId}`; + const panelId = `wiki-panel-${safeId}`; return (
+

+ +

+ + ); })} diff --git a/frontend/application/components/__tests__/IngredientWiki.test.tsx b/frontend/application/components/__tests__/IngredientWiki.test.tsx index 68322ba3..64ecc07f 100644 --- a/frontend/application/components/__tests__/IngredientWiki.test.tsx +++ b/frontend/application/components/__tests__/IngredientWiki.test.tsx @@ -281,6 +281,88 @@ describe('IngredientWiki Component', () => { }); + it('renders accessible disclosures and preserves complete content while toggling', async () => { + (api.get as any).mockResolvedValueOnce({ data: [{ + id: 'biotin', slug: 'biotin', nameFa: 'بیوتین', description: 'توضیح کامل بیوتین', + benefits: ['فایده اول', 'فایده دوم'], + }] }); + (productService.getProducts as any).mockResolvedValueOnce({ data: [{ + id: 'p-1', slug: 'biotin-tabs', name: 'محصول بیوتین', main_ingredients: ['بیوتین'], image: '/biotin.png', + }] }); + + const { container } = render(); + await waitFor(() => expect(screen.getByRole('button', { name: /بیوتین.*نمایش جزئیات/ })).toBeInTheDocument()); + + const trigger = screen.getByRole('button', { name: /بیوتین.*نمایش جزئیات/ }); + expect(trigger).toHaveAttribute('aria-expanded', 'false'); + const panelId = trigger.getAttribute('aria-controls'); + expect(panelId).toBeTruthy(); + const panel = container.querySelector(`#${panelId}`); + expect(panel).toHaveAttribute('role', 'region'); + expect(panel).toHaveAttribute('aria-labelledby', trigger.id); + expect(panel).not.toBeVisible(); + expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument(); + expect(screen.getByText('فایده اول')).toBeInTheDocument(); + expect(screen.getByText('فایده دوم')).toBeInTheDocument(); + expect(screen.getByText('محصول بیوتین')).toBeInTheDocument(); + + fireEvent.click(trigger); + expect(trigger).toHaveAttribute('aria-expanded', 'true'); + expect(panel).toBeVisible(); + expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument(); + expect(screen.getByText('محصول بیوتین')).toBeInTheDocument(); + + fireEvent.click(trigger); + expect(trigger).toHaveAttribute('aria-expanded', 'false'); + expect(panel).not.toBeVisible(); + expect(screen.getByText('توضیح کامل بیوتین')).toBeInTheDocument(); + expect(screen.getByText('محصول بیوتین')).toBeInTheDocument(); + }); + + it('auto-expands a deep-linked ingredient beyond the first page', async () => { + mockSearchParamsGet.mockImplementation((param: string) => param === 'term' ? 'ingredient-13' : null); + (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); + const ingredients = Array.from({ length: 18 }, (_, index) => ({ + id: `ingredient-${index + 1}`, + slug: `ingredient-${index + 1}`, + nameFa: `ماده شماره ${index + 1}`, + description: `توضیح ماده ${index + 1}`, + benefits: [], + })); + (api.get as any).mockResolvedValueOnce({ data: ingredients }); + + render(); + + const targetTrigger = await screen.findByRole('button', { name: /ماده شماره 13.*بستن جزئیات/ }); + expect(targetTrigger).toHaveAttribute('aria-expanded', 'true'); + expect(document.getElementById(targetTrigger.getAttribute('aria-controls')!)).toBeVisible(); + expect(screen.getByText(/نمایش ۱۶ از ۱۸ ماده/)).toBeInTheDocument(); + expect(document.getElementById('wiki-trigger-ingredient-1')).toHaveAttribute('aria-expanded', 'false'); + }); + + it('keeps an expanded ingredient open after loading another page', async () => { + (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); + const ingredients = Array.from({ length: 18 }, (_, index) => ({ + id: `ingredient-${index + 1}`, + slug: `ingredient-${index + 1}`, + nameFa: `ماده شماره ${index + 1}`, + description: `توضیح ماده ${index + 1}`, + benefits: [], + })); + (api.get as any).mockResolvedValueOnce({ data: ingredients }); + + render(); + const firstTrigger = await screen.findByRole('button', { name: /ماده شماره 1.*نمایش جزئیات/ }); + fireEvent.click(firstTrigger); + expect(firstTrigger).toHaveAttribute('aria-expanded', 'true'); + + fireEvent.click(screen.getByRole('button', { name: /مشاهده ۸ ماده بیشتر/ })); + expect(firstTrigger).toHaveAttribute('aria-expanded', 'true'); + expect(screen.getByRole('button', { name: /ماده شماره 9.*نمایش جزئیات/ })).toHaveAttribute('aria-expanded', 'false'); + expect(screen.getByText(/نمایش ۱۶ از ۱۸ ماده/)).toBeInTheDocument(); + }); + + it('keeps the mobile breadcrumb close to the top edge', () => { (productService.getProducts as any).mockResolvedValueOnce({ data: [] }); (api.get as any).mockResolvedValueOnce({ data: [] });