Skip to content

fix: correct markup for Abbr component #19317

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

Merged
merged 6 commits into from
Aug 14, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: update tests
  • Loading branch information
Parkreiner committed Aug 13, 2025
commit 0a076c7ea2094e041170b546bfed7f085a1bbd4d
55 changes: 31 additions & 24 deletions site/src/components/Abbr/Abbr.test.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
import { render, screen } from "@testing-library/react";
import { Abbr, type Pronunciation } from "./Abbr";
import { Abbr } from "./Abbr";

type AbbreviationData = {
abbreviation: string;
title: string;
expectedLabel: string;
};

type AssertionInput = AbbreviationData & {
pronunciation: Pronunciation;
};

function assertAccessibleLabel({
Copy link
Member Author

Choose a reason for hiding this comment

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

Got rid of the test helper because it felt like we were binding all our tests to a shallow abstraction

abbreviation,
title,
expectedLabel,
pronunciation,
}: AssertionInput) {
const { unmount } = render(
<Abbr title={title} pronunciation={pronunciation}>
{abbreviation}
</Abbr>,
);

screen.getByLabelText(expectedLabel, { selector: "abbr" });
unmount();
}

describe(Abbr.name, () => {
it("Has an aria-label that equals the title if the abbreviation is shorthand", () => {
const sampleShorthands: AbbreviationData[] = [
Expand All @@ -43,7 +23,18 @@ describe(Abbr.name, () => {
];

for (const shorthand of sampleShorthands) {
assertAccessibleLabel({ ...shorthand, pronunciation: "shorthand" });
const { unmount } = render(
<Abbr title={shorthand.title} pronunciation="shorthand">
{shorthand.abbreviation}
</Abbr>,
);

// The <abbr> element doesn't have any ARIA role semantics baked in,
// so we have to get a little bit more creative with making sure the
// expected content is on screen in an accessible way
const element = screen.getByTitle(shorthand.title);
expect(element).toHaveTextContent(shorthand.expectedLabel);
unmount();
}
});

Expand All @@ -67,7 +58,15 @@ describe(Abbr.name, () => {
];

for (const acronym of sampleAcronyms) {
assertAccessibleLabel({ ...acronym, pronunciation: "acronym" });
const { unmount } = render(
<Abbr title={acronym.title} pronunciation="acronym">
{acronym.abbreviation}
</Abbr>,
);

const element = screen.getByTitle(acronym.title);
expect(element).toHaveTextContent(acronym.expectedLabel);
unmount();
}
});

Expand All @@ -91,7 +90,15 @@ describe(Abbr.name, () => {
];

for (const initialism of sampleInitialisms) {
assertAccessibleLabel({ ...initialism, pronunciation: "initialism" });
const { unmount } = render(
<Abbr title={initialism.title} pronunciation="initialism">
{initialism.abbreviation}
</Abbr>,
);

const element = screen.getByTitle(initialism.title);
expect(element).toHaveTextContent(initialism.expectedLabel);
unmount();
}
});
});