"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importDefault(require("react"));
const faker_1 = __importDefault(require("faker"));
const test_utilities_1 = require("../../test-utilities");
const InlineError_1 = require("../InlineError");
const Checkbox_1 = require("./Checkbox");
const defaultProps = {
    children: 'Save this information for next time',
};
describe('<Checkbox />', () => {
    it('renders an un-checked checkbox and label', () => {
        const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps}/>);
        expect(checkbox).toContainReactComponent('input', {
            checked: false,
            type: 'checkbox',
        });
        expect(checkbox.find('label')).toContainReactText('Save this information for next time');
    });
    it('renders a checked checkbox when the checked prop is provided and set to true', () => {
        const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} checked/>);
        expect(checkbox).toContainReactComponent('input', {
            checked: true,
            type: 'checkbox',
        });
    });
    it('renders a checked checkbox when the value prop is provided and set to true', () => {
        const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} value/>);
        expect(checkbox).toContainReactComponent('input', {
            checked: true,
            type: 'checkbox',
        });
    });
    it('renders a disabled checkbox when the disabled prop is provided and set to true', () => {
        const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} disabled/>);
        expect(checkbox).toContainReactComponent('input', {
            disabled: true,
            type: 'checkbox',
        });
    });
    it('calls the onChange callback when the checkbox is checked and unchecked', () => {
        const onChangeSpy = jest.fn();
        const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} onChange={onChangeSpy}/>);
        checkbox.find('input').trigger('onChange', {
            currentTarget: { checked: true },
        });
        expect(onChangeSpy).toHaveBeenCalledWith(true);
        checkbox.find('input').trigger('onChange', {
            currentTarget: { checked: false },
        });
        expect(onChangeSpy).toHaveBeenCalledWith(false);
    });
    describe('error', () => {
        it('renders an error message when the error prop is provided', () => {
            const error = 'Error message';
            const id = faker_1.default.random.alphaNumeric();
            const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} id={id} error={error}/>);
            expect(checkbox).toContainReactComponent(InlineError_1.InlineError, {
                controlID: id,
                children: error,
            });
        });
        it('creates an ID automatically that associates an error with the input', () => {
            var _a, _b;
            const error = 'Error message';
            const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} error={error}/>);
            const id = (_a = checkbox.find('input')) === null || _a === void 0 ? void 0 : _a.prop('id');
            expect(id).not.toBeUndefined();
            expect(checkbox).toContainReactComponent(InlineError_1.InlineError, {
                controlID: (_b = checkbox.find('input')) === null || _b === void 0 ? void 0 : _b.prop('id'),
                children: error,
            });
        });
    });
    describe('accessibilityLabel', () => {
        it('adds an aria-label to the label when the accessibilityLabel is set', () => {
            const accessibilityLabelContent = 'Accessibility content';
            const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps} accessibilityLabel={accessibilityLabelContent}/>);
            expect(checkbox).toContainReactComponent('label', {
                'aria-label': accessibilityLabelContent,
            });
        });
        it('does not add aria-label to the label when accessibilityLabel is not set', () => {
            const accessibilityLabelContent = 'Accessibility content';
            const checkbox = test_utilities_1.mountWithContext(<Checkbox_1.Checkbox {...defaultProps}>Default content</Checkbox_1.Checkbox>);
            expect(checkbox).not.toContainReactComponent('label', {
                'aria-label': accessibilityLabelContent,
            });
        });
    });
});
