在渲染所有组件后,如何从表中获取元素?
我正在尝试测试我在React中的Userstable页面,但是测试库在有时间渲染之前获得了元素。 请在下面查看我的代码:
userstable.js
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import Table from 'react-bootstrap/Table';
import { fetchUsers } from 'api';
function UsersTable() {
const [users, setUsers] = useState([]);
useEffect(() => {
const getUsers = async () => {
const data = await fetchUsers();
setUsers(data);
};
getUsers();
}, []);
const renderUsers = () => users.map((user) => {
const { id, fullName, email } = user;
return (
<tr key={ id } >
<td>
<Link to={ `user/${id}` }>{ fullName }</Link>
</td>
<td>
<Link to={ `user/${id}/mail` }>{ email }</Link>
</td>
</tr>
);
});
return (
<div data-testid='userTest' className="users-table">
<h1>Users</h1>
<Table striped borderless>
<thead>
<tr style={{
borderBottom: "1px solid #cccccc"
}}>
<th>User</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{ renderUsers() }
</tbody>
</Table>
</div>
);
}
export default UsersTable;
usertable.test.js
import { render } from '@testing-library/react'
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import '@testing-library/jest-dom/extend-expect';
import UserTable from '../components/users/UserTable';
test("Test #1: Count Users", async () => {
const component = render(
<Router>
<UserTable />
</Router>
);
const users = component.getByTestId('userTest')
expect(users.querySelectorAll('tr').length).toBe(193)
})
note :&lt; router&gt;
预防错误错误:未介绍[错误:不变失败:您不应使用&lt; link&gt;外部A&lt; router&gt;
期望:期望有192个用户 + 1行包含thead
,但是我仅获得1行包含thead
。
I'm trying to test my UsersTable page in React, but the testing library gets the elements before they have time to render.
Please check my code below:
UsersTable.js
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import Table from 'react-bootstrap/Table';
import { fetchUsers } from 'api';
function UsersTable() {
const [users, setUsers] = useState([]);
useEffect(() => {
const getUsers = async () => {
const data = await fetchUsers();
setUsers(data);
};
getUsers();
}, []);
const renderUsers = () => users.map((user) => {
const { id, fullName, email } = user;
return (
<tr key={ id } >
<td>
<Link to={ `user/${id}` }>{ fullName }</Link>
</td>
<td>
<Link to={ `user/${id}/mail` }>{ email }</Link>
</td>
</tr>
);
});
return (
<div data-testid='userTest' className="users-table">
<h1>Users</h1>
<Table striped borderless>
<thead>
<tr style={{
borderBottom: "1px solid #cccccc"
}}>
<th>User</th>
<th>Email</th>
</tr>
</thead>
<tbody>
{ renderUsers() }
</tbody>
</Table>
</div>
);
}
export default UsersTable;
userTable.test.js
import { render } from '@testing-library/react'
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import '@testing-library/jest-dom/extend-expect';
import UserTable from '../components/users/UserTable';
test("Test #1: Count Users", async () => {
const component = render(
<Router>
<UserTable />
</Router>
);
const users = component.getByTestId('userTest')
expect(users.querySelectorAll('tr').length).toBe(193)
})
Note: <Router>
prevents the error Error: Uncaught [Error: Invariant failed: You should not use <Link> outside a <Router>
Expect: Expecting to have 192 users + 1 row containing thead
, but instead I get 1 row containing only the thead
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于您的测试正在尝试访问
useffect
hook userstable 已完成获取和渲染数据之前。您可以使用reacts
@testing-library
'sfindby
或waitfor
方法等待组件异步渲染组件数据。这将保证在数据可用之前避免异步渲染并避免查询。
The issue Is that your test is trying to access the DOM elements before the
useEffect
hook inUsersTable
has completed fetching and rendering the data.You can use reacts
@testing-library
'sfindBy
orwaitFor
methods to wait for the components to render your component data asynchronously.This will guarantee asynchronous rendering and avoid querying before the data is available.