gpt4 book ai didi

reactjs - React 测试库 : Test if children are passed/rendered correctly

转载 作者:行者123 更新时间:2023-12-03 14:26:20 26 4
gpt4 key购买 nike

我正在使用 TypeScript 编写一个 React 应用程序。我使用material-ui作为我的组件,使用react-testing-library作为我的单元测试。

我正在为 Material-ui 的 Grid 组件编写一个包装器,以便我始终拥有一个容器。

import Grid from "@material-ui/core/Grid";
import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles";
import React, { PureComponent } from "react";
import styles from "./styles";

export interface OwnProps {
className?: string;
}

export interface Props extends WithStyles<typeof styles>, OwnProps {}

export interface DefaultProps {
className: string;
}

export class GridContainer extends PureComponent<Props & DefaultProps> {
static defaultProps: DefaultProps = {
className: ""
};

render() {
const { classes, children, className, ...rest } = this.props;
return (
<Grid
data-testid="grid-container"
container={true}
{...rest}
className={classes.grid + " " + className}
>
{children}
</Grid>
);
}
}

export default withStyles(styles)(GridContainer);

我想编写一个测试来检查它的子项是否正确呈现。这是我写的:

import "jest-dom/extend-expect";
import React from "react";
import { cleanup, render } from "react-testing-library";
import GridContainer, { OwnProps } from "./GridContainer";
afterEach(cleanup);

const createTestProps = (props?: object): OwnProps => ({
...props
});

describe("GridContainer", () => {
const props = createTestProps();
const { getByTestId } = render(
<GridContainer {...props}>
<div data-testid="child" />
</GridContainer>
);
const container = getByTestId("grid-container");
describe("rendering", () => {
test("it renders it's children", () => {
expect(container.children.length).toBe(1);
expect(getByTestId("child")).toBeDefined();
});
});
});

问题是测试的第一部分,我检查 children 通过的时间长度。但是 expect(getByTestId("child")).toBeDefined(); 失败并显示:

● GridContainer › rendering › it renders it's children

Unable to find an element by: [data-testid="child"]

<body />

24 | test("it renders it's children", () => {
25 | expect(container.children.length).toBe(1);
> 26 | expect(getByTestId("child")).toBeDefined();
| ^
27 | });
28 | });
29 | });

at getElementError (node_modules/dom-testing-library/dist/query-helpers.js:30:10)
at getAllByTestId (node_modules/dom-testing-library/dist/queries.js:231:45)
at firstResultOrNull (node_modules/dom-testing-library/dist/query-helpers.js:38:30)
at getByTestId (node_modules/dom-testing-library/dist/queries.js:241:42)
at Object.getByTestId (src/components/Grid/GridContainer/GridContainer.test.tsx:26:14)

是否无法为渲染函数中的元素提供 data-testid?我如何测试 children 是否正确渲染?

编辑这是调试的输出:

● Console

console.log node_modules/react-testing-library/dist/index.js:57
<body>
<div>
<div
class="MuiGrid-container-2 GridContainer-grid-1 "
data-testid="grid-container"
>
<div
data-testid="child"
/>
</div>
</div>
</body>

最佳答案

问题是您在 describe block 中渲染一次。这可能并不像你想的那样。不在 test 内的 describe block 中的代码(或其他 jest 函数,如 beforeEach)会被 Jest 提前运行。一般来说,您永远不希望在 describe block 内但在 test (或其他 Jest 函数)之外有任何代码。

您可以在 beforeEach 中渲染组件,或者在每个测试中调用一个渲染辅助函数。

我用上述方法在这里 fork 了你的沙箱,现在两个测试都通过了:https://codesandbox.io/s/v3wk1vlx3l

关于reactjs - React 测试库 : Test if children are passed/rendered correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133125/

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