"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * Tests for the AccessibleCheckbox component
 */
const react_1 = __importDefault(require("react"));
const react_2 = require("@testing-library/react");
const AccessibilitySystem_1 = require("../accessibility/AccessibilitySystem");
const AccessibleCheckbox_1 = require("../AccessibleCheckbox");
// Mock the accessibility system
jest.mock('../accessibility/AccessibilitySystem', () => ({
    ...jest.requireActual('../accessibility/AccessibilitySystem'),
    useAccessibility: () => ({
        analyzeComponent: jest.fn().mockResolvedValue({
            issues: [],
            score: 100,
        }),
    }),
}));
describe('AccessibleCheckbox', () => {
    const renderCheckbox = (props = {}) => {
        return (0, react_2.render)((0, jsx_runtime_1.jsx)(AccessibilitySystem_1.AccessibilityProvider, { children: (0, jsx_runtime_1.jsx)(AccessibleCheckbox_1.AccessibleCheckbox, { label: "Test Checkbox", ...props }) }));
    };
    it('renders with basic props', () => {
        renderCheckbox();
        expect(react_2.screen.getByLabelText('Test Checkbox')).toBeInTheDocument();
    });
    it('handles checked state', () => {
        renderCheckbox({ defaultChecked: true });
        expect(react_2.screen.getByLabelText('Test Checkbox')).toBeChecked();
    });
    it('handles controlled checked state', () => {
        const handleChange = jest.fn();
        renderCheckbox({ checked: true, onChange: handleChange });
        expect(react_2.screen.getByLabelText('Test Checkbox')).toBeChecked();
    });
    it('handles indeterminate state', () => {
        renderCheckbox({ indeterminate: true });
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        expect(checkbox.indeterminate).toBe(true);
    });
    it('handles required state', () => {
        renderCheckbox({ required: true });
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        expect(checkbox).toBeRequired();
        expect(react_2.screen.getByText('*')).toBeInTheDocument();
    });
    it('displays helper text', () => {
        renderCheckbox({ helperText: 'Helper text' });
        expect(react_2.screen.getByText('Helper text')).toBeInTheDocument();
    });
    it('displays error message', () => {
        renderCheckbox({ error: 'Error message' });
        expect(react_2.screen.getByText('Error message')).toBeInTheDocument();
        expect(react_2.screen.getByLabelText('Test Checkbox')).toHaveAttribute('aria-invalid', 'true');
    });
    it('handles validation', () => {
        const validate = jest.fn().mockReturnValue('Validation error');
        renderCheckbox({ validate });
        react_2.fireEvent.click(react_2.screen.getByLabelText('Test Checkbox'));
        expect(validate).toHaveBeenCalled();
        expect(react_2.screen.getByText('Validation error')).toBeInTheDocument();
    });
    it('handles loading state', () => {
        renderCheckbox({ isLoading: true });
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        expect(checkbox).toBeDisabled();
        expect(checkbox).toHaveAttribute('aria-busy', 'true');
    });
    it('handles disabled state', () => {
        renderCheckbox({ disabled: true });
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        expect(checkbox).toBeDisabled();
    });
    it('handles onChange event', () => {
        const handleChange = jest.fn();
        renderCheckbox({ onChange: handleChange });
        react_2.fireEvent.click(react_2.screen.getByLabelText('Test Checkbox'));
        expect(handleChange).toHaveBeenCalledWith(true, expect.any(Object));
    });
    it('applies custom class names', () => {
        renderCheckbox({
            wrapperClassName: 'custom-wrapper',
            inputClassName: 'custom-input',
            labelClassName: 'custom-label',
            helperTextClassName: 'custom-helper',
            errorClassName: 'custom-error',
            containerClassName: 'custom-container',
        });
        const wrapper = react_2.screen.getByLabelText('Test Checkbox').closest('.custom-wrapper');
        const input = react_2.screen.getByLabelText('Test Checkbox');
        const label = react_2.screen.getByText('Test Checkbox');
        const container = react_2.screen.getByLabelText('Test Checkbox').closest('.custom-container');
        expect(wrapper).toHaveClass('custom-wrapper');
        expect(input).toHaveClass('custom-input');
        expect(label).toHaveClass('custom-label');
        expect(container).toHaveClass('custom-container');
    });
    it('forwards ref', () => {
        const ref = react_1.default.createRef();
        renderCheckbox({ ref });
        expect(ref.current).toBeInstanceOf(HTMLInputElement);
    });
    it('displays accessibility issues', async () => {
        const mockIssues = [
            { message: 'Issue 1' },
            { message: 'Issue 2' },
        ];
        jest.spyOn(require('../accessibility/AccessibilitySystem'), 'useAccessibility')
            .mockImplementation(() => ({
            analyzeComponent: jest.fn().mockResolvedValue({
                issues: mockIssues,
                score: 80,
            }),
        }));
        renderCheckbox({ analyzeOnMount: true });
        // Wait for issues to be displayed
        const issues = await react_2.screen.findAllByText(/Issue \d/);
        expect(issues).toHaveLength(2);
    });
    it('handles keyboard navigation', () => {
        renderCheckbox();
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        // Focus the checkbox
        checkbox.focus();
        expect(checkbox).toHaveFocus();
        // Toggle with space key
        react_2.fireEvent.keyDown(checkbox, { key: ' ' });
        expect(checkbox).toBeChecked();
        // Toggle with enter key
        react_2.fireEvent.keyDown(checkbox, { key: 'Enter' });
        expect(checkbox).not.toBeChecked();
    });
    it('maintains focus styles', () => {
        renderCheckbox();
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        // Focus the checkbox
        checkbox.focus();
        expect(checkbox).toHaveFocus();
        expect(checkbox).toHaveClass('focus:ring-2');
    });
    it('handles dark mode styles', () => {
        renderCheckbox();
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        const label = react_2.screen.getByText('Test Checkbox');
        expect(checkbox).toHaveClass('dark:border-gray-600');
        expect(label).toHaveClass('dark:text-gray-200');
    });
    it('provides proper ARIA attributes', () => {
        renderCheckbox({
            helperText: 'Helper text',
            error: 'Error message',
            'aria-describedby': 'custom-desc',
        });
        const checkbox = react_2.screen.getByLabelText('Test Checkbox');
        expect(checkbox).toHaveAttribute('aria-describedby', expect.stringContaining('custom-desc'));
        expect(checkbox).toHaveAttribute('aria-invalid', 'true');
    });
    it('handles controlled and uncontrolled states correctly', () => {
        const { rerender } = renderCheckbox({ defaultChecked: true });
        expect(react_2.screen.getByLabelText('Test Checkbox')).toBeChecked();
        // Switch to controlled
        rerender((0, jsx_runtime_1.jsx)(AccessibilitySystem_1.AccessibilityProvider, { children: (0, jsx_runtime_1.jsx)(AccessibleCheckbox_1.AccessibleCheckbox, { label: "Test Checkbox", checked: false, onChange: () => { } }) }));
        expect(react_2.screen.getByLabelText('Test Checkbox')).not.toBeChecked();
        // Update controlled value
        rerender((0, jsx_runtime_1.jsx)(AccessibilitySystem_1.AccessibilityProvider, { children: (0, jsx_runtime_1.jsx)(AccessibleCheckbox_1.AccessibleCheckbox, { label: "Test Checkbox", checked: true, onChange: () => { } }) }));
        expect(react_2.screen.getByLabelText('Test Checkbox')).toBeChecked();
    });
});
//# sourceMappingURL=AccessibleCheckbox.test.js.map