我正在为我的一个组件编写测试用例,该组件具有路由器(使用 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/