Skip to content

Commit

Permalink
fix: handle checkboxgroup children type
Browse files Browse the repository at this point in the history
  • Loading branch information
devharris7 committed Apr 21, 2021
1 parent 17634c1 commit 1634d1d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/components/CheckboxGroup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const CheckboxGroup = ({ id, className, dts, children, value, name, inline, onCh

const renderChildren = () =>
React.Children.map(children, child => {
if (!child) return null;

if (child.type === Checkbox) {
const childProps = {
...child.props,
Expand All @@ -34,11 +36,11 @@ const CheckboxGroup = ({ id, className, dts, children, value, name, inline, onCh
});
const classNames = classnames(['checkbox-group-component', className]);

return (
return children ? (
<div data-testid="checkbox-group-wrapper" id={id} className={classNames} {...expandDts(dts)}>
{renderChildren()}
</div>
);
) : null;
};

CheckboxGroup.propTypes = {
Expand Down
20 changes: 18 additions & 2 deletions src/components/CheckboxGroup/index.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import React from 'react';
import { render, cleanup, fireEvent } from '@testing-library/react';

import Checkbox from '../Checkbox';

import CheckboxGroup from '.';

afterEach(cleanup);
Expand Down Expand Up @@ -60,4 +58,22 @@ describe('<CheckboxGroup />', () => {
);
expect(console.error).toHaveBeenCalledWith("ERROR: CheckboxGroup's children should be an array of Checkbox");
});

it('should return null if there is no children', () => {
const { queryAllByTestId } = render(<CheckboxGroup name="movies" value={['test']} onChange={jest.fn()} />);
expect(queryAllByTestId('checkbox-group-wrapper')).toHaveLength(0);
});

it('should return null if there is a boolean child', () => {
console.error = jest.fn();
const { queryAllByTestId } = render(
<CheckboxGroup name="movies" value={['test']} onChange={jest.fn()}>
{false && <Checkbox label="The Terminator" value="terminator" />}
<Checkbox label="Predator" value="predator" />
</CheckboxGroup>
);

expect(queryAllByTestId('checkbox-group-wrapper')).toHaveLength(1);
expect(console.error).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 1634d1d

Please sign in to comment.