Skip to content

Commit

Permalink
fix(ToggleButton): fixes error when pressing enter (#2183)
Browse files Browse the repository at this point in the history
* fix(ToggleButton): fixes error when pressing enter

* Add a test

---------

Co-authored-by: Tobias Høegh <tobias@tujo.no>
  • Loading branch information
langz and tujoworker committed May 31, 2023
1 parent ea811e5 commit a5188da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,15 @@ export default class ToggleButton extends React.PureComponent {
onKeyDownHandler = (event) => {
switch (keycode(event)) {
case 'enter':
this.onClickHandler(event)
this.onClickHandler({ event })
break
}
}

onKeyUpHandler = (event) => {
switch (keycode(event)) {
case 'enter':
this.onClickHandler(event)
this.onClickHandler({ event })
break
}
}
Expand All @@ -232,7 +232,7 @@ export default class ToggleButton extends React.PureComponent {
return
}

// else we change the checked sstate
// else we change the checked state
const checked = !this.state.checked
this.setState({
checked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
*/

import { render } from '@testing-library/react'
import { fireEvent, render } from '@testing-library/react'
import React from 'react'
import {
mount,
Expand Down Expand Up @@ -235,6 +235,19 @@ describe('ToggleButton component', () => {
)
})

it('should support enter key', () => {
const onChange = jest.fn()
render(<Component on_change={onChange} />)

const element = document.querySelector('button')

fireEvent.keyDown(element, { keyCode: 13 }) // enter
expect(onChange).toHaveBeenCalledTimes(1)

fireEvent.keyUp(element, { keyCode: 13 }) // enter
expect(onChange).toHaveBeenCalledTimes(2)
})

it('should support spacing props', () => {
render(<Component top="2rem" />)

Expand Down

0 comments on commit a5188da

Please sign in to comment.