// jshint esversion: 6
'use strict';

import React from 'react';
import PropTypes from 'prop-types';
import {mount} from '../../../build/enzyme';

import {CheckboxGroup} from './CheckboxGroup';
import {Checkbox} from './Checkbox';

describe('CheckboxGroup', () => {
    const cbGroup = props => {
        const {values, ...rest} = props;
        return (
            <CheckboxGroup name="group" value={values} {...rest}>
                {Object.keys(values).map(key => (
                    <Checkbox key={key} name={key} />
                ))}
            </CheckboxGroup>
        );
    };

    cbGroup.propTypes = {
        values: PropTypes.object.isRequired
    };

    describe('returns main checkbox value (based on children)', () => {
        it('true if all nested checkboxes have truthy value', () => {
            const values = {
                a: true,
                b: true
            };
            const wrapper = mount(cbGroup({values}));
            expect(wrapper.instance().getMainCheckboxValue()).toBe(true);
        });

        it('false if all nested checkboxes have falsy value', () => {
            const values = {
                a: false,
                b: false
            };
            const wrapper = mount(cbGroup({values}));
            expect(wrapper.instance().getMainCheckboxValue()).toBe(false);
        });

        it('null if nested checkboxes have both truthy and false value', () => {
            const values = {
                a: true,
                b: false
            };
            const wrapper = mount(cbGroup({values}));
            expect(wrapper.instance().getMainCheckboxValue()).toBe(null);
        });
    });
});
