FAQ: React Testing Library - Mimicking User Interactions

This community-built FAQ covers the “Mimicking User Interactions” exercise from the lesson “React Testing Library”.

Paths and Courses
This exercise can be found in the following Codecademy content:

(Beta) Learn React Testing

FAQs on the exercise Mimicking User Interactions

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In both the code example and the exercise of this lesson, we grab the input with the getByRole method and pass it the string ‘textbox’, but I can’t see where we use that textbox word anywhere in the JSX.

import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
 
const GreetingForm = () => {
  return(
    <form>
      <label htmlFor="greeting">
        Greeting:
      </label>
      <input type="text" id="greeting" />
      <input type="submit" value="Submit" />
    </form>
  );
};
 
test('should show text content as Hey Mack!', () => {
  // Render the component to test
  render(<GreetingForm />);
  // Extract the textbox component
  const textbox = screen.getByRole('textbox');
  // Simulate typing 'Hey Mack!'
  userEvent.type(textbox, 'Hey Mack!');
  // Assert textbox has text content 'Hey Mack!'

Also the docs suggest we use the label name to grab the input element.

Can someone explain how come the code works like this? Is the input element by default considered to have a role of textbox as an arbitrary name? Why don’t we just use ‘input’ instead in the getByRole method or grab by the label if we need to be more specific?

Yeah the lesson skips over why ‘textbox’ was used but the reason is that input elements with a type of ‘text’ have a role of 'textbox.
Other input types have different roles. An input with a type of ‘checkbox’ would have a role of ‘checkbox’ so it’s the same in this case.

1 Like