Skip to content

Commit

Permalink
feat(Checkbox): create component
Browse files Browse the repository at this point in the history
  • Loading branch information
spartDev committed Aug 8, 2018
1 parent cfbf48c commit d83b18c
Show file tree
Hide file tree
Showing 9 changed files with 1,506 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@babel/preset-react": "7.0.0-beta.56",
"@babel/register": "7.0.0-beta.56",
"@nivo/bar": "0.42.1",
"@sambego/storybook-state": "^1.0.7",
"@storybook/addon-actions": "3.4.10",
"@storybook/addon-info": "3.4.10",
"@storybook/addon-knobs": "3.4.10",
Expand Down
35 changes: 35 additions & 0 deletions src/CheckBox/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# CheckBox

### Usage

```jsx
import CheckBox from 'components/CheckBox';

// or

import { CheckBox } from 'components';
```

<!-- STORY -->

### Properties

- `checked` - Whether the button is checked or not.
- `disabled` - Whether the button is enabled or not.
- `onChange` - Callback whence clicked.

| propName | propType | defaultValue | isRequired |
| ---------- | :------: | :----------: | :--------: |
| `checked` | `bool` | `false` | - |
| `disabled` | `bool` | `true` | - |
| `onChange` | `func` | `() => {}` | - |

### Example

```jsx
import CheckBox from 'components/CheckBox';

render() {
return <CheckBox>Add sauce?</CheckBox>
}
```
32 changes: 32 additions & 0 deletions src/CheckBox/__stories__/CheckBox.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { storiesOf } from '@storybook/react';
import { withKnobs, boolean } from '@storybook/addon-knobs/react';
import { State, Store } from '@sambego/storybook-state';

import { CheckBox } from '../..';

const store = new Store({
checked: false,
});

storiesOf('CheckBox', module)
.addDecorator(withKnobs)
.add('default', () => {
const enabledValue = boolean('Enabled', true, 'state');

return (
<State store={store}>
<CheckBox disabled={!enabledValue}>A really cool choice</CheckBox>
</State>
);
})
.add('controlled', () => {
const checkedValue = boolean('Checked', false, 'state');
const enabledValue = boolean('Enabled', true, 'state');

return (
<CheckBox checked={checkedValue} disabled={!enabledValue} onChange={() => {}}>
What?
</CheckBox>
);
});
78 changes: 78 additions & 0 deletions src/CheckBox/__tests__/CheckBox.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import { mount } from 'enzyme';
import { ThemeProvider } from 'styled-components';
import 'jest-styled-components';

import CheckBox from '..';
import Theme from '../../Theme';

describe('<CheckBox />', () => {
it('should render without a problem', () => {
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" />
</ThemeProvider>,
);
expect(render).toMatchSnapshot();
});

it('should render without a problem when checked and enabled', () => {
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked />
</ThemeProvider>,
);
expect(render).toMatchSnapshot();
});

it('should render without a problem when unchecked and enabled', () => {
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked={false} />
</ThemeProvider>,
);
expect(render).toMatchSnapshot();
});

it('should render without a problem when checked and disabled', () => {
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked disabled />
</ThemeProvider>,
);
expect(render).toMatchSnapshot();
});

it('should render without a problem when unchecked and disabled', () => {
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked={false} disabled />
</ThemeProvider>,
);
expect(render).toMatchSnapshot();
});

it('should call change handler when enabled', () => {
const spy = jest.fn();
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked onChange={spy} />
</ThemeProvider>,
);
render.simulate('click');

expect(spy).toHaveBeenCalled();
});

it('should not call change handler when disabled', () => {
const spy = jest.fn();
const render = mount(
<ThemeProvider theme={Theme}>
<CheckBox id="test" checked disabled onChange={spy} />
</ThemeProvider>,
);
render.simulate('click');

expect(spy).not.toHaveBeenCalled();
});
});
Loading

0 comments on commit d83b18c

Please sign in to comment.