Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
Rename folder, trying to get all the specs passing
Browse files Browse the repository at this point in the history
  • Loading branch information
el-mapache committed Jul 3, 2017
1 parent 5cd9f0d commit d79e326
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 41 deletions.
8 changes: 3 additions & 5 deletions static_src/components/action.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import React from 'react';
import classnames from 'classnames';
import Link from './actions/link.jsx';
import Button from './actions/button.jsx';
import Link from './action/link.jsx';
import Button from './action/button.jsx';

const BUTTON_TYPES = {
BUTTON: 'button',
Expand Down Expand Up @@ -55,10 +55,8 @@ export default class Action extends React.Component {
}

get sharedProps() {
const buttonClasses = this.buttonClasses;

return {
className: classnames(this.baseClasses, this.classes, buttonClasses),
className: classnames(this.baseClasses, this.classes, this.buttonClasses),
label: this.props.label,
clickHandler: this.props.clickHandler
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ const propTypes = {
href: React.PropTypes.string,
label: React.PropTypes.string
};
const defaultPropTypes = {
href: '#'
};
const defaultHref = '#';

const Link = ({ className, label, href, clickHandler, children }) =>
<a
className={ classnames(className, 'action-link') }
title={ label }
onClick={ clickHandler }
href={ href }
href={ href || defaultHref }
>
{ children }
</a>;

Link.propTypes = propTypes;
Link.defaultPropTypes = defaultPropTypes;

export default Link;
90 changes: 59 additions & 31 deletions static_src/test/unit/components/action.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,85 @@ import '../../global_setup.js';
import React from 'react';
import { shallow } from 'enzyme';
import Action from '../../../components/action.jsx';
import Link from '../../../components/action/link.jsx';
import Button from '../../../components/action/button.jsx';

describe('<Action />', function () {
let action, sandbox;
let action;

beforeEach(function () {
sandbox = sinon.sandbox.create();
});
describe('default behavior', () => {
it('returns a button', () => {
action = shallow(<Action />);

afterEach(function () {
sandbox.restore();
expect(action.find(Button).length).toBe(1);
});
});

describe('given type is link', function () {
beforeEach(function () {
action = shallow(<Action type="link" />);
});
describe('component creation', () => {
describe('given type is link', function () {
beforeEach(function () {
action = shallow(<Action type="link" />);
});

it('renders as a link', function () {
expect(action.find('a').length).toBe(1);
});
it('renders as a <Link />', function () {
expect(action.find(Link).length).toBe(1);
});

it('does not render a button', function () {
expect(action.find('button').length).toBe(0);
});

describe('given an href', function () {
const href = 'https://example.com';

beforeEach(function () {
action = shallow(<Action type="link" href={href} />);
});

it('does not render a button', function () {
expect(action.find('button').length).toBe(0);
it('renders with the href', function () {
expect(action.find(Link).prop('href')).toBe(href);
});
});
});

describe('given an href', function () {
beforeEach(function () {
action = shallow(<Action type="link" href="https://example.com" />);
describe('given any other kind of type', () => {
it('renders a button', () => {
action = shallow(<Action type="submit" />);
expect(action.find(Button).length).toBe(1);
action = shallow(<Action type="outline" />);
expect(action.find(Button).length).toBe(1);
});
});

describe('with props passed', () => {
it('always passes `clickHandler`, `label`, and `className`', () => {
const buttonProps = { type: 'submit', label: 'my label', classes: ['k'] };
const actionButton = shallow(<Action {...buttonProps} />).find(Button);

expect(actionButton.prop('label')).toEqual(buttonProps.label);
expect(actionButton.prop('className')).toMatch(new RegExp(buttonProps.classes[0]));
expect(typeof actionButton.prop('clickHandler')).toBe('function');

it('renders with the href', function () {
expect(action.find('a').prop('href')).toBe('https://example.com');
const linkProps = { href: 'p.com', label: 'great label', classes: ['yii'] };
const actionLink = shallow(<Action {...linkProps} />).find(Link);

expect(actionLink.prop('label')).toEqual(linkProps.label);
expect(actionLink.prop('className')).toMatch(new RegExp(linkProps.classes[0]));
expect(typeof actionLink.prop('clickHandler')).toBe('function');
});
});
});

describe('clickHandler', function () {
describe('clickHandler', () => {
let clickHandlerSpy;
beforeEach(function () {
clickHandlerSpy = sandbox.spy();
beforeEach(() => {
clickHandlerSpy = sinon.spy();
action = shallow(<Action clickHandler={ clickHandlerSpy } />);
});

describe('on click', function () {
beforeEach(function () {
action.simulate('click');
});

it('triggers clickHandler', function () {
expect(clickHandlerSpy).toHaveBeenCalledOnce();
});
xit('triggers clickHandler', () => {
action.find(Button).simulate('click');
expect(clickHandlerSpy).toHaveBeenCalledOnce();
});
});
});
29 changes: 29 additions & 0 deletions static_src/test/unit/components/action/button.spec.jsx
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);
});
});
26 changes: 26 additions & 0 deletions static_src/test/unit/components/action/link.spec.jsx
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);
});
});

0 comments on commit d79e326

Please sign in to comment.