Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#336/Fix broken unit tests caused by react-children-utilities dependency. #352

Closed
25 changes: 20 additions & 5 deletions src/components/Form/FormHelpButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import FormHelpButton, { help } from './FormHelpButton'

describe('FormHelpButton', () => {
it('renders', () => {
const { getByLabelText } = render(<FormHelpButton identifier="abc" help={help('def')} />)
const { getByLabelText } = render(<FormHelpButton identifier="abc" help={help('def', 'hij')} />)

expect(getByLabelText('help')).not.toBeNull()
})

it('initially hides help', () => {
const { queryByText } = render(<FormHelpButton identifier="abc" help={help('def')} />)
const { queryByText } = render(<FormHelpButton identifier="abc" help={help('def', 'hij')} />)

expect(queryByText('def')).toBeNull()
})

it('opens', async () => {
const { getByLabelText, findByText, queryByText } = render(<FormHelpButton identifier="abc" help={help('def')} />)
const { getByLabelText, findByText, queryByText } = render(
<FormHelpButton identifier="abc" help={help('def', 'hij')} />,
)

fireEvent.click(getByLabelText('help'))

Expand All @@ -35,8 +37,21 @@ describe('FormHelpButton', () => {
expect(queryByText('some help')).toBeTruthy()
})

it('it displays a ReactNode', async () => {
const { getByLabelText, findByText, queryByText } = render(
<FormHelpButton identifier="abc" help={help('def', <span>Hello, ReactNode</span>)} />,
)

fireEvent.click(getByLabelText('help'))

await findByText('Hello, ReactNode')
expect(queryByText('Hello, ReactNode')).toBeTruthy()
})

Comment on lines +40 to +50
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gj262 Here's a unit test for setting HelpParams content to a ReactNode

it('closes inside', async () => {
const { getByLabelText, findByText, queryByText } = render(<FormHelpButton identifier="abc" help={help('def')} />)
const { getByLabelText, findByText, queryByText } = render(
<FormHelpButton identifier="abc" help={help('def', 'hij')} />,
)
fireEvent.click(getByLabelText('help'))
await findByText('def')
expect(queryByText('def')).not.toBeNull()
Expand All @@ -50,7 +65,7 @@ describe('FormHelpButton', () => {
it('closes outside', async () => {
const { getByLabelText, findByText, getByText, queryByText } = render(
<div>
<FormHelpButton identifier="abc" help={help('def')} />
<FormHelpButton identifier="abc" help={help('def', 'hij')} />
<span>click outside</span>
</div>,
)
Expand Down
10 changes: 5 additions & 5 deletions src/components/Form/FormHelpButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ function safeId(id: string) {

export interface HelpProps {
label: string
text: string
content: string | React.ReactNode
}

export function help(label = '', text = ''): HelpProps {
return { label, text }
export function help(label: string, content: string | React.ReactNode): HelpProps {
return { label, content }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gj262 text renamed to content, and it allows React.ReactNode.

}

export interface FormHelpButtonProps {
identifier: string
help?: HelpProps
help: HelpProps
}

export default function FormHelpButton({ identifier, help }: FormHelpButtonProps) {
Expand All @@ -43,7 +43,7 @@ export default function FormHelpButton({ identifier, help }: FormHelpButtonProps
<Card className="card--help">
<CardBody>
<h4>{help.label}</h4>
<p>{help.text}</p>
<p>{help.content}</p>
</CardBody>
</Card>
</UncontrolledPopover>
Expand Down