Skip to content

Commit

Permalink
test: update accessibility testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adamalston committed Dec 9, 2023
1 parent c28e759 commit 1f61e95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
12 changes: 5 additions & 7 deletions src/Index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ describe('application tests', () => {
element: HTMLElement,
display: RegExp,
link?: string,
skipA11yNameCheck?: boolean,
) => {
expect(element).toBeVisible();
expect(element).toHaveAccessibleName();
expect(element).toHaveAccessibleDescription();
if (!skipA11yNameCheck) expect(element).toHaveAccessibleName();
expect(element).toHaveTextContent(display);
if (link) expect(element).toHaveAttribute('href', link);
};
Expand All @@ -54,26 +54,25 @@ describe('application tests', () => {

expect(parent).toBeVisible();
expect(parent).toHaveAccessibleName();
expect(parent).toHaveAccessibleDescription();
expect(parent).toHaveAttribute('href', link);
};

it('should render name: Adam Alston', () => {
const element = screen.getByTestId('name');

checkContent(element, /^Adam Alston$/);
checkContent(element, /^Adam Alston$/, undefined, true);
});

it('should render title: Software Engineer', () => {
const element = screen.getByTestId('title');

checkContent(element, /^Software Engineer$/);
checkContent(element, /^Software Engineer$/, undefined, true);
});

it('should render creator', () => {
const element = screen.getByTestId('creator');

checkContent(element, /^Adam Alston$/, 'https://www.adamalston.com');
checkContent(element, /^Adam Alston$/, 'https://www.adamalston.com/');
});

it('should render link to source code', () => {
Expand Down Expand Up @@ -129,7 +128,6 @@ describe('application tests', () => {
expect(toggle).toHaveAccessibleDescription();

expect(particles).toBeVisible();
expect(particles).toHaveAccessibleName();

// site should default to the dark theme
expect(toggle).toBeChecked();
Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const Footer = () => {
<F.Link
data-v2="creator"
aria-label="Adam Alston's personal website"
href="https://www.adamalston.com"
href="https://www.adamalston.com/"
rel="noopener noreferrer"
target="_blank"
$theme={theme}
Expand Down
8 changes: 7 additions & 1 deletion src/components/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ const T = {
export const Toggle = () => {
const { theme, setTheme } = useContext(AppContext);
const isDark: boolean = theme.key === 'dark';
const ariaLabel: string = `Currently in ${
const ariaLabel = `Currently in ${
isDark ? 'dark' : 'light'
} mode, switch to ${!isDark ? 'dark' : 'light'} mode`;
const descriptionId = 'toggle-description';
const descriptionText =
'Switch between dark and light mode for visual comfort.';

const handleToggle = (checked: boolean) => {
const key: string = checked ? 'dark' : 'light';

Expand All @@ -74,13 +78,15 @@ export const Toggle = () => {

return (
<T.Container>
<T.VisuallyHidden id={descriptionId}>{descriptionText}</T.VisuallyHidden>
<T.Toggle
data-v2="toggle"
id="toggle"
name="toggle"
type="checkbox"
checked={isDark}
aria-label={ariaLabel}
aria-describedby={descriptionId}
onChange={({ target }: ChangeEvent<HTMLInputElement>) => {
handleToggle(target.checked);
}}
Expand Down

0 comments on commit 1f61e95

Please sign in to comment.