From 2e452993ff5dbf0983a2d6cc2d95cf642a49951d Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 17 Mar 2019 18:09:10 +0100 Subject: [PATCH] Created Checkbox test --- src/utils/Checkbox.js | 9 ++--- test/utils/Checkbox.test.js | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 test/utils/Checkbox.test.js diff --git a/src/utils/Checkbox.js b/src/utils/Checkbox.js index c83d1a10..0c39e5e4 100644 --- a/src/utils/Checkbox.js +++ b/src/utils/Checkbox.js @@ -12,16 +12,11 @@ const propTypes = { const Checkbox = ({ checked, onChange, className, children }) => { const id = uuid(); + const onChecked = (e) => onChange(e.target.checked, e); return ( - onChange(e.target.checked, e)} - /> + ); diff --git a/test/utils/Checkbox.test.js b/test/utils/Checkbox.test.js new file mode 100644 index 00000000..9ee2ecee --- /dev/null +++ b/test/utils/Checkbox.test.js @@ -0,0 +1,67 @@ +import React from 'react'; +import { mount, shallow } from 'enzyme'; +import Checkbox from '../../src/utils/Checkbox'; +import DateInput from './DateInput.test'; + +describe('', () => { + let wrapped; + + const createComponent = (props = {}) => { + wrapped = mount(); + + return wrapped; + }; + + afterEach(() => wrapped && wrapped.unmount()); + + it('includes extra class names when provided', () => { + const classNames = [ 'foo', 'bar', 'baz' ]; + const checked = false; + const onChange = () => {}; + + expect.assertions(classNames.length); + classNames.forEach((className) => { + const wrapped = createComponent({ className, checked, onChange }); + + expect(wrapped.prop('className')).toContain(className); + }); + }); + + it('marks input as checked if defined', () => { + const checkeds = [ true, false ]; + const onChange = () => {}; + + expect.assertions(checkeds.length); + checkeds.forEach((checked) => { + const wrapped = createComponent({ checked, onChange }); + const input = wrapped.find('input'); + + expect(input.prop('checked')).toEqual(checked); + }); + }); + + it('renders provided children inside the label', () => { + const labels = [ 'foo', 'bar', 'baz' ]; + const checked = false; + const onChange = () => {}; + + expect.assertions(labels.length); + labels.forEach((children) => { + const wrapped = createComponent({ children, checked, onChange }); + const label = wrapped.find('label'); + + expect(label.text()).toEqual(children); + }); + }); + + it('changes checked status on input change', () => { + const onChange = jest.fn(); + const e = { target: { checked: false } }; + const wrapped = createComponent({ checked: true, onChange }); + const input = wrapped.find('input'); + + input.prop('onChange')(e); + + expect(onChange).toHaveBeenCalledWith(false, e); + }); +});