Skip to content

Commit

Permalink
chore: update warning in button component (#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chisomchima authored and alaa-yahia committed Jun 10, 2024
1 parent 21d37f1 commit c6c36db
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
46 changes: 46 additions & 0 deletions components/button/src/button/__tests__/Button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ import React from 'react'
import { Button } from '../button.js'

describe('<Button>', () => {
let consoleSpy

beforeEach(() => {
consoleSpy = jest.spyOn(console, 'debug').mockImplementation()
})

afterEach(() => {
consoleSpy.mockRestore()
})

describe('warning for missing aria-label and title', () => {
it('No warning if children exist but aria-label and title is missing', () => {
render(<Button>Children content</Button>)

expect(consoleSpy).not.toHaveBeenCalled()
})

it('does not warn if aria-label and title is present', () => {
render(
<Button aria-label="Test" title="Test">
Children content
</Button>
)

expect(consoleSpy).not.toHaveBeenCalled()
})

it('warns if no children are present with no arial-label and title', () => {
render(<Button>{/* No children */}</Button>)

expect(consoleSpy).toHaveBeenCalledWith(
'Button component has no children but is missing title and ariaLabel attribute.'
)
})

it('No warning if there are no children but arial label and title', () => {
render(
<Button aria-label="Test" title="Test">
{/* No children */}
</Button>
)

expect(consoleSpy).not.toHaveBeenCalled()
})
})

it('renders a default data-test attribute', () => {
const dataTest = 'dhis2-uicore-button'
const wrapper = mount(<Button dataTest={dataTest} />)
Expand Down
8 changes: 8 additions & 0 deletions components/button/src/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export const Button = ({
}
}, [initialFocus, ref.current])

const { 'aria-label': ariaLabel, title } = otherProps

if (!children && !title && !ariaLabel) {
console.debug(
'Button component has no children but is missing title and ariaLabel attribute.'
)
}

const handleClick = (event) => onClick && onClick({ value, name }, event)
const handleBlur = (event) => onBlur && onBlur({ value, name }, event)
const handleFocus = (event) => onFocus && onFocus({ value, name }, event)
Expand Down

0 comments on commit c6c36db

Please sign in to comment.