import { cleanup, render, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { afterEach, beforeEach, expect, it, vi } from 'vitest' import { Watchlist } from '../src/pages/Watchlist' const { search, list, add } = vi.hoisted(() => ({ search: vi.fn(), list: vi.fn(), add: vi.fn() })) vi.mock('@/api/assets', () => ({ assetsApi: { search, list, add, remove: vi.fn() } })) afterEach(cleanup) beforeEach(() => { vi.clearAllMocks() list.mockResolvedValue([]) search.mockResolvedValue([{ company_id: 'company-ibm', name: 'International Business Machines Corporation', country: 'United States', exchange: 'New York Stock Exchange', mic: 'XNYS', symbol: 'IBM' }]) }) it.each([ { language: 'fr', label: 'Nom ou nom courant de l’entreprise', help: /Intel.*IBM/, results: 'Résultats des entreprises', query: 'Intel', empty: 'Aucune entreprise trouvée. Essayez un autre nom, sigle courant ou symbole boursier.' }, { language: 'en', label: 'Company name or commonly used name', help: /Intel.*IBM/, results: 'Company results', query: 'IBM', empty: 'No company found. Try another name, commonly used acronym or stock symbol.' }, ] as const)('affiche les textes accessibles $language et garde la recherche explicite au clavier', async ({ language, label, help, results, query, empty }) => { const user = userEvent.setup() render() if (language === 'en') await user.selectOptions(screen.getByLabelText('Langue / Language'), 'en') const input = screen.getByRole('textbox', { name: label }) expect(input).toHaveAccessibleDescription(help) expect(input).toHaveAttribute('maxLength', '100') await user.type(input, query) expect(search).not.toHaveBeenCalled() await user.keyboard('{Enter}') await waitFor(() => expect(search).toHaveBeenCalledWith(query, 20)) const region = screen.getByRole('region', { name: results }) expect(await within(region).findByText('International Business Machines Corporation')).toBeInTheDocument() expect(within(region).getByText('United States')).toBeInTheDocument() expect(within(region).getByText('New York Stock Exchange')).toBeInTheDocument() for (const value of ['IBM', 'XNYS', 'company-ibm']) expect(within(region).queryByText(value, { exact: true })).not.toBeInTheDocument() expect(add).not.toHaveBeenCalled() search.mockResolvedValueOnce([]) await user.clear(input) await user.type(input, 'Unknown{Enter}') expect(await screen.findByText(empty)).toHaveAttribute('role', 'status') })