Skip to content

Commit

Permalink
feat(Toggle): support onChange event (carbon-design-system#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
asudoh authored and marijohannessen committed Jan 23, 2018
1 parent 7782b84 commit 6d2ddd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/components/Toggle/Toggle-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ describe('Toggle', () => {
});

describe('events', () => {
it('passes along onChange to <input>', () => {
const onChange = jest.fn();
const id = 'test-input';
const wrapper = mount(<Toggle id={id} onChange={onChange} />);

const input = wrapper.find('input');
const inputElement = input.instance();

inputElement.checked = true;
wrapper.find('input').simulate('change');

expect(
onChange.mock.calls.map(call =>
call.map((arg, i) => (i > 0 ? arg : arg.target))
)
).toEqual([[inputElement]]);
});

it('should invoke onToggle with expected arguments', () => {
const onToggle = jest.fn();
const id = 'test-input';
Expand Down
2 changes: 2 additions & 0 deletions src/components/Toggle/Toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Toggle = ({
className,
defaultToggled,
toggled,
onChange,
onToggle,
id,
labelA,
Expand Down Expand Up @@ -35,6 +36,7 @@ const Toggle = ({
id={id}
className="bx--toggle"
onChange={evt => {
onChange && onChange(evt);
onToggle(input.checked, id, evt);
}}
ref={el => {
Expand Down

0 comments on commit 6d2ddd9

Please sign in to comment.