Exploring Responsive UI Testing in React using Material-UI Components (Hidden, Grid, Breakpoints) with Enzyme or React Testing Library

Looking to verify responsiveness in Material-UI?

Here is an example:

import React from "react";
import Hidden from "@material-ui/core/Hidden";

const HideOnMobile = (props) => {
  return <Hidden xsDown>{props.children}</Hidden>;
};

Check out these test scenarios:

describe(HideOnMobile, () => {
  describe("when screensize is sm", () => {
    it("shows children", () => {});
  });

  describe("when screensize is xs", () => {
    it("hides children", () => {});
  });
});

Answer №1

MUI Version 5

Unfortunately, the solution that worked for MUI version 4 does not work for version 5.

MUI Version 4

As mentioned here, you can use theme props to set size properties for Material-UI:

import { createMuiTheme, MuiThemeProvider } from "@material-ui/core/styles";

const SizeWrapper = (props) => {
  const theme = createMuiTheme({
    props: { MuiWithWidth: { initialWidth: "sm" } },
  });

  return <MuiThemeProvider theme={theme}>{props.children}</MuiThemeProvider>;
};

For example, when using React Testing Library:

describe(HideOnMobile, () => {
  describe("when screensize is sm", () => {
    it("shows children", () => {
      const testMessage = "Test Message";
      render(<HideOnMobile>{testMessage}</HideOnMobile>, { wrapper: SizeWrapper });

      expect(screen.getByText(testMessage)).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

Modify language in MUI mobile date selection for React JS

Is there a way to modify the text "clear" or change the language of this message? Click here for image description <LocalizationProvider adapterLocale={ru} dateAdapter={AdapterDateFns}> <MobileDatePicker ...

Dynamic color changing AppBar in Material-UI while scrolling in a React application

I have been struggling to change the color of the material-ui Appbar component when scrolling. I am not sure if there is a built-in functionality for this or if 'useScrollTrigger' can be used in this case. Any guidance on how to achieve this woul ...

Changing the className of buttons based on user interaction

I'm looking to create a function in React JS that changes the button's color when clicked. Specifically, I want the color of the button to change upon clicking. I have attempted to achieve this using the following code: {"classname" i ...

What is the best way to eliminate the input range in a React date range picker?

Here is an image illustrating a date range picker: https://i.stack.imgur.com/pwKaI.png I am looking to remove the labels for days before today and starting from today in the date range picker. How can I achieve this? Below is my code snippet: class Better ...

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 ...

Is there a way to trigger a function from a specific div element and showcase the JSON data it retrieves in

I am working with a React JS code page that looks like this: import React, { useState } from "react"; import { Auth, API } from "aws-amplify"; function dailyFiles(props) { const [apiError502, setApiError502] = useState(false); // Extracted into a re ...

What is the common approach for organizing an express and react based application - should the server and client code be stored together in one project or kept in separate projects or folders

Coming from a Microsoft background, I have always kept server and client applications in separate projects. Now, I am venturing into writing a client-server application using Express as the backend and React JS as the frontend. As a newbie to these tools, ...

Utilize NPM package exclusively for server-side rendering (SSR) within a Next.js application

I'm currently attempting to incorporate a private NPM module exclusively during the initial page load when rendering is taking place on the server. Despite using the code below, I am noticing that my package still appears in chunks in client.html when ...

Unpacking objects within an array in the backend of a Next.js application

How can I resolve this issue? I am passing the req.query parameter with an array but I am unable to destructure or use items from the array. In my Next.js API backend, I only get [object Object] or undefined. How can I access and select specific values fro ...

Having issues with the pagination feature of DataTables in the material-ui framework

I've set up a table to display some recommended products: <DataTables height={'auto'} selectable={false} showRowHover={true} columns={RECOMMENDED_TABLE_COLUMNS} data={this.state.products} showCheckboxes={fal ...

How can I check if the VPN is turned off in a React application?

import React from "react"; import { Offline, Online } from "react-detect-offline"; export default function DropDown() { return ( <> <Online>Only displayed when connected to the internet</Online> <Offline ...

What is the procedure for entering the output generated by genMockFromModule when creating a custom mock in Jest?

Looking at my orders directory structure, I have a function in the orders/helpers file that I want to test using a manual Jest mock. orders __mocks__ helpers.ts __tests__ orders.ts helpers.ts orders.ts The orders/helpers.ts file contains ...

What is the reason that the 400 status code consistently causes the enter catch block to execute when using axios?

In the frontend of my website, there is a contact form with three fields -> name, email, message. These fields are then sent to the backend using Axios. If a user fails to fill out any of the required fields, they should see a message saying "please f ...

Is there a way to achieve a scrollbar for both vertical and horizontal scrolling in a Material UI table while also

When working with Material UI code for creating a sticky header and vertical scroll, I encountered an issue while trying to implement horizontal scrolling as well. Despite attempting to use overflowX: "auto" and scroll on paper, the functionality did not w ...

In NextJs SSR, external components from a different package do not inherit styles when applied

I have incorporated an external react component into my Next.js app code. Here is a snippet of how the component appears: <AppBar className={clsx(classes.header)}> </AppBar> export const header = makeStyles((theme) => ({ header: { ...

"Personalizing Your MUI V5 TextField: A Step-by-Step

I'm currently working on customizing my theme for material v5 and I'm wondering how I can remove the black border that appears when hovering over the textfield component. This is the code snippet from my customized theme: MuiTextField: { s ...

Customize Typography style variations in Material-UI version 5

I have encountered a similar issue, but with a twist from Can't override root styles of Typography from Materil-UI Back in v4, I had a theme set up like this: import { createTheme } from '@material-ui/core/styles'; export const theme = cre ...

The request for headimage.png at http://localhost:3000/assets/images/headimage.png resulted in a 404 (Not Found) error

I am facing an issue where I am unable to display an image from the assets/images/headimage.png directory on my webpage. Instead, it is showing an error message indicating that the image cannot be found. Despite trying multiple solutions, I have been una ...

The implementation of combineSlices in reduxjs/[email protected] is an essential feature for managing

I am struggling to figure out how to properly useAppSelector with LazyLoadedSlices: Here is the setup of my store // @shared/redux/store.ts // comment: https://redux-toolkit.js.org/api/combineSlices // eslint-disable-next-line @typescript-eslint/no-empty ...

Global usage of makeStyles - useStyles() results in 'Invalid Hook Call' error

I am encountering the 'Invalid hook error' while trying to implement this example from Material UI's website: https://material-ui.com/styles/advanced/#GlobalCss.js import React from 'react'; import { makeStyles } from '@materi ...