Files
jobtrackingapp/job-tracker-ui/src/confirm.test.tsx
T

143 lines
6.5 KiB
TypeScript

import React from 'react';
import '@testing-library/jest-dom';
import { render, screen, fireEvent, act, waitFor, within } from '@testing-library/react';
import { ConfirmProvider, useConfirm } from './confirm';
import { I18nProvider } from './i18n/I18nProvider';
import { translations } from './i18n/translations';
afterEach(() => window.localStorage.removeItem('uiLanguage'));
function Demo() {
const { confirm } = useConfirm();
return (
<button onClick={() => void confirm({ title: 'Delete job', message: 'Are you sure?', confirmLabel: 'Delete', destructive: true })}>
Open confirm
</button>
);
}
function DefaultDemo() {
const { confirm } = useConfirm();
return <button onClick={() => void confirm({ message: 'Fortsett?' })}>Open localized confirm</button>;
}
function LocaleDemo({ result, copy }: { result: (value: boolean) => void; copy?: { title: string; confirmLabel: string; cancelLabel: string } }) {
const { confirm } = useConfirm();
return <button onClick={() => void confirm({ message: 'Literal spørsmål 東京?', ...copy }).then(result)}>Open locale confirm</button>;
}
test.each([
['en', 'nb', true], ['nb', 'en', false],
] as const)('open confirm defaults follow %s to %s without replacing focus or resolver', async (from, to, accepted) => {
localStorage.setItem('uiLanguage', from);
const result = jest.fn();
render(<I18nProvider><ConfirmProvider><LocaleDemo result={result} /></ConfirmProvider></I18nProvider>);
const trigger = screen.getByRole('button', { name: 'Open locale confirm' });
act(() => trigger.focus());
fireEvent.click(trigger);
const dialog = await screen.findByRole('dialog', { name: translations[from].confirmAction });
expect(screen.getByRole('button', { name: translations[from].cancel })).toHaveFocus();
const message = screen.getByRole('region', { name: translations[from].confirmAction });
act(() => message.focus());
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
expect(screen.getByRole('dialog', { name: translations[to].confirmAction })).toBe(dialog);
expect(screen.getByRole('button', { name: translations[to].confirm })).toBeVisible();
expect(screen.getByRole('button', { name: translations[to].cancel })).toBeVisible();
expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?');
expect(message).toHaveFocus();
expect(result).not.toHaveBeenCalled();
const action = screen.getByRole('button', { name: translations[to][accepted ? 'confirm' : 'cancel'] });
fireEvent.click(action); fireEvent.click(action);
await waitFor(() => expect(result).toHaveBeenCalledTimes(1));
expect(result).toHaveBeenCalledWith(accepted);
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
expect(trigger).toHaveFocus();
});
test.each([
{ title: 'Literal title', confirmLabel: 'Literal accept', cancelLabel: 'Literal cancel' },
{ title: '', confirmLabel: '', cancelLabel: '' },
])('locale changes preserve explicit confirm copy %j verbatim', async copy => {
localStorage.setItem('uiLanguage', 'en');
const result = jest.fn();
render(<I18nProvider><ConfirmProvider><LocaleDemo result={result} copy={copy} /></ConfirmProvider></I18nProvider>);
fireEvent.click(screen.getByRole('button', { name: 'Open locale confirm' }));
const dialog = await screen.findByRole('dialog');
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: 'nb' })));
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(within(dialog).getByText(translations.nb.confirmStandardHint)).toBeVisible();
expect(dialog).toHaveAccessibleDescription('Literal spørsmål 東京?');
expect(result).not.toHaveBeenCalled();
fireEvent.click(buttons[1]);
await waitFor(() => expect(result).toHaveBeenCalledTimes(1));
expect(result).toHaveBeenCalledWith(true);
});
test('renders app-owned confirmation dialog', async () => {
render(
<ConfirmProvider>
<Demo />
</ConfirmProvider>,
);
fireEvent.click(screen.getByRole('button', { name: /open confirm/i }));
expect(await screen.findByText(/are you sure/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /delete/i })).toBeInTheDocument();
expect(screen.getByRole('dialog', { name: 'Delete job' })).toHaveAccessibleDescription('Are you sure?');
expect(screen.getByRole('button', { name: 'Cancel' })).toHaveFocus();
const messageRegion = screen.getByRole('region', { name: 'Delete job' });
expect(messageRegion).toHaveAttribute('tabindex', '0');
messageRegion.focus();
expect(messageRegion).toHaveFocus();
});
test('uses the selected language for shared dialog defaults', async () => {
window.localStorage.setItem('uiLanguage', 'nb');
render(
<I18nProvider>
<ConfirmProvider>
<DefaultDemo />
</ConfirmProvider>
</I18nProvider>,
);
fireEvent.click(screen.getByRole('button', { name: /open localized confirm/i }));
expect(await screen.findByRole('heading', { name: 'Bekreft handling' })).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Bekreft' })).toBeInTheDocument();
window.localStorage.removeItem('uiLanguage');
});
test('finishing the opening transition preserves focus already moved inside the dialog', async () => {
jest.useFakeTimers();
try {
render(<ConfirmProvider><Demo /></ConfirmProvider>);
fireEvent.click(screen.getByRole('button', { name: /open confirm/i }));
expect(screen.getByRole('button', { name: 'Cancel' })).toHaveFocus();
const message = screen.getByRole('region', { name: 'Delete job' });
act(() => message.focus());
await act(async () => { jest.advanceTimersByTime(1000); });
expect(message).toHaveFocus();
} finally {
jest.useRealTimers();
}
});
test('finishing the opening transition recovers Cancel from the nested-modal container fallback', async () => {
jest.useFakeTimers();
try {
render(<ConfirmProvider><Demo /></ConfirmProvider>);
fireEvent.click(screen.getByRole('button', { name: /open confirm/i }));
const container = screen.getByRole('dialog', { name: 'Delete job' }).parentElement!;
expect(container).toHaveAttribute('role', 'presentation');
act(() => container.focus());
expect(container).toHaveFocus();
await act(async () => { jest.advanceTimersByTime(1000); });
expect(screen.getByRole('button', { name: 'Cancel' })).toHaveFocus();
} finally {
jest.useRealTimers();
}
});