gpt4 book ai didi

reactjs - 如何在 React 中使用 Enzyme 或 React 测试库测试 Material-UI 的响应式 UI(例如隐藏、网格、断点)

转载 作者:行者123 更新时间:2023-12-03 16:48:19 24 4
gpt4 key购买 nike

如何在 Material-UI 中测试响应式元素?
例子:

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

const HideOnMobile = (props) => {
return <Hidden xsDown>{props.children}</Hidden>;
};
测试用例:
describe(HideOnMobile, () => {
describe("when screensize is sm", () => {
it("shows children", () => {});
});

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

最佳答案

如解释 here您可以使用主题 Prop 为 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>;
};
例如使用 React 测试库:
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();
});
});
});

关于reactjs - 如何在 React 中使用 Enzyme 或 React 测试库测试 Material-UI 的响应式 UI(例如隐藏、网格、断点),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63116406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com