Skip to content

Commit

Permalink
fix: button's onClick functions not being called (#339)
Browse files Browse the repository at this point in the history
- close #336
  • Loading branch information
LouisBarranqueiro committed Jan 8, 2021
1 parent cf9e52f commit 50b7331
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/NotificationButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {useState} from 'react'
import {Notification, NotificationButton as ButtonType} from '../reducers/notifications/types'
import {Theme} from '../themes/types'
import {classnames} from '../constants'
import {noop} from '../utils'

type Props = {
button: ButtonType
Expand All @@ -27,6 +28,7 @@ export const NotificationButton = (props: Props) => {
onMouseEnter={() => setIsHovered(true)}
onMouseDown={() => setIsActive(true)}
onMouseUp={() => setIsActive(false)}
onClick={typeof button.onClick === 'function' ? () => button.onClick!() : noop}
>
<span className={classnames.notificationButtonText} style={buttonTextStyles}>
{button.primary ? <b>{button.name}</b> : button.name}
Expand Down
17 changes: 17 additions & 0 deletions src/components/tests/NotificationButton.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ describe('<NotificationButton/>', () => {

jest.resetAllMocks()
})

it('should call `onClick` function by clicking on it', () => {
const button = {
name: 'yes',
onClick: jest.fn(),
}
const {getByTestId} = render(
<NotificationButton button={button} position={1} notification={notification} theme={atalhoTheme} />
)
const buttonElem = getByTestId('button')
act(() => {
fireEvent.click(buttonElem)
})

expect(button.onClick).toHaveBeenCalledTimes(1)
expect(button.onClick).toHaveBeenCalledWith()
})
})
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ export const clone = <T extends unknown>(origObject: T): T => {
}

export const isObject = (value: unknown) => !!value && (value as any).constructor === Object

export const noop = () => {
// noop
}

0 comments on commit 50b7331

Please sign in to comment.