reactjs - 酶测试与上下文 api 反应
问题描述
我正在尝试测试表格。提交表单时,它应该设置一个状态error: true
,然后div
应该出现一个错误信息。我的测试如下所示:
outer = shallow(<Search />);
it("should not submit a form and display an error if title or author is empty", () => {
const Children = outer.props().children({});
const wrapper = mount(Children);
const button = wrapper.find("form button");
button.simulate("submit", {
preventDefault() {
outer.setState({ error: true });
}
});
expect(wrapper.find("div.error")).toHaveLength(1);
});
不幸的是,它不起作用。我是单元测试的新手,我不知道我是否做对了,我应该如何解决这个问题。
我想我也应该以某种方式获得输入值,但不知道如何。
解决方案
这是为输入元素设置值的示例:
it('Should capture firstname correctly onChange', function(){
const component = mount(<Form />);
const input = component.find('input').at(0);
input.instance().value = 'hello';
input.simulate('change');
expect(component.state().firstname).toEqual('hello');
})
但由于其他原因可能无法正常工作,请确保您已在beforeAll()
. 尝试阅读有关此主题的酶示例。
推荐阅读
- javascript - 清除 HTML 画布
- android - 协程中 rxJava onNext 和 onError 的等价物是什么
- python - 来自 IP 摄像机的 Python OpenCV VideoCapturing 问题..error:(-5:Bad argument) CAP_IMAGES: 找不到起始编号
- python - Python paramiko 库 - 将文件从本地服务器放到远程服务器(CSV 文件)
- html - 移动导航栏对象
- julia - 将 ForwardDiff.jl 用于包含许多变量和参数的函数 Julia
- clojure - Clojure:我正在尝试使用“some”而不是“doseq”,但我不确定如何使用它
- python - 根据数组对矩阵的行进行排序
- json - 无后端 + Zapier。建立对外关系记录
- colors - 使用引导程序更改导航栏颜色