Commit 44e66936 by Sarah Fischmann

wrote several tests for checkbox; made some small edits in component

parent a5b7a4fb
...@@ -7,5 +7,8 @@ ...@@ -7,5 +7,8 @@
}, },
"settings": { "settings": {
"import/resolver": "webpack" "import/resolver": "webpack"
},
"env": {
"jest": true
} }
} }
/* eslint-disable no-console */
import React from 'react'; import React from 'react';
import { shallow, mount } from 'enzyme'; import { shallow, mount } from 'enzyme';
import CheckBox from '../src/CheckBox'; import CheckBox from '../src/CheckBox';
...@@ -10,8 +11,8 @@ describe('<CheckBox />', () => { ...@@ -10,8 +11,8 @@ describe('<CheckBox />', () => {
describedBy="this is a checkbox" describedBy="this is a checkbox"
label="check me out!" label="check me out!"
checked="false" checked="false"
/> />,
) );
expect(wrapper.find('[name="checkbox"]').exists()).toEqual(true); expect(wrapper.find('[name="checkbox"]').exists()).toEqual(true);
expect(wrapper.find('[type="checkbox"]').exists()).toEqual(true); expect(wrapper.find('[type="checkbox"]').exists()).toEqual(true);
...@@ -42,7 +43,7 @@ describe('<CheckBox />', () => { ...@@ -42,7 +43,7 @@ describe('<CheckBox />', () => {
expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false); expect(wrapper.find('[aria-checked=true]').exists()).toEqual(false);
}); });
it('check that callback function is triggered when clicked', ()=> { it('check that callback function is triggered when clicked', () => {
const wrapper = shallow( const wrapper = shallow(
<CheckBox <CheckBox
name="checkbox" name="checkbox"
...@@ -84,7 +85,7 @@ describe('<CheckBox />', () => { ...@@ -84,7 +85,7 @@ describe('<CheckBox />', () => {
describedBy="checkbox" describedBy="checkbox"
label="I am disabled" label="I am disabled"
checked="false" checked="false"
disabled={true} disabled
/>, />,
); );
......
import React from 'react'; import React from 'react';
import { inputProps } from './utils/asInput'; import { inputProps } from './utils/asInput';
import newId from './utils/newId';
class CheckBox extends React.Component { class CheckBox extends React.Component {
...@@ -12,12 +13,15 @@ class CheckBox extends React.Component { ...@@ -12,12 +13,15 @@ class CheckBox extends React.Component {
this.onChange = this.props.onChange.bind(this); this.onChange = this.props.onChange.bind(this);
} }
const id = newId('checkbox');
if (props.checked === 'true') { if (props.checked === 'true') {
this.state = { this.state = {
id,
checked: true, checked: true,
}; };
} else { } else {
this.state = { this.state = {
id,
checked: false, checked: false,
}; };
} }
...@@ -33,7 +37,7 @@ class CheckBox extends React.Component { ...@@ -33,7 +37,7 @@ class CheckBox extends React.Component {
checked: true, checked: true,
}); });
} }
if(this.onChange) { if (this.onChange) {
this.onChange(); this.onChange();
} }
} }
...@@ -43,8 +47,9 @@ class CheckBox extends React.Component { ...@@ -43,8 +47,9 @@ class CheckBox extends React.Component {
const props = { ...this.props }; const props = { ...this.props };
return ( return (
<label> <label htmlFor={this.state.id}>
<input <input
id={this.state.id}
name={props.name} name={props.name}
type="checkbox" type="checkbox"
defaultChecked={this.state.checked} defaultChecked={this.state.checked}
......
/* eslint-disable import/no-extraneous-dependencies */ /* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */
import React from 'react'; import React from 'react';
import { storiesOf, linkTo } from '@kadira/storybook'; import { storiesOf, linkTo } from '@kadira/storybook';
...@@ -141,7 +142,7 @@ storiesOf('CheckBox', module) ...@@ -141,7 +142,7 @@ storiesOf('CheckBox', module)
describedBy="checkbox" describedBy="checkbox"
label="you cannot check me out" label="you cannot check me out"
checked="false" checked="false"
disabled="true" disabled
/> />
)) ))
.add('default checked', () => ( .add('default checked', () => (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment