Skip to content

Commit

Permalink
feat(composeEventHandlers): Break out event handler composition
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Martinez authored and ooHmartY committed Jun 28, 2018
1 parent cec37e8 commit 4466387
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/components/Input/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SearchIcon from "../Icons/Search";
import colors from "../../theme/colors";
import typography from "../../theme/typography";
import constants from "../../theme/constants";
import composeEventHandlers from "../../utils/composeEventHandlers";

const Input = styled.input.attrs({
type: "search"
Expand Down Expand Up @@ -70,10 +71,6 @@ const IconContainer = styled.div`
}
`;

const composeHandler = (...args) => e => {
args.forEach(fn => fn && fn(e));
};

export default class SearchInput extends React.Component {
static propTypes = {
onBlur: PropTypes.func,
Expand Down Expand Up @@ -120,8 +117,8 @@ export default class SearchInput extends React.Component {
<Input
{...{
...props,
onBlur: composeHandler(this.onBlur, props.onBlur),
onFocus: composeHandler(this.onFocus, props.onFocus),
onBlur: composeEventHandlers(this.onBlur, props.onBlur),
onFocus: composeEventHandlers(this.onFocus, props.onFocus),
slim,
invert
}}
Expand Down
27 changes: 27 additions & 0 deletions src/utils/__tests__/composeEventHandlers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import composeEventHandlers from "../composeEventHandlers";

describe("composeEventHandlers", () => {
const mockEvent = { target: "element" };

it("calls single event handler", () => {
const mock = jest.fn();
const handler = composeEventHandlers(mock);

handler(mockEvent);

expect(mock).toHaveBeenCalledWith(mockEvent);
});

it("calls multiple event handlers", () => {
const mock1 = jest.fn();
const mock2 = jest.fn();
const mock3 = jest.fn();
const handler = composeEventHandlers(mock1, mock2, mock3);

handler(mockEvent);

expect(mock1).toHaveBeenCalledWith(mockEvent);
expect(mock2).toHaveBeenCalledWith(mockEvent);
expect(mock3).toHaveBeenCalledWith(mockEvent);
});
});
5 changes: 5 additions & 0 deletions src/utils/composeEventHandlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const composeHandler = (...args) => e => {
args.forEach(fn => fn && fn(e));
};

export default composeHandler;

0 comments on commit 4466387

Please sign in to comment.