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

Commit

Permalink
fix: call given onClick when using getInputTogglerProps(props) & getE…
Browse files Browse the repository at this point in the history
…lementTogglerProps(props) (#12)
  • Loading branch information
Andarist authored and Kent C. Dodds committed Jan 19, 2018
1 parent 94b1a56 commit 56b2430
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@ test('getTogglerProps returns an onClick that calls the given onClick', () => {
expect(childSpy).toHaveBeenLastCalledWith(expect.objectContaining({on: true}))
})

test('getElementTogglerProps returns an onClick that calls the given onClick', () => {
const {childSpy, getElementTogglerProps} = setup()
const mockClick = jest.fn()
const {onClick} = getElementTogglerProps({onClick: mockClick})
const fakeEvent = {target: null}
onClick(fakeEvent)
expect(mockClick).toHaveBeenCalledTimes(1)
expect(mockClick).toHaveBeenCalledWith(fakeEvent)
expect(childSpy).toHaveBeenLastCalledWith(expect.objectContaining({on: true}))
})

test('getInputTogglerProps returns an onClick that calls the given onClick', () => {
const {childSpy, getInputTogglerProps} = setup()
const mockClick = jest.fn()
const {onClick} = getInputTogglerProps({onClick: mockClick})
const fakeEvent = {target: null}
onClick(fakeEvent)
expect(mockClick).toHaveBeenCalledTimes(1)
expect(mockClick).toHaveBeenCalledWith(fakeEvent)
expect(childSpy).toHaveBeenLastCalledWith(expect.objectContaining({on: true}))
})

test('getElementTogglerProps returns an onKeyUp that toggles on enter', () => {
const {childSpy, getElementTogglerProps} = setup()
const {onKeyUp} = getElementTogglerProps()
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Toggle extends Component {

getInputTogglerProps = (props = {}) =>
this.getTogglerProps({
...props,
onKeyUp: callAll(props.onKeyUp, event => {
if (event.key === 'Enter') {
// <input> already respond to Enter
Expand All @@ -47,6 +48,7 @@ class Toggle extends Component {

getElementTogglerProps = (props = {}) =>
this.getTogglerProps({
...props,
onKeyUp: callAll(props.onKeyUp, event => {
if (this.toggleKeys.indexOf(event.key) > -1) {
this.toggle()
Expand Down

0 comments on commit 56b2430

Please sign in to comment.