Skip to content

Commit

Permalink
feat(Button): onClick of inner button returns value of props.onClick.
Browse files Browse the repository at this point in the history
This change makes Button implementation return the value of props.onClick in its own onClick function.

Rationale for the change: in my own use case, I have some onClick function call fetch methods. These onClick methods return the promise returned by fetch. For normal application use, this does not do anything. In unit tests however, this allows the unit-test to trigger the click and await on the handling of the response before doing assertions (using fetch-mock...). After switching from react-bootstrap to reactstrap, those tests do not work anymore since this onClick implementation does not pass the promise along.

This change is backwards compatible if you assume nobody relies on onClick returning undefined. Please let me know if I can improve this change in any way.
  • Loading branch information
fmichea authored and iamandrewluca committed Jun 26, 2020
1 parent 5282735 commit f09cdd6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Button extends React.Component {
}

if (this.props.onClick) {
this.props.onClick(e);
return this.props.onClick(e);
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/Button.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ describe('Button', () => {
expect(onClick).toHaveBeenCalled();
});

it('returns the value returned by props.onClick', () => {
const onClick = jest.fn(() => 1234);
const wrapper = mount(<Button onClick={onClick}>Testing Click</Button>);

const result = wrapper.find('button').props().onClick();
expect(onClick).toHaveBeenCalled();
expect(result).toEqual(1234);
});

it('is not called when disabled', () => {
const e = createSpyObj('e', ['preventDefault']);
const wrapper = mount(<Button>Testing Click</Button>);
Expand Down

0 comments on commit f09cdd6

Please sign in to comment.