reactjs - 如何在反应中测试或从状态中获取价值?
问题描述
嗨,你能告诉我如何在反应中测试或从状态中获取价值吗?收到错误 wrapper.instance(...).handleClickShowPassword is not a function
这是我的代码 https://codesandbox.io/s/l2lk4n794l
it("toggle showpassword value", () => {
wrapper.setState({ showPassword: false });
wrapper.instance().handleClickShowPassword();
expect(wrapper.state.showPassword).toEqual(true);
});
解决方案
由于LoginContainer
是使用 HOC 包装的,因此您要么需要在没有withStyles
HOC的情况下导出组件,要么dive
在包装器上使用来获取组件的实例。也是state
一个函数component instance
,因此您需要调用它来访问状态
describe("<LoginContainer/>", () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<LoginContainer />);
});
it("toggle showpassword value", () => {
const comp = wrapper.dive();
comp.dive().setState({ showPassword: false });
comp.instance().handleClickShowPassword();
expect(comp.state("showPassword")).toEqual(true);
});
});
推荐阅读
- python - 使用包含带有 span 元素的文本时的 Python selenium 问题
- node.js - Cloud Functions, Firebase: parsing function triggers error
- javascript - 使用 Express-Handlebars/NodeJs 构建 DOM 递归
- vagrant - 在 vagrant 上运行时无法浏览 consul ui(来自 consul 教程)
- r - 如何在 r 中缩放 ggplot2 中 x 轴的时间(小时)?
- java - jetbrains.exodus.ExodusException:0 毫秒后无法获取环境锁
- php - 如何在php中显示项目的数量
- ansible - Ansible:当我禁用防火墙时,总是显示状态改变
- javascript - Queryselector 删除多余的逗号分隔的项目列表
- spring-boot - 为 application.properties 文件赋值 Spring boot