Skip to content

Commit

Permalink
Fix handling of external prop
Browse files Browse the repository at this point in the history
  • Loading branch information
kuy committed Feb 11, 2017
1 parent 9c2c883 commit 636fc8f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/blog/components/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Layout extends Component {
<Menu.Menu position='right'>
{auth}
<Menu.Item><Link to='/admin/posts'>Admin</Link></Menu.Item>
<Menu.Item><Link external target='_blank' to='https://github.com/kuy/redux-tower'>GitHub</Link></Menu.Item>
<Menu.Item><Link external to='https://github.com/kuy/redux-tower'>GitHub</Link></Menu.Item>
<Menu.Item>
<Input onChange={this.handleChange.bind(this)} icon='search' placeholder='Search...' />
</Menu.Item>
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@
"babel-register": "^6.18.0",
"coveralls": "^2.11.15",
"css-loader": "^0.26.1",
"enzyme": "^2.7.1",
"file-loader": "^0.10.0",
"flow-bin": "^0.38.0",
"nyc": "^10.0.0",
"react": "^15.4.2",
"react-addons-test-utils": "^15.4.2",
"react-dom": "^15.4.2",
"react-redux": "^5.0.1",
"redux": "^3.6.0",
Expand All @@ -64,6 +66,7 @@
"semantic-ui-css": "^2.2.4",
"semantic-ui-react": "^0.64.3",
"style-loader": "^0.13.1",
"testdouble": "^1.11.1",
"url-loader": "^0.5.7",
"webpack": "^2.1.0-beta.27",
"webpack-dev-server": "^2.1.0-beta.12"
Expand Down
50 changes: 50 additions & 0 deletions src/react/__tests__/link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import test from 'ava';
import React from 'react';
import { shallow } from 'enzyme';
import td from 'testdouble';
import { Link } from '../link';

test('basic', t => {
const wrapper = shallow(<Link to='/hoge'>Hoge</Link>);
t.true(wrapper.matchesElement(<a href='/hoge' target='_self'>Hoge</a>));
});

test('href', t => {
const wrapper = shallow(<Link href='/foo'>Foo</Link>);
t.true(wrapper.matchesElement(<a href='/foo' target='_self'>Foo</a>));
});

test('external', t => {
const wrapper = shallow(<Link external to='http://example.com'>Example</Link>);
t.true(wrapper.matchesElement(<a href='http://example.com' target='_blank'>Example</a>));
});

test('class', t => {
const wrapper = shallow(<Link className='ui link' to='/users'>Users</Link>);
t.true(wrapper.matchesElement(<a href='/users' className='ui link' target='_self'>Users</a>));
});

test('click', t => {
const ev = td.object(['preventDefault']);
const dispatch = td.function('dispatch');
const handler = td.function('handler');
const wrapper = shallow(<Link to='/users' onClick={handler} dispatch={dispatch}>Link</Link>);
wrapper.find('a').simulate('click', ev);

td.verify(ev.preventDefault(), { times: 1 });
td.verify(handler(ev), { times: 1 });
td.verify(dispatch(td.matchers.isA(Object)), { times: 1 });
});

test('click + external', t => {
const handler = td.function('handler');
const wrapper = shallow(<Link external to='http://external.com' onClick={handler} dispatch={() => {}}>External</Link>);
wrapper.find('a').simulate('click', { preventDefault: () => {} });

td.verify(handler(), { times: 0 });
});

test('either "to" or "href"', t => {
const err = t.throws(() => shallow(<Link>Either</Link>));
t.is(err.message, "<Link> component requires either 'to' or 'href' props.");
});
6 changes: 4 additions & 2 deletions src/react/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getHref(props, withOffset = false) {
return href;
}

class Link extends Component {
export class Link extends Component {
handleClick(e) {
e.preventDefault();

Expand All @@ -39,7 +39,9 @@ class Link extends Component {
const href = getHref(this.props, true);

const props = { href, target, className };
if (!external) {
if (external) {
props.target = '_blank';
} else {
props.onClick = this.handleClick.bind(this);
}

Expand Down

0 comments on commit 636fc8f

Please sign in to comment.