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

Update error summary markup following nhs frontend #1036 #255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/components/form-elements/error-summary/ErrorSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import React, {
import classNames from 'classnames';
import useDevWarning from '@util/hooks/UseDevWarning';

const DefaultErrorSummaryTitleID = 'error-summary-title';

const ErrorSummaryTitle: FC<HTMLProps<HTMLHeadingElement>> = ({
className,
id = DefaultErrorSummaryTitleID,
id,
...rest
}) => (
<h2 className={classNames('nhsuk-error-summary__title', className)} id={id} {...rest} />
<h2 className={classNames('nhsuk-error-summary__title', className)}
{...(id && { id })}
{...rest}
/>
);


Expand Down Expand Up @@ -48,24 +49,24 @@ const ErrorSummaryDiv = forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>(
({
className,
tabIndex = -1,
role = 'alert',
'aria-labelledby': ariaLabelledBy = DefaultErrorSummaryTitleID,
children,
...rest
},
ref
) => {
useDevWarning('The ErrorSummary component should always have a tabIndex of -1', () => tabIndex !== -1)
useDevWarning('The ErrorSummary component should always have a role of alert', () => role !== 'alert')

return (
<div
className={classNames('nhsuk-error-summary', className)}
ref={ref}
tabIndex={tabIndex}
role={role}
aria-labelledby={ariaLabelledBy}
{...rest}
/>
>
<div role="alert">
{children}
</div>
</div>
)
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ describe('ErrorSummary', () => {
const { container } = render(<ErrorSummary />);

expect(container.querySelector('div')?.getAttribute('tabindex')).toBe('-1');
expect(container.querySelector('div')?.getAttribute('role')).toBe('alert');
expect(container.querySelector('div')?.getAttribute('aria-labelledby')).toBe('error-summary-title');
expect(container.querySelector('div')?.getAttribute('role')).toBeNull();
expect(container.querySelector('div')?.getAttribute('aria-labelledby')).toBeNull();
})

it('has role="alert" on the inner div', () => {
const { container } = render(<ErrorSummary />);

const alertDiv = container.querySelector('.nhsuk-error-summary > div[role="alert"]');
expect(alertDiv).not.toBeNull();
})

it('throws a dev warning if tabIndex is not -1', () => {
Expand All @@ -30,12 +37,6 @@ describe('ErrorSummary', () => {
expect(console.warn).toHaveBeenCalledWith('The ErrorSummary component should always have a tabIndex of -1');
})

it('throws a dev warning if role is not alert', () => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
render(<ErrorSummary role="status" />);
expect(console.warn).toHaveBeenCalledWith('The ErrorSummary component should always have a role of alert');
})

describe('ErrorSummary.Title', () => {
it('matches snapshot', () => {
const { container } = render(<ErrorSummary.Title>Title</ErrorSummary.Title>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exports[`ErrorSummary ErrorSummary.Title matches snapshot: ErrorSummary.Title 1`
<div>
<h2
class="nhsuk-error-summary__title"
id="error-summary-title"
>
Title
</h2>
Expand All @@ -44,10 +43,12 @@ exports[`ErrorSummary ErrorSummary.Title matches snapshot: ErrorSummary.Title 1`
exports[`ErrorSummary matches snapshot: ErrorSummary 1`] = `
<div>
<div
aria-labelledby="error-summary-title"
class="nhsuk-error-summary"
role="alert"
tabindex="-1"
/>
>
<div
role="alert"
/>
</div>
</div>
`;
Loading