【发布时间】:2019-04-12 17:49:34
【问题描述】:
我正在学习玩笑,我正在尝试测试我的点击事件。我尝试测试的功能在组件内是私有的。
我尝试过使用 spyOn() 方法。
这是调用函数的组件
<NavbarToggler onClick={this.toggleNavbar} />
这是函数
private toggleNavbar = (): void => {
this.setState({
isOpen: !this.state.isOpen
});
}
这是测试
it('toggleNavbar is called when NavbarToggler is clicked', () => {
const wrapper = shallow(<NavBar />);
const instance = wrapper.instance();
jest.spyOn(instance, 'toggleNavbar');
wrapper.find(NavbarToggler).simulate('click');
expect(instance.toggleNavbar).toEqual(true);
});
我目前在 jest.spyOn 上收到一个错误,说 toggleNavbar 不能分配给参数
【问题讨论】:
标签: reactjs typescript jestjs