import React from 'react'; import '@testing-library/jest-dom'; import { render, screen, fireEvent, act, waitFor, within } from '@testing-library/react'; import { PromptProvider, usePrompt } from './prompt'; import { I18nProvider } from './i18n/I18nProvider'; import { translations } from './i18n/translations'; afterEach(() => window.localStorage.removeItem('uiLanguage')); function LocaleDemo({ result, copy }: { result: (value: string | null) => void; copy?: { title: string; confirmLabel: string; cancelLabel: string } }) { const { prompt } = usePrompt(); return ; } test.each([ ['en', 'nb', true], ['nb', 'en', false], ] as const)('open prompt defaults follow %s to %s without replacing its Unicode draft, focus or resolver', async (from, to, accepted) => { localStorage.setItem('uiLanguage', from); const result = jest.fn(); render(); const trigger = screen.getByRole('button', { name: 'Open locale prompt' }); act(() => trigger.focus()); fireEvent.click(trigger); const dialog = await screen.findByRole('dialog', { name: translations[from].enterValue }); const input = screen.getByRole('textbox', { name: 'Literal spørsmål 東京?' }) as HTMLInputElement; await waitFor(() => expect(input).toHaveFocus()); const draft = 'Søknad 東京 🧑‍💻'; fireEvent.change(input, { target: { value: draft } }); input.setSelectionRange(2, 7); await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to }))); expect(screen.getByRole('dialog', { name: translations[to].enterValue })).toBe(dialog); expect(screen.getByRole('button', { name: translations[to].save })).toBeVisible(); expect(screen.getByRole('button', { name: translations[to].cancel })).toBeVisible(); expect(input).toHaveValue(draft); expect(input).toHaveFocus(); expect([input.selectionStart, input.selectionEnd]).toEqual([2, 7]); expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?'); expect(result).not.toHaveBeenCalled(); const action = screen.getByRole('button', { name: translations[to][accepted ? 'save' : 'cancel'] }); fireEvent.click(action); fireEvent.click(action); await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); expect(result).toHaveBeenCalledWith(accepted ? draft : null); await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); expect(trigger).toHaveFocus(); }); test.each([ { title: 'Literal title', confirmLabel: 'Literal save', cancelLabel: 'Literal cancel' }, { title: '', confirmLabel: '', cancelLabel: '' }, ])('locale changes preserve explicit prompt copy %j verbatim', async copy => { localStorage.setItem('uiLanguage', 'en'); const result = jest.fn(); render(); fireEvent.click(screen.getByRole('button', { name: 'Open locale prompt' })); const dialog = await screen.findByRole('dialog'); const input = screen.getByRole('textbox', { name: 'Literal spørsmål 東京?' }); fireEvent.change(input, { target: { value: 'Typed æøå 東京' } }); await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: 'nb' }))); expect(document.documentElement.lang).toBe('nb-NO'); expect(within(dialog).getByRole('heading').textContent).toBe(copy.title); const buttons = within(dialog).getAllByRole('button'); expect(buttons.map(button => button.textContent)).toEqual([copy.cancelLabel, copy.confirmLabel]); expect(input).toHaveValue('Typed æøå 東京'); expect(result).not.toHaveBeenCalled(); fireEvent.click(buttons[1]); await waitFor(() => expect(result).toHaveBeenCalledTimes(1)); expect(result).toHaveBeenCalledWith('Typed æøå 東京'); }); function Demo() { const { prompt } = usePrompt(); return ( ); } test('renders app-owned prompt dialog', async () => { render( , ); fireEvent.click(screen.getByRole('button', { name: /open prompt/i })); expect(await screen.findByText(/choose a new name/i)).toBeInTheDocument(); expect(screen.getByDisplayValue('resume.pdf')).toBeInTheDocument(); expect(screen.getByRole('textbox', { name: 'Choose a new name' })).toHaveAccessibleDescription('10/180'); expect(screen.getByRole('textbox', { name: 'Choose a new name' })).toHaveFocus(); expect(screen.getByRole('dialog', { name: 'Rename attachment' })).toHaveAccessibleDescription('Choose a new name'); expect(screen.getByRole('button', { name: /rename/i })).toBeInTheDocument(); }); test('shows prompt input helper length text', async () => { render( , ); fireEvent.click(screen.getByRole('button', { name: /open prompt/i })); expect(await screen.findByText(/10\/180/i)).toBeInTheDocument(); });