Skip to content

Commit

Permalink
chore: add RadioGroup jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
BRaimbault committed Dec 23, 2024
1 parent ddb7e98 commit 7af9ac1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/components/core/RadioGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ export const RadioContext = React.createContext()

const RadioGroup = ({
value,
label,
helpText,
display,
label = '',
helpText = '',
display = 'column',
onChange,
boldLabel,
compact,
boldLabel = false,
compact = false,
children,
dataTest,
}) => {
const [radio, setRadio] = useState(value)

useEffect(() => {
if (value !== radio) {
setRadio(value)
}
}, [value, radio])
setRadio(value)
}, [value])

return (
<RadioContext.Provider value={{ radio, onChange }}>
Expand All @@ -33,20 +31,17 @@ const RadioGroup = ({
[styles.compact]: compact,
})}
>
<FieldGroup
label={
<span
className={cx({
[styles.boldLabel]: boldLabel,
[styles.compact]: compact,
})}
>
{label}
</span>
}
helpText={helpText}
dataTest={dataTest}
>
{label && (
<div
className={cx({
[styles.boldLabel]: boldLabel,
[styles.compact]: compact,
})}
>
{label}
</div>
)}
<FieldGroup helpText={helpText} dataTest={dataTest}>
{children}
</FieldGroup>
</div>
Expand All @@ -60,7 +55,7 @@ RadioGroup.propTypes = {
children: PropTypes.arrayOf(PropTypes.node),
compact: PropTypes.bool,
dataTest: PropTypes.string,
display: PropTypes.string,
display: PropTypes.oneOf(['row', 'column']),
helpText: PropTypes.string,
label: PropTypes.string,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
Expand Down
81 changes: 81 additions & 0 deletions src/components/core/__tests__/RadioGroup.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { mount } from 'enzyme'
import React from 'react'
import { FieldGroup } from '@dhis2/ui'

Check failure on line 3 in src/components/core/__tests__/RadioGroup.spec.js

View workflow job for this annotation

GitHub Actions / lint

`@dhis2/ui` import should occur before import of `enzyme`
import RadioGroup, { RadioContext } from '../RadioGroup.js'

describe('RadioGroup', () => {
const mockOnChange = jest.fn()

const renderWithProps = (props) => mount(<RadioGroup {...props} />)

const value = 'option1'
const label = 'Select an option'
const helpText = 'Choose one of the available options'
const children = [
<input key="key1" type="radio" value="option1" />,
<input key="key2" type="radio" value="option2" />,
]
let props

beforeEach(() => {
jest.clearAllMocks()
props = {
value,
label,
helpText,
display: 'column',
onChange: mockOnChange,
boldLabel: true,
compact: false,
children,
dataTest: 'radio-group',
}
})

it('renders the label with the correct style', () => {
const wrapper = renderWithProps(props)
const labelElement = wrapper.children().find('div').first()
expect(labelElement.text()).toBe(label)
expect(labelElement.hasClass('boldLabel')).toBe(true)
})

it('renders the help text', () => {
const wrapper = renderWithProps(props)
expect(wrapper.find(FieldGroup).prop('helpText')).toBe(helpText)
})

it('provides the current value and onChange via context', () => {
const wrapper = renderWithProps(props)
const context = wrapper.find(RadioContext.Provider).prop('value')
expect(context.radio).toBe(value)
expect(context.onChange).toBe(mockOnChange)
})

it('renders children inside the FieldGroup', () => {
const wrapper = renderWithProps(props)
expect(wrapper.find(FieldGroup).contains(children)).toBe(true)
})

it('updates the radio state when value prop changes', () => {
const wrapper = renderWithProps(props)
expect(wrapper.find(RadioContext.Provider).prop('value').radio).toBe(
value
)

wrapper.setProps({ value: 'option2' })
wrapper.update()
expect(wrapper.find(RadioContext.Provider).prop('value').radio).toBe(
'option2'
)
})

it('applies the correct class based on display and compact props', () => {
props.display = 'row'
props.compact = true
const wrapper = renderWithProps(props)
const container = wrapper.find('div').first()

expect(container.hasClass('row')).toBe(true)
expect(container.hasClass('compact')).toBe(true)
})
})

0 comments on commit 7af9ac1

Please sign in to comment.