diff --git a/src/__tests__/index.js b/src/__tests__/index.js index 1c9afe3..f3dbf4f 100644 --- a/src/__tests__/index.js +++ b/src/__tests__/index.js @@ -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() diff --git a/src/index.js b/src/index.js index a48f3e5..c466358 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,7 @@ class Toggle extends Component { getInputTogglerProps = (props = {}) => this.getTogglerProps({ + ...props, onKeyUp: callAll(props.onKeyUp, event => { if (event.key === 'Enter') { // already respond to Enter @@ -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()