Skip to content

Commit

Permalink
export blockEvent to utils, update Options-test and Options to reflec…
Browse files Browse the repository at this point in the history
…t this change
  • Loading branch information
gwyneplaine committed Jan 5, 2018
1 parent 6f52559 commit 7f2810a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
14 changes: 1 addition & 13 deletions src/Option.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React from 'react';

const blockEvent = event => {
event.preventDefault();
event.stopPropagation();
if ((event.target.tagName !== 'A') || !('href' in event.target)) {
return;
}
if (event.target.target) {
window.open(event.target.href, event.target.target);
} else {
window.location.href = event.target.href;
}
};
import { blockEvent } from './utils/blockEvent';

class Option extends React.Component {

Expand Down
12 changes: 12 additions & 0 deletions src/utils/blockEvent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default event => {
event.preventDefault();
event.stopPropagation();
if ((event.target.tagName !== 'A') || !('href' in event.target)) {
return;
}
if (event.target.target) {
window.open(event.target.href, event.target.target);
} else {
window.location.href = event.target.href;
}
};
10 changes: 6 additions & 4 deletions test/Option-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import unexpected from 'unexpected';
import unexpectedDom from 'unexpected-dom';
import unexpectedSinon from 'unexpected-sinon';

import blockEvent from '../src/utils/blockEvent';

import Option from '../src/Option';

helper();
Expand Down Expand Up @@ -123,7 +125,7 @@ describe('Option component', () => {
stopPropagation,
};

instance.blockEvent(event);
blockEvent(event);

expect(openStub, 'was called once');
expect(openStub, 'was called with', event.target.href, event.target.target);
Expand All @@ -146,7 +148,7 @@ describe('Option component', () => {

expect(window.location.href, 'not to equal', event.target.href);

instance.blockEvent(event);
blockEvent(event);

expect(window.location.href, 'to equal', event.target.href);
expect(openStub, 'was not called');
Expand All @@ -169,7 +171,7 @@ describe('Option component', () => {

expect(window.location.href, 'to equal', 'url');

instance.blockEvent(event);
blockEvent(event);

expect(window.location.href, 'to equal', 'url');
expect(openStub, 'was not called');
Expand All @@ -191,7 +193,7 @@ describe('Option component', () => {

expect(window.location.href, 'to equal', 'url');

instance.blockEvent(event);
blockEvent(event);

expect(window.location.href, 'to equal', 'url');
expect(openStub, 'was not called');
Expand Down

0 comments on commit 7f2810a

Please sign in to comment.