Skip to content

Commit

Permalink
[RadioGroup] Add tests for edge cases (#6409)
Browse files Browse the repository at this point in the history
* [RadioGroup] Add tests for edge cases

- Focus with no radios
- Internal radios should fire onChange
- on mount register internal radios to this.radios

* [RadioGroup] Fix linting errors
  • Loading branch information
agamrafaeli authored and oliviertassinari committed Mar 25, 2017
1 parent 444c603 commit f1ca1a9
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/Radio/RadioGroup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import React from 'react';
import { assert } from 'chai';
import { spy } from 'sinon';
import { createShallow } from 'src/test-utils';
import { createShallow, createMount } from 'src/test-utils';
import RadioGroup from './RadioGroup';
import Radio from './Radio';

describe('<RadioGroup />', () => {
let shallow;
Expand Down Expand Up @@ -112,5 +113,65 @@ describe('<RadioGroup />', () => {
assert.strictEqual(radios[2].focus.callCount, 0);
assert.strictEqual(radios[3].focus.callCount, 1);
});

it('should be able to focus with no radios', () => {
wrapper.instance().radios = [];
wrapper.instance().focus();
});
});

describe('children radios fire change event', () => {
let wrapper;

beforeEach(() => {
wrapper = shallow(
<RadioGroup>
<Radio />
</RadioGroup>,
);
});

it('should fire onChange', () => {
const internalRadio = wrapper.children().first();
const event = { target: { value: 'woof' } };
const onChangeSpy = spy();
wrapper.setProps({ onChange: onChangeSpy });

internalRadio.simulate('change', event, true);
assert.strictEqual(onChangeSpy.callCount, 1);
assert.strictEqual(onChangeSpy.calledWith(event), true);
});

it('should not fire onChange if not checked', () => {
const internalRadio = wrapper.children().first();
const onChangeSpy = spy();
wrapper.setProps({ onChange: onChangeSpy });
internalRadio.simulate('change', { target: { value: 'woof' } }, false);
assert.strictEqual(onChangeSpy.callCount, 0);
});
});

describe('register internal radios to this.radio', () => {
let mount;

before(() => {
mount = createMount();
});

it('should add a child', () => {
const wrapper = mount(
<RadioGroup>
<Radio />
</RadioGroup>,
);
assert.strictEqual(wrapper.instance().radios.length, 1);
});

it('should keep radios empty', () => {
const wrapper = mount(
<RadioGroup />,
);
assert.strictEqual(wrapper.instance().radios.length, 0);
});
});
});

0 comments on commit f1ca1a9

Please sign in to comment.