gpt4 book ai didi

javascript - 如何测试返回函数值

转载 作者:行者123 更新时间:2023-12-02 23:11:41 26 4
gpt4 key购买 nike

如何测试此函数的返回值?

handleValidatePassword = (confirmPassword) => {
const { newPassword } = this.props;
if (newPassword !== confirmPassword) {
return <FormattedMessage id="some value" />;
}
}

如果条件正确,我需要检查此函数的返回值是否是预期的值:(FormattedMessage return span with value === id)

<span> some value </span>

我使用浅 enzyme 进行测试

最佳答案

如果你使用 Jest 或 Jasmine ,我个人会使用 enzyme 匹配器。 https://github.com/FormidableLabs/enzyme-matchers/blob/master/packages/jest-enzyme/README.md#tocontainreact

import { shallow } from 'enzyme' 
import Element from './yourpath'

const wrapper = shallow(<Element />); // mount/render/shallow when applicable

//as shallow renders only one level of children components
expect(wrapper).toContainReact(<FormattedMessage id="some value" />);

//or if you want to test actually the span element you should use mount
const wrapper2 = mount(<Element />);
expect(wrapper).toContainReact(<span> some value </span>);

关于javascript - 如何测试返回函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57326110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com