using jest and enzyme testing useEffect and useState

无人久伴 提交于 2020-01-05 04:17:12

问题


How can I test the useEffect for one time call, so I am loading a number of links on mount. so it will be called only once

see the code

app.js

import React, { useState, useEffect } from "react";

const App = () => {
    const [links, setLinks] = useState([]);

    useEffect(() => {
        const links = createLinks();
        setLinks(links);
    }, []);

    const createLinks = () => {
        return [{ link: "https://www.google.com", text: "Google" }];
    };

    return <>{links.length && links.map(({ link, text }) => <a href={link}>{text}</a>)}</>;
};


export default App

how can i write a test case for the above sample app component using jest and enzyme

来源:https://stackoverflow.com/questions/59169619/using-jest-and-enzyme-testing-useeffect-and-usestate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!