This repository has been archived by the owner on May 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break up the render method logic into functions
* Adds two components to represent a link and button. * Removes nested if/else statements in favor of smalled named functions Rename folder, trying to get all the specs passing
- Loading branch information
1 parent
2ac776b
commit 87a89b0
Showing
6 changed files
with
229 additions
and
101 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
|
||
const propTypes = { | ||
children: React.PropTypes.any, | ||
className: React.PropTypes.string, | ||
clickHandler: React.PropTypes.func, | ||
disabled: React.PropTypes.bool, | ||
label: React.PropTypes.string, | ||
type: React.PropTypes.string | ||
}; | ||
|
||
const button = ({ className, label, clickHandler, disabled, type, children }) => | ||
<button | ||
className={ className } | ||
aria-label={ label } | ||
onClick={ clickHandler } | ||
disabled={ disabled } | ||
type={type} | ||
> | ||
{ children } | ||
</button>; | ||
|
||
button.propTypes = propTypes; | ||
|
||
export default button; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react'; | ||
import classnames from 'classnames'; | ||
|
||
const propTypes = { | ||
children: React.PropTypes.any, | ||
className: React.PropTypes.string, | ||
clickHandler: React.PropTypes.func, | ||
href: React.PropTypes.string, | ||
label: React.PropTypes.string | ||
}; | ||
const defaultHref = '#'; | ||
|
||
const Link = ({ className, label, href, clickHandler, children }) => | ||
<a | ||
className={ classnames(className, 'action-link') } | ||
title={ label } | ||
onClick={ clickHandler } | ||
href={ href || defaultHref } | ||
> | ||
{ children } | ||
</a>; | ||
|
||
Link.propTypes = propTypes; | ||
|
||
export default Link; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import '../../../global_setup.js'; | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import Button from '../../../../components/action/button.jsx'; | ||
|
||
describe('<Button/>', () => { | ||
it('returns a button tag', () => { | ||
expect(shallow(<Button />).find('button').length).toBe(1); | ||
}); | ||
|
||
it('supplies the correct props to its child', () => { | ||
const props = { | ||
className: 'usa-button', | ||
disabled: false, | ||
clickHandler: () => true, | ||
type: 'button', | ||
label: 'my-button' | ||
}; | ||
const button = shallow(<Button { ...props } />); | ||
const actualProps = button.find('button').props(); | ||
|
||
expect(actualProps.className).toEqual(props.className); | ||
expect(actualProps.disabled).toEqual(props.disabled); | ||
expect(actualProps.onClick).toEqual(props.clickHandler); | ||
expect(actualProps.type).toEqual(props.type); | ||
expect(actualProps['aria-label']).toEqual(props.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import '../../../global_setup.js'; | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import Link from '../../../../components/action/link.jsx'; | ||
|
||
describe('<Link />', () => { | ||
it('returns an `a` tag', () => { | ||
expect(shallow(<Link />).find('a').length).toBe(1); | ||
}); | ||
|
||
it('sets a default href', () => { | ||
const link = shallow(<Link />); | ||
expect(link.find('a').prop('href')).toBe('#'); | ||
}); | ||
|
||
it('sets a base class of `action-link`', () => { | ||
expect(shallow(<Link />).find('a').hasClass('action-link')).toBe(true); | ||
}); | ||
|
||
it('renders its children', () => { | ||
const child = 'hi'; | ||
const link = shallow(<Link>{child}</Link>); | ||
expect(link.find('a').prop('children')).toBe(child); | ||
}); | ||
}); |