Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Material-UI's Responsive UI (e.g. Hidden, Grid, Breakpoints) in React with Enzyme or React Testing Library

How to test responsive elements in Material-UI?

Example:

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

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

Test cases:

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

  describe("when screensize is xs", () => {
    it("hides children", () => {});
  });
});
like image 589
thisismydesign Avatar asked Jul 27 '20 13:07

thisismydesign


People also ask

What does act do react testing library?

act() To prepare a component for assertions, wrap the code rendering it and performing updates inside an act() call. This makes your test run closer to how React works in the browser. If you use react-test-renderer , it also provides an act export that behaves the same way.

Does react testing library use Jest?

React Testing Library is not specific to any testing framework; we can use it with any other testing library, although Jest is recommended and preferred by many developers. create-react-app uses both Jest and React Testing Library by default.

Does react testing library shallow render?

React Testing libraryEnzyme's shallow renderer doesn't render sub-components, so React Testing Library's render method is more similar to Enzyme's mount method. In React Testing Library, you don't need to assign the render result to a variable (i.e. wrapper ).

Is Materialui responsive?

Material Design layouts encourage consistency across platforms, environments, and screen sizes by using uniform elements and spacing. Responsive layouts in Material Design adapt to any possible screen size.

Is there a way to test the material UI?

Yes, this is the best approach I find also by now as Material UI is a wrapper around the base component select in this case, you have to specify the data-testId through the inputProps. This worked for me but it feels like it doesn't really test whether the UI works, but rather that the value can be set programmatically.

How do I make the Ui responsive in Material Design?

Responsive layouts in Material Design adapt to any possible screen size. We provide the following helpers to make the UI responsive: Grid: The Material Design responsive layout grid adapts to screen size and orientation, ensuring consistency across layouts. Container: The container centers your content horizontally.

How to test MUI without the React component tree?

By not relying on the React component tree you make your test more robust against internal changes in MUI or, if you need snapshot testing, adding additional wrapper components such as context providers. We don't recommend snapshot testing though.

Do you use your own components with material UI?

I use my own components with material UI under the hood). The tests in myapp are written using enzyme and jest. And they work just fine. Material-UI is deprecating their test utils and will no longer be present in v5.


1 Answers

MUI v5

Unfortunately, the v4 solution doesn't work for v5.

MUI v4

As explained here you can use theme props to set size properties for MUI:

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 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();
    });
  });
});
like image 76
thisismydesign Avatar answered Sep 21 '22 19:09

thisismydesign