【发布时间】:2023-01-21 13:03:12
【问题描述】:
我在 TypeScript 中有如下组件:
import React, {useState} from 'react';
import {App} from "../../../../interfaces/interfaces";
import {map, find, filter} from "lodash";
import NavigationItem from "./NavigationItem";
import {useLocation, useNavigate} from 'react-router-dom';
interface NavigationProps {
applications: App[],
companyName: string
}
const Navigation: React.FC<NavigationProps> = ({applications, companyName}: NavigationProps) => {
const [expanded, setExpanded] = useState<boolean>(false)
const location = useLocation();
const navigate = useNavigate();
const activeApplication = find(applications, application => application.url === location.pathname);
const inactiveApplications = filter(applications, application => application.url !== location.pathname)
return (
<div data-testid="navigation-component" className="flex h-full">
<div className="z-[2]">
<NavigationItem application={activeApplication}
companyName={companyName}
handleClick={() => setExpanded(expanded => !expanded)}
selected={expanded}
/>
</div>
<div data-testid="inactive-items" className={`flex transform transition ${expanded ? 'translate-x-0' : '-translate-x-full'}`}>
{map(inactiveApplications, application => {
return <NavigationItem key={application.id}
application={application}
companyName={companyName}
handleClick={() => {
setExpanded(false);
navigate(application.url);
}}
/>
})}
</div>
</div>
);
};
export default Navigation;
我想为它编写测试。我想测试click event。因此,我应该检查是否已单击活动导航项,以及是否更新了 expanded 状态。
下一个是(仅当 expanded 状态为 true 时),检查非活动元素上的点击事件并检查该点击事件是否将展开状态更新回 false 并导航到另一条路线。
我所拥有的如下:
import Header from "./Header";
import Navigation from "./components/Navigation";
import {MemoryRouter} from "react-router-dom";
import {describe, it} from "@jest/globals";
import {act, fireEvent, render, screen, waitFor} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
describe("<Navigation />", () => {
it("checks navigation clicks events", () => {
const applications = [
{id: 1, name: "Home", url: "/", classes: "bg-gray-100 text-blue-800"},
{id: 3, name: "Sales", url: "/sales", classes: "bg-red-500 text-white"},
{id: 2, name: "Expert", url: "/expert", classes: "bg-green-400 text-white"}
];
const companyName = 'My company';
render(<MemoryRouter initialEntries={["/"]}>
<Navigation applications={applications} companyName={companyName} />
</MemoryRouter>);
// Get the selected NavigationItem
const activeNavigationItem = screen.getByTestId(applications[0].name);
// Simulate click event
fireEvent.click(activeNavigationItem);
// Check if click updated the state, changed expanded to true AND maybe check if other inactive NavigationItems have translate-x-0 class
// Check if expanded is true, if it's true, then check if clicking one of the other NavigationsItems sets it to false and navigate to proper route
});
});
我不确定如何获取状态并检查它是否已更改。
【问题讨论】:
-
有意测试库abstracts from component's implementation details。您应该测试由更改组件状态引起的行为。
标签: reactjs typescript jestjs react-testing-library