我正在使用 HOC在一个看起来像这样的 React 组件中:
import React from 'react';
import Wrapper from 'wrapper';
class Component extends React.Component {
render () {
return (
<div className='component' />
)
};
}
export default Wrapper(Component)
当使用 Mocha 测试组件时,我试图寻找一个应该包含在组件中的类名。像这样:
describe('Component', function () {
it('can be mounted with the required class', function () {
const component = shallow(
<Component />
);
expect(component).to.have.className('component');
});
});
问题是 Mocha 不知道在组件的包装器中查找并尝试在 HOC 中找到它。当然不会。
我收到的错误是:
AssertionError: expected <Wrapper(Component) /> to have a 'component' class, but it has undefined
HTML:
<div class="component">
</div>
我如何告诉 Mocha 在 HOC 中查找类名的正确位置而不是 HOC 本身?
最佳答案
您可以使用 enzyme .dive()
const component = shallow(<Component />).dive();
expect(component.props().className).toEqual('component')
关于testing - 在 React 中使用 Mocha 测试高阶组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106763/