Exploring the functionality of closing Material UI Drawer on escape key in a React 16 app with RTL support

I am currently experimenting with the Material UI Drawer component. I expected it to close when pressing the Esc key or clicking outside of it, but unfortunately, it is not behaving as anticipated.
I am utilizing the react testing library for my tests and have set up a codesandbox environment with React 16.9.0.

sandbox with index.js open

Answer №1

When I faced a similar issue, I found a solution by ensuring that the "Escape" key bubbles up correctly. To simplify your testing process, you can assign datat-testid to the list content element. You may name it as data-testid="list-component". Below is an example test based on your code:

  it('closes drawer on escape key', () => {
    // render
    const { queryByTestId } = render(<TemporaryDrawer />);

    // open drawer
    fireEvent.click(queryByTestId('right-btn'));
    expect(queryByTestId('list-component')).toBeInTheDocument();

    // close drawer
    fireEvent.keyDown(queryByTestId('list-component'), { key: 'Escape', bubbles: true });
    expect(queryByTestId('list-component')).not.toBeInTheDocument();
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the recommended data type for Material UI Icons when being passed as props?

What specific type should I use when passing Material UI Icons as props to a component? import {OverridableComponent} from "@mui/material/OverridableComponent"; import {SvgIconTypeMap} from "@mui/material"; interface IconButtonProps { ...

Handlers for adjustments not correctly updating values in introduced object

I am facing an issue with a table that displays data fetched from an API and a select dropdown. Whenever a checkbox is selected, the name key along with its value is added to an array object named selectedFields. checkbox = ({ name, isChecked }) => { ...

Tips for preventing a function from being triggered twice during a state change

I'm currently working with a react component that looks like this: const [filter, setFilter] = useState(valueFromProps); const [value, setValue] = useState(valueFromProps); const initialRender = useRef(true); useEffect(() => { if (initialRender. ...

The provided argument, which is of type 'RefObject<HTMLDivElement>', cannot be assigned to the parameter of type 'IDivPosition'

Currently, I am implementing Typescript with React. To organize my code, I've created a separate file for my custom function called DivPosition.tsx. In this setup, I am utilizing useRef to pass the reference of the div element to my function. However ...

Encountering an ERR! message of missing script when trying to run the <npm start> command

Each time I attempt to run npm start, the frustrating "npm ERR! missing script: start" error appears. I have exhaustively attempted to modify the "start" line in the package.json file following all available suggestions on Google dating back a year. In a ...

What is the best way to determine which DOM element triggered a click event?

I currently have a few Card components from material UI, each containing an EDIT button with a corresponding handler. These cards are dynamically added using Map traversal (for this example, I have hard coded two of them). My challenge now is trying to ma ...

What is the best way to retrieve a variable within an arrow function?

I'm attempting to pass the value of score, which is inside the getScoreText arrow function, as a prop to a child component. However, I am struggling to even console log it from outside. Is what I want to achieve possible? const getScore = () => { ...

Tips for customizing text color in a disabled MUI TextField?

In the disabled TextField, I want the text color to be black instead of the default gray. I attempted the following after finding inspiration from this source: <TextField id="outlined-basic" value={'https://git.responsive.software/my-app ...

I keep encountering a "map is not a function" error while working with React JS

I am attempting to retrieve and display data from a MySQL database using Spring Boot and React JS in a table format. However, I am encountering an error while trying to display the information. componentDidMount(){ const currentUser = AuthService.g ...

Ending the Active Element within React's Accordion Component

I have created a basic accordion component for my product page in a Next.js/react app. It is functioning mostly as expected, but I am facing an issue. When a user clicks on a new accordion item, I want the currently active one to close automatically. Below ...

React JS iterates through incorrect properties

Currently, I am retrieving two types of keywords from an API - user-added keywords and allowed keywords. Despite setting up the actions and reducers correctly, I am facing an issue where mapping through the 'keywords' props also includes the allo ...

Problem with React Router: Uncaught Error - Invariant Violation: The element type is not valid, a string is expected for built-in components

I am encountering an issue with react-router and unable to render my app due to this error. Here is a screenshot of the error I have searched extensively for a solution but have not been able to find anything useful. Any help would be greatly appreciated ...

Using useNavigate outside of a React hook: What you need to know

This code retrieves a list of emails from Firestore and verifies if the current user is registered. If the user is new, it redirects them to the sign-up page. The functionality works correctly as it redirects successfully, but an error message appears: W ...

What is the best way to set up a server in React using Express or HTTP?

I am currently in the process of developing a web application using react js. In order to create a server for my client within the project, I have decided to utilize either express or http. Here is the code snippet that I attempted: import React from " ...

Enhancing React Performance: Storing Component Identity in Redux State

Can I safely pass this to a Redux action creator from a component defined using React.createClass? In my code, I have created the following reducer: const unsavedChangesProtectionReducer = handleActions({ [ENABLE_UNSAVED_CHANGES_PROTECTION]: (unsaved ...

Utilizing React Native to seamlessly integrate one JavaScript function into another

I am trying to integrate one javascript module into another. I recently downloaded a demo of GiftedMessanger After downloading the GiftedMessanger demo code, I incorporated all its dependencies into my own React Native (ios) project and successfully insta ...

The ability to update a variable passed through the state in Link is restricted

Currently, I am in the process of updating the theme in my App using useState. The updated theme is passed to the Topbar Component as a prop, triggering console.log() every time it changes. The theme is then passed from the Topbar to the AboutMe Component ...

Using redux thunk with react-redux alongside Router functionality

I recently created a git repository that I've been using to practice ReactJS and delve into learning about redux-thunk. While it started off relatively easy, I've hit a roadblock in understanding how it works with routes. During my investigation ...

I am facing an issue where the state in Next.js React does not get updated when passing a

"use client"; import { EntityType, World } from "@/types/World"; import React, { useState } from "react"; import dynamic from "next/dynamic"; ; let RayTracingPage = dynamic(()=>import("./RayTracingCanvas&qu ...

React Material UI and DayJs are not syncing up properly when displaying dates

In my React application, I am using MUI Cards to display a list of objects. One of the fields in these objects is of type date in postgres. When I retrieve a sample card object from the database, this is the value that appears in the browser console: { ...