import * as React from 'react'; import { shallow, mount } from 'enzyme'; import ChoiceList from '../ChoiceList'; import RadioButton from '../../RadioButton'; import Checkbox from '../../Checkbox'; const theme = { choiceList: 'choiceList', titleHidden: 'titleHidden', choices: 'choices', title: 'title', }; describe('', () => { let choices: ({ label: string, value: string })[]; beforeEach(() => { choices = [ { label: 'One', value: 'one' }, { label: 'Two', value: 'two' }, { label: 'Three', value: 'three' }, ]; }); describe('when default props are provided', () => { it('should verify renders a fieldset', () => { const element = mount( ); expect(element.find('div').exists()).toBe(true); expect(element.find('div').at(0).hasClass('choiceList')).toBe(true); }); }); describe('title property', () => { describe('when not set', () => { it('should verify not renders a legend for the fieldset', () => { const element = mount( ); expect(element.prop('div')).toBeUndefined(); }); }); describe('when set', () => { it('should verify renders a legend for the fieldset', () => { const element = mount( ); expect(element.find('div.title')).toHaveLength(1); expect(element.find('div').at(1).hasClass('title')).toBe(true); expect(element.find('div').at(1).text()).toBe('My title'); }); }); }); describe('choices property', () => { it('shoild verify renders a choice with the label and value properties', () => { const choiceElements = mount( ).find(RadioButton); choiceElements.forEach((choiceElement, index) => { expect(choiceElement.prop('label')).toBe(choices[index].label); expect(choiceElement.prop('value')).toBe(choices[index].value); }); }); }); describe('selected property', () => { it('sets the appropriate choices to be selected', () => { const selectedIndexes = [0, 2]; const selected = selectedIndexes.map(index => choices[index].value); const choiceElements = mount( ).find(RadioButton); choiceElements.forEach((choiceElement, index) => { expect(choiceElement.prop('checked')).toBe(selectedIndexes.includes(index)); }); }); }); describe('name property', () => { it('provides a unique name when none is provided', () => { const choiceElements = shallow( ).find(RadioButton); let name: string; choiceElements.forEach((choiceElement) => { const choiceName = choiceElement.prop('componentName'); if (name == null) { name = choiceName; } else { expect(choiceName).toBe(name); } expect(typeof choiceName).toBe('string'); }); }); it('uses the same name for every choice', () => { const name = 'MyChoiceList'; const choiceElements = shallow( ).find(RadioButton); choiceElements.forEach((choiceElement) => { expect(choiceElement.prop('componentName')).toBe(name); }); }); it('postpends [] when multiple options are allowed', () => { const name = 'MyChoiceList'; const choiceElements = shallow( ).find(RadioButton); choiceElements.forEach((choiceElement) => { expect(choiceElement.prop('componentName')).toBe(`${name}[]`); }); }); }); describe('allowMultiple property', () => { it('renders a radio button for each option when allowMultiple is not true', () => { let element = mount( ); expect(element.find(RadioButton).length).toBe(choices.length); expect(element.find(Checkbox).exists()).toBe(false); element = mount( ); expect(element.find(RadioButton).length).toBe(choices.length); expect(element.find(Checkbox).exists()).toBe(false); }); it('renders a checkbox each option when allowMultiple is true', () => { const element = mount( ); expect(element.find(RadioButton).exists()).toBe(false); expect(element.find(Checkbox).length).toBe(choices.length); }); }); describe('titlehidden property', () => { describe('when not set', () => { it('should verify not renders a titlehidden for the choicelist', () => { const element = mount( ); expect(element.prop('titleHidden')).toBeUndefined(); expect(element.find('div').at(0).hasClass('titleHidden')).toBe(false); }); }); describe('when set as true', () => { it('should verify renders a titlehidden for the choicelist when set as true', () => { const element = mount( ); expect(element.prop('titleHidden')).toBe(true); expect(element.find('div').at(0).hasClass('titleHidden')).toBe(true); }); }); describe('when set as false', () => { it('should verify renders a titlehidden for the choicelist when set as false', () => { const element = mount( ); expect(element.prop('titleHidden')).toBe(false); expect(element.find('div').at(0).hasClass('titleHidden')).toBe(false); }); }); }); });