[英]Test conditionals and useEffect in React with Jest
I need to write a test with the following steps:我需要使用以下步骤编写测试:
And the component looks like this:该组件如下所示:
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { getUserData } from "../../firebase/user";
import { selectProject } from "../../actions/projects";
import { getItem } from "../../tools/localStorage";
import { getProjectDetails } from "../../firebase/projects";
import { selectPages } from "../../actions/pages";
import Pages from "../Pages";
import Projects from "../Projects";
import Artboard from "../Artboard";
import Switch from "../Transitions/Switch";
import { getUserId, getClientId } from "../../selectors/user";
import { getSelectedProject } from "../../selectors/projects";
import { getSelectedPages, getPagesWithDetails } from "../../selectors/pages";
import { getPagesDetails } from "../../firebase/pages";
const cachedProject = JSON.parse(getItem("selectedProject"));
const cachedPages = JSON.parse(getItem("selectedPages"));
const Dashboard = () => {
const dispatch = useDispatch();
const userId = useSelector(getUserId);
const clientId = useSelector(getClientId);
const selectedProject = useSelector(getSelectedProject) || cachedProject;
const selectedPages = useSelector(getSelectedPages) || cachedPages;
const pagesWithDetails = useSelector(getPagesWithDetails);
useEffect(() => {
dispatch(
getUserData(userId)
);
cachedProject && selectProject(cachedProject);
cachedPages && selectPages(cachedPages);
}, []);
useEffect(() => {
if (selectedProject && clientId) {
dispatch(
getProjectDetails(
clientId,
selectedProject
)
);
}
}, [selectedProject, clientId]);
useEffect(() => {
if (selectedPages && selectedProject && clientId) {
const pagesWithoutDetails = selectedPages.filter(pageId => (
!Object.keys(pagesWithDetails).includes(pageId)
));
dispatch(
getPagesDetails(
selectedProject,
pagesWithoutDetails
)
);
}
}, [selectedPages, selectedProject, clientId]);
const Content = () => {
if (!clientId) return null;
if (!selectedProject) {
return <Projects key="projects" />;
}
if (!selectedPages) {
return <Pages key="pages" />;
}
return <Artboard key="artboard" />;
};
console.log("Update Dashboard")
return (
<Switch>
{Content()}
</Switch>
);
};
Where I use some functions to fetch data from firebase, some to dispatch actions, and some conditionals.我使用一些函数从 firebase 获取数据,一些用于调度操作,还有一些条件。
I'm trying to get deep into testing with Jest and Enzyme.我正在尝试使用 Jest 和 Enzyme 进行深入测试。 When I was searching for testing approaches, testing useEffect, variables, and conditions, I haven't found anything.
当我在寻找测试方法、测试 useEffect、变量和条件时,我什么也没找到。 All I saw is testing if a text changes, if a button has get clicked, etc. but what about testing components which aren't really changing anything in the DOM, just loading data, and depending on that data, renders a component?
我所看到的只是测试文本是否更改、按钮是否被单击等,但是测试那些并没有真正改变 DOM 中的任何内容、只是加载数据并根据该数据呈现组件的组件呢?
What's the question here?这里有什么问题? What have you tried?
你试过什么? To me it seems pretty straightforward to test:
对我来说,测试似乎很简单:
mount
or shallow
to render the component and assign that to a variable and wrap it in a store provider so it has access to a redux store.mount
或shallow
渲染组件并将其分配给一个变量并将其包装在存储提供程序中,以便它可以访问 redux 存储。jest.mock
to mock things you don't want to actually want to happen (like the dispatching of actions) or use something like redux-mock-store .jest.mock
来模拟你不想发生的事情(比如动作的调度)或使用类似redux-mock-store 的东西。 Let me know if you have more specific issues.如果您有更具体的问题,请告诉我。 Good luck!
祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.