简体   繁体   English

在 React with Jest 中测试条件和 useEffect

[英]Test conditionals and useEffect in React with Jest

I need to write a test with the following steps:我需要使用以下步骤编写测试:

  1. get user data on mount获取挂载用户数据
  2. get project details if it has selectedProject and clientId when they change获取项目详细信息,如果它有 selectedProject 和 clientId 更改时
  3. get pages details if it has selectedProject, clientId, and selectedPages when they change获取页面详细信息,如果它有 selectedProject、clientId 和 selectedPages 更改时
  4. render Content inside Switch在 Switch 内渲染内容
  5. if doesn't have clientId, Content should return null如果没有 clientId,内容应该返回 null
  6. if doesn't have selectedProject, Content should return Projects如果没有选择项目,内容应该返回项目
  7. if doesn't have selectedPages, Content should return Pages如果没有 selectedPages,Content 应该返回 Pages
  8. else Content should render Artboard否则内容应呈现画板

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:对我来说,测试似乎很简单:

  • Use Enzymes 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.使用 Enzymes mountshallow渲染组件并将其分配给一个变量并将其包装在存储提供程序中,以便它可以访问 redux 存储。
  • Use 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 的东西。
  • Use that component ".find" to get the actual button you want.使用该组件“.find”来获取您想要的实际按钮。
  • Assert that, given a specific redux state, it renders correctly.断言,给定特定的 redux state,它可以正确呈现。
  • Assert that actions are dispatched with the proper type and payload at the proper times.断言在适当的时间使用适当的类型和有效负载分派操作。
  • You may need to call component.update() to force it to rerender within the enzyme test.您可能需要调用 component.update() 来强制它在酶测试中重新呈现。

Let me know if you have more specific issues.如果您有更具体的问题,请告诉我。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM