Hone logo
Hone
Problems

Accessible React Component Testing with Jest

Ensuring your React components are accessible is crucial for inclusivity and reaching a wider audience. This challenge focuses on implementing Jest tests to verify basic accessibility compliance of a simple React component. You'll use jest-axe to automate accessibility checks, ensuring your components adhere to WCAG guidelines.

Problem Description

You are tasked with creating Jest tests for a simple React component called Greeting. The Greeting component accepts a name prop and displays a personalized greeting. Your goal is to write tests that verify the component's accessibility, specifically checking for:

  • Element has a valid role: The root element of the component should have a meaningful role (e.g., 'presentation' if it's purely presentational).
  • Element has sufficient contrast: The text color should have sufficient contrast against the background color.
  • Element has a label or accessible name: If the component contains interactive elements (like buttons or inputs), they should have associated labels or accessible names.

You will be provided with the Greeting component code. Your tests should use jest-axe to perform the accessibility checks and assert that no violations are found.

Key Requirements:

  • Install jest-axe as a dev dependency.
  • Write Jest tests that render the Greeting component with different name props.
  • Use axe.run within your tests to analyze the rendered component.
  • Assert that the number of accessibility violations is zero.
  • Handle potential errors during the accessibility check gracefully.

Expected Behavior:

The tests should pass if the Greeting component is accessible according to the checks performed by jest-axe. If any accessibility violations are detected, the tests should fail, providing detailed information about the violations.

Edge Cases to Consider:

  • Consider how the tests might behave if the Greeting component is rendered with invalid or unexpected props. While this challenge focuses on basic accessibility, thinking about robustness is good practice.
  • Think about how to handle asynchronous operations within the tests, if any are needed.

Examples

Example 1:

Input: Greeting component rendered with name "Alice"
Output: No accessibility violations found.
Explanation: The component displays "Hello, Alice!" with sufficient contrast and a valid role.

Example 2:

Input: Greeting component rendered with name "Bob" and a background color that results in insufficient contrast.
Output: Accessibility violation: "Contrast (minimum) - The visual contrast of some content is not sufficient for users with low vision."
Explanation: The low contrast between the text and background triggers the contrast violation.

Example 3: (Edge Case)

Input: Greeting component rendered with an empty string as the name.
Output: No accessibility violations found.
Explanation: The component displays "Hello, " with sufficient contrast and a valid role.  The empty name doesn't inherently introduce accessibility issues.

Constraints

  • You must use Jest and jest-axe.
  • The Greeting component is provided and should not be modified.
  • Tests should be written in TypeScript.
  • The focus is on basic accessibility checks; complex accessibility scenarios are beyond the scope of this challenge.
  • Performance is not a primary concern for this challenge.

Notes

  • jest-axe provides detailed reports of accessibility violations, which can be helpful for debugging.
  • Consider using toHaveTextContent in your tests to verify the displayed text.
  • Remember to install the necessary dependencies: npm install --save-dev jest-axe @testing-library/react @testing-library/jest-dom
  • The Greeting component code is provided below. You'll need to include this in your test file.
// Greeting.tsx
import React from 'react';

interface GreetingProps {
  name: string;
}

const Greeting: React.FC<GreetingProps> = ({ name }) => {
  return (
    <div style={{ backgroundColor: '#f0f0f0', padding: '10px', color: '#333' }}>
      Hello, {name}!
    </div>
  );
};

export default Greeting;
Loading editor...
typescript