import { cleanup, render, screen, within, waitFor } 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 { list, search, add } = vi.hoisted(() => ({ list: vi.fn(), search: vi.fn(), add: vi.fn() })) vi.mock('@/api/assets', () => ({ assetsApi: { list, search, add, remove: vi.fn() } })) afterEach(cleanup) beforeEach(() => { vi.clearAllMocks(); list.mockResolvedValue([]) }) it.each([ { language: 'fr', symbol: 'AMZN', name: 'Amazon.com, Inc.' }, { language: 'fr', symbol: 'BABA', name: 'Alibaba Group Holding Limited' }, { language: 'en', symbol: 'AMZN', name: 'Amazon.com, Inc.' }, { language: 'en', symbol: 'BABA', name: 'Alibaba Group Holding Limited' }, ] as const)('$language explique la recherche par symbole $symbol sans le rendre dans les résultats', async ({ language, symbol, name }) => { const company = { company_id: `company-${symbol}`, name, country: 'United States', exchange: 'Nasdaq', mic: 'XNAS', symbol } search.mockResolvedValueOnce([company]).mockResolvedValueOnce([]) add.mockResolvedValueOnce({ id: 'followed-company', ...company }) const user = userEvent.setup() render() if (language === 'en') await user.selectOptions(screen.getByLabelText('Langue / Language'), 'en') const input = screen.getByRole('textbox') expect(input).toHaveAccessibleDescription(language === 'fr' ? /nom,.*sigle courant.*symbole boursier/ : /name,.*commonly used acronym.*stock symbol/) await user.type(input, symbol) expect(search).not.toHaveBeenCalled() await user.keyboard('{Enter}') await waitFor(() => expect(search).toHaveBeenCalledWith(symbol, 20)) const results = screen.getByRole('region', { name: language === 'fr' ? 'Résultats des entreprises' : 'Company results' }) expect(await within(results).findByText(name)).toBeInTheDocument() for (const hidden of [symbol, company.company_id, company.mic]) expect(within(results).queryByText(hidden, { exact: true })).not.toBeInTheDocument() expect(add).not.toHaveBeenCalled() await user.click(within(results).getByRole('button', { name: language === 'fr' ? 'Suivre cette entreprise' : 'Follow this company' })) await waitFor(() => expect(add).toHaveBeenCalledWith({ company_id: company.company_id })) const followed = screen.getByRole('region', { name: language === 'fr' ? 'Entreprises suivies' : 'Followed companies' }) expect(within(followed).getByText(name)).toBeInTheDocument() expect(within(followed).queryByText(symbol, { exact: true })).not.toBeInTheDocument() expect(within(followed).queryByText(company.mic)).not.toBeInTheDocument() await user.clear(input) await user.type(input, 'Unknown{Enter}') expect(await screen.findByText(language === 'fr' ? 'Aucune entreprise trouvée. Essayez un autre nom, sigle courant ou symbole boursier.' : 'No company found. Try another name, commonly used acronym or stock symbol.')).toHaveAttribute('role', 'status') })