gpt4 book ai didi

reactjs - TypeError : wrapper. 存在不是函数 && TypeError : wrapper. find 不是函数

转载 作者:行者123 更新时间:2023-11-28 21:37:07 27 4
gpt4 key购买 nike

我正在为我的一个组件编写测试用例,该组件具有路由器(使用 withrouter)。我收到错误 wrapper.find is not a function。基本要求是需要检查我的渲染中是否存在标签,还要检查标签的 aria-label 属性

常量组件 = () => const WrappedComponent = withRouter(组件)

  describe('Error Boundary...', () => {
it('should render children when no error is encountered', () => {
const wrapper = ReactTestRenderer.create(
<Router><WrappedComponent /></Router>
);
expect(wrapper.toJSON().type).toEqual('div');
expect(wrapper.toJSON()find('a').attr('aria-label')).toEqual('Hello World');
});
});

最佳答案

您不应该在期望行中调用 .toJSON()

 describe('Error Boundary...', () => {
it('should render children when no error is encountered', () => {
const wrapper = ReactTestRenderer.create(
<Router><WrappedComponent /></Router>
);
expect(wrapper.root.type).toEqual('div');
expect(wrapper.root.find('a').attr('aria-label')).toEqual('Hello World');
});
});

关于reactjs - TypeError : wrapper. 存在不是函数 && TypeError : wrapper. find 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305080/

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