Hone logo
Hone
Problems

Accessible Component Testing with Jest and a Mock Screen Reader

Ensuring your React components are accessible to users with screen readers is crucial for inclusivity. This challenge focuses on creating a Jest test suite that verifies a component's accessibility by simulating screen reader interactions and asserting on the announced text. You'll use a mock screen reader implementation to avoid relying on a real screen reader during testing.

Problem Description

You are tasked with creating a Jest test suite for a simple React component called Greeting. The Greeting component displays a personalized greeting message. The goal is to write tests that verify the component announces the correct greeting message when a screen reader is active. You will use a mock screen reader object to simulate screen reader announcements and assert that the expected text is announced.

What needs to be achieved:

  • Create a Jest test suite for the Greeting component.
  • Mock the screenReader object (provided below) to simulate screen reader announcements.
  • Use act from react-dom/test-utils to wrap the component rendering and interaction to ensure proper state updates are reflected.
  • Assert that the correct greeting message is announced by the mock screen reader.

Key Requirements:

  • The test suite should use Jest and React Testing Library.
  • The Greeting component should accept a name prop.
  • The mock screenReader object should have an announce method that accepts a string.
  • The tests should be reliable and avoid false positives.
  • The tests should be easy to understand and maintain.

Expected Behavior:

When the Greeting component is rendered with a specific name, the test should verify that the screen reader announces a message containing the provided name. For example, if name is "Alice", the test should assert that the screen reader announces "Hello, Alice!".

Edge Cases to Consider:

  • What happens if the name prop is empty or null? The component should still announce a greeting, but without a name.
  • Ensure the test suite handles asynchronous updates correctly, using act to wrap the component rendering and any interactions.

Examples

Example 1:

Input: Greeting component with name prop set to "Bob"
Output: The mock screenReader.announce is called with "Hello, Bob!"
Explanation: The component renders the greeting with Bob's name, and the screen reader announces the appropriate message.

Example 2:

Input: Greeting component with no name prop (name is undefined)
Output: The mock screenReader.announce is called with "Hello, there!"
Explanation: The component renders the default greeting when no name is provided.

Constraints

  • The Greeting component is provided below. Do not modify it.
  • The screenReader object is provided below. Do not modify it.
  • You must use React Testing Library and Jest.
  • The tests should run within a reasonable time (less than 1 second).
  • The tests should be written in TypeScript.

Notes

  • Consider using waitFor from React Testing Library to handle asynchronous updates.
  • The act function from react-dom/test-utils is crucial for ensuring that state updates are properly reflected before assertions are made. Wrap the component rendering and any interactions within act.
  • Focus on testing the accessibility aspect of the component, not its visual rendering.
  • The provided screenReader object is a simplified mock. Real screen readers have more complex behavior.

Provided Code:

import React from 'react';

interface GreetingProps {
  name?: string;
}

const Greeting: React.FC<GreetingProps> = ({ name }) => {
  const greetingMessage = name ? `Hello, ${name}!` : 'Hello, there!';

  return <div>{greetingMessage}</div>;
};

export default Greeting;

const screenReader = {
  announce: (text: string) => {
    // This is a mock - it doesn't actually announce anything.
  },
};
Loading editor...
typescript