Jest.js 在useEffect中更新后React测试状态值

owfi6suc  于 2023-02-17  发布在  Jest
关注(0)|答案(1)|浏览(165)

我需要测试useEffect后的状态更改
FooView.jsx文件:

const [total, setTotal] = useState(0);

useEffect(() => {
    calculatedTotal = calculateTotal(page.paymentSchedule);
    setTotal(calculatedTotal);
}, [
    page.paymentSchedule,
]);

FooView-测试.jsx:

describe('...', () => {
    const paymentSchedule = [
        {
          count: 2,
          amount: 100,
        },
        {
          count: 3,
          amount: 200,
        }
    ];

    it('should update total when the payment schedule changes', () => {
        const container = mount(<FooView />);
        container.find('FooView').prop('paymentSchedule')(paymentSchedule);
        // what to do next
    });
}

我使用了Jest和Enzyme,如何测试得到的状态值?

vcirk6k6

vcirk6k61#

您需要设置另一个UseEffect,如下所示:

useEffect(() => {
    console.log(total)
}, [total]);

这样,通过setTotaltotal所做的每一次更改都将返回到控制台中

相关问题