在jest中找到组件的道具可以通过以下步骤实现:
Component.test.js
,或者在已有的测试文件中添加相关测试代码。shallow
函数来渲染组件,例如:import { shallow } from 'enzyme';
import MyComponent from './MyComponent';
describe('MyComponent', () => {
it('should render correctly', () => {
const wrapper = shallow(<MyComponent />);
// 组件渲染测试代码
});
});
props()
方法来获取组件的道具,例如:it('should pass props correctly', () => {
const props = {
prop1: 'value1',
prop2: 'value2',
};
const wrapper = shallow(<MyComponent {...props} />);
expect(wrapper.props().prop1).toEqual('value1');
expect(wrapper.props().prop2).toEqual('value2');
});
connect
函数与Redux连接的,可以使用dive()
方法来获取被连接的组件的道具,例如:import { shallow } from 'enzyme';
import { MyComponent } from './MyComponent';
describe('MyComponent', () => {
it('should pass props correctly', () => {
const props = {
prop1: 'value1',
prop2: 'value2',
};
const wrapper = shallow(<MyComponent {...props} />);
const connectedComponent = wrapper.dive(); // 获取被连接的组件
expect(connectedComponent.props().prop1).toEqual('value1');
expect(connectedComponent.props().prop2).toEqual('value2');
});
});
通过以上步骤,你可以在jest中找到组件的道具,并进行相应的测试。请注意,以上示例中使用了enzyme库来辅助测试,你需要确保已经安装并配置了enzyme。另外,根据你的具体项目和组件结构,可能需要进行适当的调整和修改。
领取专属 10元无门槛券
手把手带您无忧上云