React 测试库:测试属性/prop
React testing library: Test attribute / prop
我正在使用 TypeScript 编写 React 应用程序。我对我的组件使用 material-ui,对我的单元测试使用 react-testing-library。
我正在为 material-ui 的网格组件编写一个包装器,以便我始终有一个项目。
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 GridItem extends PureComponent<Props & DefaultProps> {
static defaultProps: DefaultProps = {
className: ""
};
render() {
const { classes, children, className, ...rest } = this.props;
return (
<Grid
data-testid="grid-item"
item={true}
{...rest}
className={classes.grid + " " + className}
>
{children}
</Grid>
);
}
}
export default withStyles(styles)(GridItem);
我想编写一个单元测试来检查 item={true}
。我试着像这样使用助手库 jest-dom 的 toHaveAttribute
:
import "jest-dom/extend-expect";
import React from "react";
import { cleanup, render } from "react-testing-library";
import GridItem, { OwnProps } from "./GridItem";
afterEach(cleanup);
const createTestProps = (props?: object): OwnProps => ({
...props
});
describe("Parallax", () => {
const props = createTestProps();
const { getByTestId } = render(<GridItem {...props} />);
describe("rendering", () => {
test("it renders the image", () => {
expect(getByTestId("grid-item")).toHaveAttribute("item", "true");
});
});
});
但是这个测试失败了:
● GridItem › rendering › it renders the image
expect(element).toHaveAttribute("item", "true") // element.getAttribute("item") === "true"
Expected the element to have attribute:
item="true"
Received:
null
14 | describe("rendering", () => {
15 | test("it renders the image", () => {
> 16 | expect(getByTestId("grid-item")).toHaveAttribute("item", "true");
| ^
17 | });
18 | });
19 | });
at Object.toHaveAttribute (src/components/Grid/GridItem/GridItem.test.tsx:16:40)
Test Suites: 1 failed, 3 passed, 4 total
Tests: 1 failed, 3 passed, 4 total
Snapshots: 0 total
Time: 1.762s, estimated 2s
Ran all test suites related to changed files.
如何测试元素是否具有特定属性?
jest-dom
toHaveAttribute
断言断言 item
属性 而测试试图测试 item
prop .
item
属性不一定会导致 item
属性,并且由于它是非标准属性,所以很可能不会。
react-testing-library
传播功能测试并断言结果 DOM,这需要了解组件的工作方式。可以看出 here,item
道具导致向网格元素添加 class。
除测试单元外的所有单元都应在单元测试中模拟,例如:
...
import GridItem, { OwnProps } from "./GridItem";
jest.mock("@material-ui/core/Grid", () => ({
default: props => <div data-testid="grid-item" className={props.item && item}/>
}));
那么可以断言为:
expect(getByTestId("grid-item")).toHaveClass("item");
如果有人仍然遇到这个问题,我是这样解决的:
it('Check if it is a materialUI Grid item', () => {
//Rendering the component in a constant.
const { container } = render(<YourComponent />);
//Accessing the grid wrapper. In this case by the attribute you provided.
const grid = container.querySelector('[data-testid="grid-item"]');
//What we are expecting the grid to have.
expect(grid).toHaveClass('MuiGrid-item');
})
备注:
- 我注意到在代码项中它被声明为字符串而不是布尔值:item='true',这将在您 运行 测试时触发警告。 item={true} 是声明它的正确方法。实际上在 material UI 中,当你在网格中写入 item 时,它当然默认为 true,在我看来是没有必要的。
- item 是 class inside material UI Grid 正如前面的答案正确建议的那样。因此在这种情况下应该引用正确的 class 名称是 'MuiGrid-item'.
我正在使用 TypeScript 编写 React 应用程序。我对我的组件使用 material-ui,对我的单元测试使用 react-testing-library。
我正在为 material-ui 的网格组件编写一个包装器,以便我始终有一个项目。
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 GridItem extends PureComponent<Props & DefaultProps> {
static defaultProps: DefaultProps = {
className: ""
};
render() {
const { classes, children, className, ...rest } = this.props;
return (
<Grid
data-testid="grid-item"
item={true}
{...rest}
className={classes.grid + " " + className}
>
{children}
</Grid>
);
}
}
export default withStyles(styles)(GridItem);
我想编写一个单元测试来检查 item={true}
。我试着像这样使用助手库 jest-dom 的 toHaveAttribute
:
import "jest-dom/extend-expect";
import React from "react";
import { cleanup, render } from "react-testing-library";
import GridItem, { OwnProps } from "./GridItem";
afterEach(cleanup);
const createTestProps = (props?: object): OwnProps => ({
...props
});
describe("Parallax", () => {
const props = createTestProps();
const { getByTestId } = render(<GridItem {...props} />);
describe("rendering", () => {
test("it renders the image", () => {
expect(getByTestId("grid-item")).toHaveAttribute("item", "true");
});
});
});
但是这个测试失败了:
● GridItem › rendering › it renders the image
expect(element).toHaveAttribute("item", "true") // element.getAttribute("item") === "true"
Expected the element to have attribute:
item="true"
Received:
null
14 | describe("rendering", () => {
15 | test("it renders the image", () => {
> 16 | expect(getByTestId("grid-item")).toHaveAttribute("item", "true");
| ^
17 | });
18 | });
19 | });
at Object.toHaveAttribute (src/components/Grid/GridItem/GridItem.test.tsx:16:40)
Test Suites: 1 failed, 3 passed, 4 total
Tests: 1 failed, 3 passed, 4 total
Snapshots: 0 total
Time: 1.762s, estimated 2s
Ran all test suites related to changed files.
如何测试元素是否具有特定属性?
jest-dom
toHaveAttribute
断言断言 item
属性 而测试试图测试 item
prop .
item
属性不一定会导致 item
属性,并且由于它是非标准属性,所以很可能不会。
react-testing-library
传播功能测试并断言结果 DOM,这需要了解组件的工作方式。可以看出 here,item
道具导致向网格元素添加 class。
除测试单元外的所有单元都应在单元测试中模拟,例如:
...
import GridItem, { OwnProps } from "./GridItem";
jest.mock("@material-ui/core/Grid", () => ({
default: props => <div data-testid="grid-item" className={props.item && item}/>
}));
那么可以断言为:
expect(getByTestId("grid-item")).toHaveClass("item");
如果有人仍然遇到这个问题,我是这样解决的:
it('Check if it is a materialUI Grid item', () => {
//Rendering the component in a constant.
const { container } = render(<YourComponent />);
//Accessing the grid wrapper. In this case by the attribute you provided.
const grid = container.querySelector('[data-testid="grid-item"]');
//What we are expecting the grid to have.
expect(grid).toHaveClass('MuiGrid-item');
})
备注:
- 我注意到在代码项中它被声明为字符串而不是布尔值:item='true',这将在您 运行 测试时触发警告。 item={true} 是声明它的正确方法。实际上在 material UI 中,当你在网格中写入 item 时,它当然默认为 true,在我看来是没有必要的。
- item 是 class inside material UI Grid 正如前面的答案正确建议的那样。因此在这种情况下应该引用正确的 class 名称是 'MuiGrid-item'.