This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function statelessRadio(React) { | |
return function StatelessRadio({ baseId, defaultValue, titleText, inputs = [], onSelection = () => {}} = {}) { | |
function handleSelectionChange(event) { | |
onSelection(event.target.value); | |
} | |
return ( | |
<div id={baseId}> | |
<p id={`${baseId}__title`}>{titleText}</p> | |
{inputs.map((inputItem) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
import { spy } from 'sinon'; | |
// ... | |
it('should take an onSelection callback prop', () => { | |
// "Arrange" | |
const props = { | |
inputs: [{}], | |
onSelection: spy() | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return function StatelessRadio({ baseId, defaultValue, titleText, inputs = [] } = {}) { | |
// ... | |
{inputs.map((inputItem) => { | |
const defaultChecked = (defaultValue === inputItem.value); | |
return ( | |
<div> | |
<input type="radio" name={baseId} value={inputItem.value} defaultChecked={defaultChecked} /> | |
<label>{inputItem.label}</label> | |
</div> | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('should check a default input by using the defaultValue prop', () => { | |
const props = { | |
defaultValue: 'bravo.value', | |
inputs: [ | |
{ value: 'alpha.value', label: 'alpha.label' }, | |
{ value: 'bravo.value', label: 'bravo.label' }, | |
{ value: 'charlie.value', label: 'charlie.label' } | |
] | |
}; | |
const component = createComponent(props); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function statelessRadio(React) { | |
return function StatelessRadio({ baseId, titleText, inputs = [] } = {}) { | |
return ( | |
<div id={baseId}> | |
<p id={`${baseId}__title`}>{titleText}</p> | |
{inputs.map((inputItem) => { | |
return ( | |
<div> | |
<input name={baseId} value={inputItem.value} /> | |
<label>{inputItem.label}</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function statelessRadio(React) { | |
return function StatelessRadio(props) { | |
return ( | |
<div id={props.baseId}> | |
<p id={`${props.baseId}__title`}>{props.titleText}</p> | |
{props.inputs.map((inputItem) => { | |
return ( | |
<div> | |
<input name={props.baseId} value={inputItem.value} /> | |
<label>{inputItem.label}</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
function createComponent(customProps = {}) { | |
const props = Object.assign({ baseId: 'test-baseId' }, customProps); | |
const StatelessRadio = statelessRadio(React); | |
return <StatelessRadio {...props} />; | |
} | |
describe('statelessRadio', () => { | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it('should render radio inputs and their labels from the inputs prop', () => { | |
// Create props with an inputs collection. | |
// I use consistent and ordered naming for data series i.e. "alpha", "bravo", "charlie", "delta", etc. | |
// I find using a naming scheme like this in cases where order matters, helps clarify the test setup and assertions. | |
const props = { | |
baseId: 'test-baseId', | |
inputs: [ | |
{ value: 'alpha.value', label: 'alpha.label' }, | |
{ value: 'bravo.value', label: 'bravo.label' }, | |
{ value: 'charlie.value', label: 'charlie.label' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="radio" name="bagel-choices" value="onion" /> | |
<label>Onion</label> | |
<input type="radio" name="bagel-choices" value="everything" /> | |
<label>Everything</label> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const props = { | |
baseId: 'bagel-choices' | |
inputs: [ | |
{ value: 'onion', label: 'Onion' }, | |
{ value: 'everything', label: 'Everything' } | |
] | |
} |