-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Make Summary Box more flexible (#1929)
BREAKING CHANGE: SummaryBox now exposes sub-components (SummaryBoxHeading and SummaryBoxContent) for a more compositional API. Consumers will need to update their implementation to match.
- Loading branch information
Isaac Garfinkle
authored
Apr 21, 2022
1 parent
37a3e32
commit a46ed35
Showing
9 changed files
with
234 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* eslint-disable jsx-a11y/anchor-is-valid */ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { SummaryBox } from './SummaryBox' | ||
import { SummaryBoxHeading } from '../SummaryBoxHeading/SummaryBoxHeading' | ||
import { SummaryBoxContent } from '../SummaryBoxContent/SummaryBoxContent' | ||
|
||
const testSummaryBoxContent = ( | ||
<> | ||
<SummaryBoxHeading headingLevel="h3">My heading</SummaryBoxHeading> | ||
<SummaryBoxContent> | ||
<ul className="usa-list"> | ||
<li> | ||
If you are under a winter storm warning, | ||
<a className="usa-summary-box__link" href="#"> | ||
find shelter | ||
</a> | ||
right away. | ||
</li> | ||
<li> | ||
Sign up for | ||
<a className="usa-summary-box__link" href="#"> | ||
your community’s warning system | ||
</a> | ||
. | ||
</li> | ||
<li> | ||
Learn the signs of, and basic treatments for, | ||
<a className="usa-summary-box__link" href="#"> | ||
frostbite | ||
</a> | ||
and | ||
<a className="usa-summary-box__link" href="#"> | ||
hypothermia | ||
</a> | ||
. | ||
</li> | ||
<li> | ||
Gather emergency supplies for your | ||
<a className="usa-summary-box__link" href="#"> | ||
home | ||
</a> | ||
and your | ||
<a className="usa-summary-box__link" href="#"> | ||
car | ||
</a> | ||
. | ||
</li> | ||
</ul> | ||
</SummaryBoxContent> | ||
</> | ||
) | ||
|
||
const customProps = { | ||
role: 'complementary', | ||
className: 'custom-class-name', | ||
} | ||
|
||
describe('SummaryBox component', () => { | ||
it('renders without errors', () => { | ||
const { getByTestId } = render( | ||
<SummaryBox>{testSummaryBoxContent}</SummaryBox> | ||
) | ||
|
||
expect(getByTestId('summary-box')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders passed in children', () => { | ||
const { getAllByRole, getByRole } = render( | ||
<SummaryBox>{testSummaryBoxContent}</SummaryBox> | ||
) | ||
|
||
expect(getByRole('heading')).toBeInTheDocument() | ||
expect(getAllByRole('listitem')).toHaveLength(4) | ||
expect(getAllByRole('link')).toHaveLength(6) | ||
}) | ||
|
||
it('renders attributes passed in through props', () => { | ||
const { queryByTestId } = render( | ||
<SummaryBox {...customProps}>{testSummaryBoxContent}</SummaryBox> | ||
) | ||
|
||
const qByTestId = queryByTestId('summary-box') | ||
expect(qByTestId).toHaveAttribute('role', 'complementary') | ||
expect(qByTestId).toHaveClass('usa-summary-box custom-class-name') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/components/SummaryBox/SummaryBoxContent/SummaryBoxContent.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { SummaryBoxContent } from './SummaryBoxContent' | ||
|
||
describe('SummaryBoxContent component', () => { | ||
it('renders without errors', () => { | ||
const { queryByTestId } = render( | ||
<SummaryBoxContent data-testid="collection-heading" /> | ||
) | ||
|
||
expect(queryByTestId('collection-heading')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders children', () => { | ||
const { queryByTestId } = render( | ||
<SummaryBoxContent data-testid="collection-heading"> | ||
<div data-testid="test-child" /> | ||
</SummaryBoxContent> | ||
) | ||
|
||
expect(queryByTestId('test-child')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders custom class name', () => { | ||
const { getByTestId } = render( | ||
<SummaryBoxContent | ||
data-testid="collection-heading" | ||
className="custom-class" | ||
/> | ||
) | ||
|
||
expect(getByTestId('collection-heading')).toHaveClass('custom-class') | ||
}) | ||
|
||
it('renders custom heading attributes', () => { | ||
const { getByTestId } = render( | ||
<SummaryBoxContent data-testid="collection-heading" aria-label="Hello" /> | ||
) | ||
|
||
expect(getByTestId('collection-heading')).toHaveAttribute( | ||
'aria-label', | ||
'Hello' | ||
) | ||
}) | ||
}) |
20 changes: 20 additions & 0 deletions
20
src/components/SummaryBox/SummaryBoxContent/SummaryBoxContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import classnames from 'classnames' | ||
|
||
interface SummaryBoxTextProps { | ||
children?: React.ReactNode | ||
className?: string | ||
} | ||
|
||
export const SummaryBoxContent = ({ | ||
children, | ||
className, | ||
...divProps | ||
}: SummaryBoxTextProps & JSX.IntrinsicElements['div']): React.ReactElement => { | ||
const classes = classnames('usa-summary-box__text', className) | ||
return ( | ||
<div className={classes} {...divProps}> | ||
{children} | ||
</div> | ||
) | ||
} |
50 changes: 50 additions & 0 deletions
50
src/components/SummaryBox/SummaryBoxHeading/SummaryBoxHeading.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { SummaryBoxHeading } from './SummaryBoxHeading' | ||
|
||
describe('SummaryBoxHeading component', () => { | ||
it('renders without errors', () => { | ||
const { queryByText, getByRole } = render( | ||
<SummaryBoxHeading headingLevel="h3">test text</SummaryBoxHeading> | ||
) | ||
|
||
expect(getByRole('heading')).toBeInTheDocument() | ||
expect(queryByText('test text')).toBeInTheDocument() | ||
}) | ||
|
||
it('renders default heading level', () => { | ||
const { getByRole } = render( | ||
<SummaryBoxHeading headingLevel="h3">test text</SummaryBoxHeading> | ||
) | ||
|
||
expect(getByRole('heading').tagName).toEqual('H3') | ||
}) | ||
|
||
it('renders custom level', () => { | ||
const { getByRole } = render( | ||
<SummaryBoxHeading headingLevel="h6">test text </SummaryBoxHeading> | ||
) | ||
|
||
expect(getByRole('heading').tagName).toEqual('H6') | ||
}) | ||
|
||
it('renders custom class name', () => { | ||
const { getByRole } = render( | ||
<SummaryBoxHeading className="custom-class" headingLevel="h3"> | ||
test text{' '} | ||
</SummaryBoxHeading> | ||
) | ||
|
||
expect(getByRole('heading')).toHaveClass('custom-class') | ||
}) | ||
|
||
it('renders custom heading attributes', () => { | ||
const { getByRole } = render( | ||
<SummaryBoxHeading aria-label="Hello" headingLevel="h3"> | ||
test text{' '} | ||
</SummaryBoxHeading> | ||
) | ||
|
||
expect(getByRole('heading')).toHaveAttribute('aria-label', 'Hello') | ||
}) | ||
}) |
24 changes: 24 additions & 0 deletions
24
src/components/SummaryBox/SummaryBoxHeading/SummaryBoxHeading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { ReactNode } from 'react' | ||
import classnames from 'classnames' | ||
|
||
interface SummaryBoxHeadingProps { | ||
children: ReactNode | ||
className?: string | ||
headingLevel: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | ||
} | ||
|
||
export const SummaryBoxHeading = ({ | ||
children, | ||
className, | ||
headingLevel, | ||
...h3Props | ||
}: SummaryBoxHeadingProps & | ||
JSX.IntrinsicElements['h3']): React.ReactElement => { | ||
const classes = classnames('usa-summary-box__heading', className) | ||
const Heading = headingLevel | ||
return ( | ||
<Heading className={classes} {...h3Props}> | ||
{children} | ||
</Heading> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters