[英]How to test this component with react testing library - just a starter with this new testing library
I have 2 components that i am learning to test with react-testing-library but i am stuck in the middle.我有 2 个组件,我正在学习使用 react-testing-library 进行测试,但我被困在中间。
My components is NestedLists.js我的组件是 NestedLists.js
import React from 'react'
export const NestedLists = ({filteredData}) => {
return (
<ul>
{filteredData && filteredData.map((m,i) => {
return (
<li key={i}>
{m.id}
{m.children && <NestedLists filteredData={m.children} />}
</li>
);
})}
</ul>
)
}
and filteredData props is an array with the following values ::而filteredData props 是一个具有以下值的数组::
export const filteredData = [{
"id": 1,
"label": "List item 1",
"parent_id": 0
},
{
"id": 9,
"label": "List item 9",
"parent_id": 8
},
{
"id": 8,
"label": "List item 8",
"parent_id": 1
},
{
"id": 5,
"label": "List item 5",
"parent_id": 1
},
{
"id": 6,
"label": "List item 6",
"parent_id": 1
},
{
"id": 7,
"label": "List item 7",
"parent_id": 1
},
{
"id": 10,
"label": "List item 10",
"parent_id": 8
},
{
"id": 2,
"label": "List item 2",
"parent_id": 0
}
]
I have done some tests but those are not working.我做了一些测试,但这些都不起作用。 Kindly suggests some of the tests that i can do on the above component with testing library::
请建议我可以使用测试库对上述组件进行的一些测试:
My tests that are npt working or failing is below:我的 npt 工作或失败的测试如下:
NestedLists.test.js NestedLists.test.js
import React from 'react';
import { render, waitForElement } from '@testing-library/react';
import {NestedLists} from '../components/NestedLists';
import {apiData} from '../fixtures'
test('renders NestedLists components', async () => {
const { getByText } = render(<NestedLists filteredData = {apiData} />);
await waitForElement(() => {
expect(getByText(props.filteredData[0].id)).toBeTruthy();
})
});
You probably don't need test for such simple component but if you still want to add test you can do something like below.您可能不需要对这样简单的组件进行测试,但如果您仍想添加测试,您可以执行以下操作。
You might need to import jest-dom from testing library to use toBeInTheDocument您可能需要从测试库中导入 jest-dom 才能使用 toBeInTheDocument
import React from 'react';
import { render} from '@testing-library/react';
import {NestedLists} from '../components/NestedLists';
import {apiData} from '../fixtures'
test('renders NestedLists components', async () => {
const { findByText} = render(<NestedLists filteredData = {apiData} />);
const firsListItem = await getByText(apiData[0].id);
epxect(firstListItem).toBeInTheDocument()
});
You can test like all elements are rendered.您可以像渲染所有元素一样进行测试。 Render the component using render from @testing-library/react by passing proper props and check all elements are present
使用来自@testing-library/react 的渲染通过传递适当的道具来渲染组件并检查所有元素是否存在
Here is the codesandbox - https://codesandbox.io/s/sad-merkle-lpnrw You can see test in test tab这是代码和框 - https://codesandbox.io/s/sad-merkle-lpnrw您可以在测试选项卡中看到测试
import React from "react";
import { render } from "@testing-library/react";
import { NestedLists } from "./App";
const filteredData = [
{
id: 1,
label: "List item 1",
parent_id: 0
},
{
id: 9,
label: "List item 9",
parent_id: 8
},
{
id: 8,
label: "List item 8",
parent_id: 1
},
{
id: 5,
label: "List item 5",
parent_id: 1
},
{
id: 6,
label: "List item 6",
parent_id: 1
},
{
id: 7,
label: "List item 7",
parent_id: 1
},
{
id: 10,
label: "List item 10",
parent_id: 8
},
{
id: 2,
label: "List item 2",
parent_id: 0
}
];
it("renders search screen without crash", () => {
const { getByText } = render(<NestedLists filteredData={filteredData} />);
expect(getByText("1")).toBeDefined();
expect(getByText("9")).toBeDefined();
expect(getByText("8")).toBeDefined();
expect(getByText("5")).toBeDefined();
expect(getByText("6")).toBeDefined();
expect(getByText("7")).toBeDefined();
expect(getByText("10")).toBeDefined();
expect(getByText("2")).toBeDefined();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.