Israel Lavi | 1994c98 | 2018-05-21 17:42:00 +0300 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import RadioGroup from '../../src/react/RadioGroup.js'; |
| 3 | |
| 4 | import renderer from 'react-test-renderer'; |
| 5 | import {mount} from 'enzyme'; |
| 6 | |
| 7 | class RadioGroupForm extends React.Component { |
| 8 | constructor(props) { |
| 9 | super(props); |
| 10 | this.state = {value: undefined}; |
| 11 | this.handleChange = this.handleChange.bind(this); |
| 12 | } |
| 13 | |
| 14 | handleChange(val) { |
| 15 | this.setState({value: val}); |
| 16 | } |
| 17 | |
| 18 | getValue() { |
| 19 | return this.grp.getValue(); |
| 20 | } |
| 21 | |
| 22 | render() { |
| 23 | return ( |
| 24 | <form > |
| 25 | <RadioGroup name='grp1' title='Group A' value={this.state.value} ref={(grp) => { this.grp = grp;}} onChange={this.handleChange} data-test-id='grp1' |
| 26 | options={[{value: '1', label: 'option 1'}, {value: '2', label: 'option 2'}]} /> |
| 27 | </form> |
| 28 | ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | describe('RadioGroup', () => { |
| 33 | test('RadioGroup - basic rendering', () => { |
| 34 | const radio = renderer.create(<RadioGroup name='grp1' defaultValue='2' value='1' title='Group A' |
| 35 | onChange={()=>{}} data-test-id='grp1' |
| 36 | options={[{value: '1', label: 'option 1'}, {value: '2', label: 'option 2'}]} />).toJSON(); |
| 37 | expect(radio).toMatchSnapshot(); |
| 38 | }); |
| 39 | |
| 40 | test('RadioGroup - value overrides default value', () => { |
| 41 | const radio = mount(<RadioGroup name='grp1' defaultValue='2' value='1' title='Group A' |
| 42 | onChange={()=>{}} data-test-id='grp1' |
| 43 | options={[{value: '1', label: 'option 1'}, {value: '2', label: 'option 2'}]} />); |
| 44 | expect(radio.instance().getValue()).toEqual('1'); |
| 45 | }); |
| 46 | |
| 47 | test('RadioGroup - can have no value', () => { |
| 48 | const radio = mount(<RadioGroup name='grp1' title='Group A' |
| 49 | onChange={()=>{}} data-test-id='grp1' |
| 50 | options={[{value: '1', label: 'option 1'}, {value: '2', label: 'option 2'}]} />); |
| 51 | expect(radio.instance().getValue()).toEqual(undefined); |
| 52 | }); |
| 53 | |
| 54 | test('RadioGroup - can be rendered without title', () => { |
| 55 | const radio = mount(<RadioGroup name='grp1' |
| 56 | onChange={()=>{}} data-test-id='grp1' |
| 57 | options={[{value: '1', label: 'option 1'}, {value: '2', label: 'option 2'}]} />); |
| 58 | expect(radio.find('.sdc-radio-group__legend').length).toEqual(0); |
| 59 | }); |
| 60 | |
| 61 | test('RadioGroup - value changes', () => { |
| 62 | const radio = mount(<RadioGroupForm />); |
| 63 | expect(radio.instance().getValue()).toEqual(undefined); |
| 64 | radio.find('input[value="1"]').simulate('change', { target : { checked: true }}); |
| 65 | expect(radio.instance().getValue()).toEqual('1'); |
| 66 | radio.find('input[value="2"]').simulate('change', { target : { checked: true }}); |
| 67 | expect(radio.instance().getValue()).toEqual('2'); |
| 68 | }); |
| 69 | }); |