Skip to content
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
5 changes: 2 additions & 3 deletions packages/react-core/src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const Spinner: React.FunctionComponent<SpinnerProps> = ({
'aria-valuetext': ariaValueText = 'Loading...',
diameter,
isInline = false,
'aria-label': ariaLabel,
'aria-label': ariaLabel = 'Contents',
'aria-labelledBy': ariaLabelledBy,
...props
}: SpinnerProps) => (
Expand All @@ -42,9 +42,8 @@ export const Spinner: React.FunctionComponent<SpinnerProps> = ({
aria-valuetext={ariaValueText}
viewBox="0 0 100 100"
{...(diameter && { style: { [cssDiameter.name]: diameter } as React.CSSProperties })}
{...(ariaLabel && { 'aria-label': ariaLabel })}
aria-label={ariaLabel}
{...(ariaLabelledBy && { 'aria-labelledBy': ariaLabelledBy })}
{...(!ariaLabel && !ariaLabelledBy && { 'aria-label': 'Contents' })}
{...props}
>
<circle className={styles.spinnerPath} cx="50" cy="50" r="45" fill="none" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { Spinner } from '../Spinner';

test('simple spinner', () => {
const { asFragment } = render(<Spinner />);
expect(asFragment()).toMatchSnapshot();
});

test('uses default aria-label of "Contents" when none is provided', () => {
render(<Spinner />);
expect(screen.getByRole('progressbar')).toHaveAccessibleName('Contents');
});

test('uses a custom aria-label when one is provided', () => {
render(<Spinner aria-label="Loading users" />);
expect(screen.getByRole('progressbar')).toHaveAccessibleName('Loading users');
});

test('keeps the default aria-label when aria-labelledby is provided', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Renders with accessible name via aria-labelledby when passed" might be slightly more fitting test name here.

render(
<>
<span id="spinner-label">Loading reports</span>
<Spinner aria-labelledby="spinner-label" />
</>
);

const spinner = screen.getByRole('progressbar');
expect(spinner).toHaveAttribute('aria-label', 'Contents');
expect(spinner).toHaveAttribute('aria-labelledby', 'spinner-label');
Comment on lines +28 to +29

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we separate the aria-label check into a separate test, "Renders with aria-label even when aria-labelledby is passed".

Then we can also remove the toHaveAttribute('aria-labelledby'... line; the check for the accessible name should suffice for ensuring aria-labelledby has the expected value.

expect(spinner).toHaveAccessibleName('Loading reports');
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

test('small spinner', () => {
const { asFragment } = render(<Spinner size="sm" />);
expect(asFragment()).toMatchSnapshot();
Expand Down