mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
72a82b3c44
214 items came out of writing the tests. Real defects are corrected, dead code is removed rather than covered, and the tests that pinned the old behaviour move with them. Also drops two agent coverage scratch files that slipped into the coverage PR and adds the ignore rule that keeps them out.
104 lines
3.8 KiB
TypeScript
104 lines
3.8 KiB
TypeScript
import { render, screen, fireEvent } from '../../../tests/helpers/render';
|
|
import userEvent from '@testing-library/user-event';
|
|
import ConfirmDialog from './ConfirmDialog';
|
|
|
|
describe('ConfirmDialog', () => {
|
|
const onClose = vi.fn();
|
|
const onConfirm = vi.fn();
|
|
|
|
beforeEach(() => {
|
|
onClose.mockClear();
|
|
onConfirm.mockClear();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-001: does not render when isOpen is false', () => {
|
|
render(
|
|
<ConfirmDialog isOpen={false} onClose={onClose} onConfirm={onConfirm} message="Are you sure?" />
|
|
);
|
|
expect(screen.queryByText('Are you sure?')).toBeNull();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-002: renders with default title "Confirm" and message', () => {
|
|
render(
|
|
<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={onConfirm} message="Are you sure?" />
|
|
);
|
|
expect(screen.getByText('Confirm')).toBeTruthy();
|
|
expect(screen.getByText('Are you sure?')).toBeTruthy();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-003: renders custom title and message', () => {
|
|
render(
|
|
<ConfirmDialog
|
|
isOpen={true}
|
|
onClose={onClose}
|
|
onConfirm={onConfirm}
|
|
title="Remove item"
|
|
message="This cannot be undone."
|
|
/>
|
|
);
|
|
expect(screen.getByText('Remove item')).toBeTruthy();
|
|
expect(screen.getByText('This cannot be undone.')).toBeTruthy();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-004: Cancel button calls onClose', async () => {
|
|
const user = userEvent.setup();
|
|
render(<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={onConfirm} />);
|
|
await user.click(screen.getByRole('button', { name: /cancel/i }));
|
|
expect(onClose).toHaveBeenCalledOnce();
|
|
expect(onConfirm).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-005: Confirm button calls onConfirm and onClose', async () => {
|
|
const user = userEvent.setup();
|
|
render(<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={onConfirm} />);
|
|
await user.click(screen.getByRole('button', { name: /delete/i }));
|
|
expect(onConfirm).toHaveBeenCalledOnce();
|
|
expect(onClose).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-006: custom button labels render correctly', () => {
|
|
render(
|
|
<ConfirmDialog
|
|
isOpen={true}
|
|
onClose={onClose}
|
|
onConfirm={onConfirm}
|
|
confirmLabel="Yes, remove"
|
|
cancelLabel="Go back"
|
|
/>
|
|
);
|
|
expect(screen.getByRole('button', { name: 'Yes, remove' })).toBeTruthy();
|
|
expect(screen.getByRole('button', { name: 'Go back' })).toBeTruthy();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-007: Escape key calls onClose', () => {
|
|
render(<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={onConfirm} />);
|
|
fireEvent.keyDown(document, { key: 'Escape' });
|
|
expect(onClose).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-009: a rejecting async onConfirm does not escape as an unhandled rejection', async () => {
|
|
const unhandled = vi.fn();
|
|
process.on('unhandledRejection', unhandled);
|
|
const failing = vi.fn(() => Promise.reject(new Error('delete failed')));
|
|
|
|
render(<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={failing} />);
|
|
fireEvent.click(screen.getByRole('button', { name: /delete/i }));
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
|
|
expect(failing).toHaveBeenCalledOnce();
|
|
expect(onClose).toHaveBeenCalledOnce();
|
|
expect(unhandled).not.toHaveBeenCalled();
|
|
process.off('unhandledRejection', unhandled);
|
|
});
|
|
|
|
it('FE-COMP-CONFIRM-008: clicking backdrop calls onClose', async () => {
|
|
const user = userEvent.setup();
|
|
render(<ConfirmDialog isOpen={true} onClose={onClose} onConfirm={onConfirm} message="msg" />);
|
|
// The outermost fixed div is the backdrop — click outside the card
|
|
const backdrop = document.querySelector('.fixed') as HTMLElement;
|
|
// fireEvent click on the backdrop element directly
|
|
fireEvent.click(backdrop);
|
|
expect(onClose).toHaveBeenCalledOnce();
|
|
});
|
|
});
|