Skip to content

Commit

Permalink
SiteAlert enhancement (#1215)
Browse files Browse the repository at this point in the history
Co-authored-by: haworku <haworku@users.noreply.github.com>
  • Loading branch information
Kyle Hill and haworku authored Sep 8, 2021
1 parent 53fb48f commit 3cd70f9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/components/SiteAlert/SiteAlert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,15 @@ export const alertWithCustomControls = (
{shortAlertContent}
</SiteAlert>
)

export const alertWithStringContent = (): React.ReactElement => (
<SiteAlert variant="info">Short alert content</SiteAlert>
)

export const alertWithMultipleChildContent = (): React.ReactElement => (
<SiteAlert variant="info">
<p className="usa-alert__text">Alert content</p>
<em>which includes</em> <strong>formatting tags</strong> and{' '}
<Link href="#">links</Link>.
</SiteAlert>
)
47 changes: 47 additions & 0 deletions src/components/SiteAlert/SiteAlert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,51 @@ describe('SiteAlert component', () => {
'usa-site-alert--no-heading'
)
})

it('wraps a child string in a <p> tag', () => {
const { getByText } = render(
<SiteAlert variant="info">Test alert text</SiteAlert>
)

expect(getByText('Test alert text')).toHaveClass('usa-alert__text')
expect(getByText('Test alert text').tagName).toEqual('P')
})

it('does not wrap a single child element', () => {
const { getByText } = render(
<SiteAlert variant="info">
<strong className="test-element-class-name">Test alert text</strong>
</SiteAlert>
)

const childElement = getByText('Test alert text')
const parentElement = childElement.parentElement

expect(childElement).not.toHaveClass('usa-alert__text')
expect(childElement).toHaveClass('test-element-class-name')
expect(parentElement).not.toHaveClass('usa-alert__text')
expect(parentElement).toHaveClass('usa-alert__body')
})

it('renders, but does not wrap, multiple child elements', () => {
const { getByText } = render(
<SiteAlert variant="info">
{[
<strong key={0} className="test-main-class-name">
Test alert text
</strong>,
<em key={1} className="test-subtext-class-name">
Test alert subtext
</em>,
]}
</SiteAlert>
)

const childElement = getByText('Test alert subtext')
const parentElement = childElement.parentElement

expect(childElement).not.toHaveClass('usa-alert__text')
expect(parentElement?.childNodes.length).toEqual(2)
expect(parentElement).not.toHaveClass('usa-alert__text')
})
})
10 changes: 8 additions & 2 deletions src/components/SiteAlert/SiteAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import classnames from 'classnames'

interface SiteAlertProps {
variant: 'info' | 'emergency'
children: React.ReactNode
children: string | React.ReactNode | React.ReactNode[]
heading?: string
showIcon?: boolean
slim?: boolean
Expand All @@ -30,6 +30,12 @@ export const SiteAlert = ({
},
className
)

let content = children
if (typeof children === 'string') {
content = <p className="usa-alert__text">{children}</p>
}

return (
<section
data-testid="siteAlert"
Expand All @@ -39,7 +45,7 @@ export const SiteAlert = ({
<div className="usa-alert">
<div className="usa-alert__body">
{heading && <h3 className="usa-alert__heading">{heading}</h3>}
{children}
{content}
</div>
</div>
</section>
Expand Down

0 comments on commit 3cd70f9

Please sign in to comment.