import { screen } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { createStatusRegistry } from '~/shared/utils/status-registry'; import { renderWithQueryClientAndRouter } from '~/unit-test-utils'; import { RegistryStatusIcon, RegistryStatusIconWithText, buildStatusFilterOptions, createStatusComponents, useStatusDisplay, } from '../StatusRegistryComponents'; // --------------------------------------------------------------------------- // Minimal test registry // --------------------------------------------------------------------------- type TestStatus = 'Active' | 'Done' | 'Unknown'; type TestResource = { state: string; detail?: string }; const testRegistry = createStatusRegistry()({ statuses: [ { status: 'Active', match: (obj) => obj.state === 'active', category: 'info', weight: 10, reason: (obj) => obj.detail, }, { status: 'Done', match: (obj) => obj.state === 'done', category: 'success', weight: 50, label: 'Completed', }, { status: 'Unknown', match: () => true, category: 'neutral', weight: 90 }, ], }); // --------------------------------------------------------------------------- // useStatusDisplay // --------------------------------------------------------------------------- describe('useStatusDisplay', () => { const TestHookConsumer = ({ resource }: { resource: TestResource | null }) => { const display = useStatusDisplay(testRegistry, resource); return (
{display.status ?? 'null'} {display.label} {display.category} {display.color} {display.message ?? 'none'}
); }; it('derives status from resource', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('status')).toHaveTextContent('Active'); expect(screen.getByTestId('label')).toHaveTextContent('Active'); expect(screen.getByTestId('category')).toHaveTextContent('info'); }); it('uses label override when present', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('label')).toHaveTextContent('Completed'); }); it('returns null status for null resource', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('status')).toHaveTextContent('null'); }); it('returns message from reason fn when present', () => { renderWithQueryClientAndRouter( , ); expect(screen.getByTestId('message')).toHaveTextContent('Waiting for pods'); }); it('returns undefined message when reason fn is not defined', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('message')).toHaveTextContent('none'); }); it('returns undefined message for null resource', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('message')).toHaveTextContent('none'); }); }); // --------------------------------------------------------------------------- // RegistryStatusIcon // --------------------------------------------------------------------------- describe('RegistryStatusIcon', () => { it('renders without crashing', () => { const { container } = renderWithQueryClientAndRouter( , ); expect(container.firstChild).toBeTruthy(); }); }); // --------------------------------------------------------------------------- // RegistryStatusIconWithText // --------------------------------------------------------------------------- describe('RegistryStatusIconWithText', () => { it('renders status label text', () => { renderWithQueryClientAndRouter( , ); expect(screen.getByText('Active')).toBeInTheDocument(); }); it('uses custom text when provided', () => { renderWithQueryClientAndRouter( , ); expect(screen.getByText('Custom Label')).toBeInTheDocument(); }); it('renders data-test attribute', () => { renderWithQueryClientAndRouter( , ); expect(screen.getByTestId('test-status')).toBeInTheDocument(); }); it('renders without Tooltip when tooltip prop is undefined', () => { const { container } = renderWithQueryClientAndRouter( , ); expect(container.querySelector('[id^="pf-tooltip"]')).not.toBeInTheDocument(); }); it('renders with Tooltip when tooltip prop is a non-empty string', async () => { const user = userEvent.setup(); renderWithQueryClientAndRouter( , ); const statusElement = screen.getByText('Active'); await user.hover(statusElement); expect(await screen.findByText('Waiting for resources')).toBeInTheDocument(); }); }); // --------------------------------------------------------------------------- // buildStatusFilterOptions // --------------------------------------------------------------------------- describe('buildStatusFilterOptions', () => { it('returns filter options for all statuses sorted by weight', () => { const options = buildStatusFilterOptions(testRegistry); expect(options).toHaveLength(3); expect(options[0].value).toBe('Active'); expect(options[1].value).toBe('Done'); expect(options[2].value).toBe('Unknown'); }); it('each option has label and value', () => { const options = buildStatusFilterOptions(testRegistry); for (const opt of options) { expect(opt.label).toBeTruthy(); expect(opt.value).toBeTruthy(); } }); it('uses label override for option label', () => { const options = buildStatusFilterOptions(testRegistry); const doneOpt = options.find((o) => o.value === 'Done'); expect(doneOpt?.label).toBe('Completed'); }); it('excludes specified statuses', () => { const options = buildStatusFilterOptions(testRegistry, new Set(['Unknown'] as TestStatus[])); expect(options).toHaveLength(2); expect(options.find((o) => o.value === 'Unknown')).toBeUndefined(); }); }); // --------------------------------------------------------------------------- // createStatusComponents (factory → namespace pattern) // --------------------------------------------------------------------------- describe('createStatusComponents', () => { const TestStatus = createStatusComponents(testRegistry); describe('StatusIconWithText', () => { it('renders status text without passing registry', () => { renderWithQueryClientAndRouter(); expect(screen.getByText('Active')).toBeInTheDocument(); }); it('uses label override', () => { renderWithQueryClientAndRouter(); expect(screen.getByText('Completed')).toBeInTheDocument(); }); it('accepts custom text', () => { renderWithQueryClientAndRouter(); expect(screen.getByText('Busy')).toBeInTheDocument(); }); }); describe('StatusIcon', () => { it('renders without crashing', () => { const { container } = renderWithQueryClientAndRouter( , ); expect(container.firstChild).toBeTruthy(); }); }); describe('useStatusDisplay', () => { const HookConsumer = ({ resource }: { resource: TestResource | null }) => { const display = TestStatus.useStatusDisplay(resource); return (
{display.status ?? 'null'} {display.label}
); }; it('derives status without passing registry', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('ns-status')).toHaveTextContent('Done'); expect(screen.getByTestId('ns-label')).toHaveTextContent('Completed'); }); it('returns null for null resource', () => { renderWithQueryClientAndRouter(); expect(screen.getByTestId('ns-status')).toHaveTextContent('null'); }); }); describe('filterOptions', () => { it('has options for all statuses', () => { expect(TestStatus.filterOptions).toHaveLength(3); }); it('each option has label and value', () => { for (const opt of TestStatus.filterOptions) { expect(opt.label).toBeTruthy(); expect(opt.value).toBeTruthy(); } }); }); describe('statusFilterFn', () => { it('filters by derived status', () => { expect(TestStatus.statusFilterFn({ state: 'active' }, ['Active'])).toBe(true); expect(TestStatus.statusFilterFn({ state: 'active' }, ['Done'])).toBe(false); }); }); });