Reactjs 如何测试作为props jest JS传递的字符串

Reactjs 如何测试作为props jest JS传递的字符串,reactjs,jestjs,enzyme,Reactjs,Jestjs,Enzyme,我想测试将在段落元素中显示的值。因此,为了做到这一点,我想在jest测试中将该值作为参数传递,然后检查该段落是否包含相同的值。当我渲染组件和控制台时,将其注销(查看下图)。我将得到一个对象列表。但是我如何才能访问正确的数据testid并从中获取内容,以查看它是否与props值匹配 如果有更干净的方法,那么也让我知道 //SelectedValues.test.js 它('should call check props value in SelectedValue component',()=>

我想测试将在段落元素中显示的值。因此,为了做到这一点,我想在jest测试中将该值作为参数传递,然后检查该段落是否包含相同的值。当我渲染组件和控制台时,将其注销(查看下图)。我将得到一个对象列表。但是我如何才能访问正确的数据testid并从中获取内容,以查看它是否与props值匹配

如果有更干净的方法,那么也让我知道

//SelectedValues.test.js
它('should call check props value in SelectedValue component',()=>{
const vehicleProps=['汽车']
const brandProps=['特斯拉']
const colorProps=['红色']
const component=render();
console.log(component.find('p'))
});
//SelectedValues.js
从“React”导入React;
导入{
头颅,
段落
ValueContainer,
值块
}从“../../styles/SelectOptions”开始;
导出常量SelectedValues=({
驾驶车辆,
品牌,,
彩色
}) => {
返回(
选定车辆
{车辆}
精选品牌
{BRAND}
选定颜色
{COLOR}
)
}
Ciao,一个工作示例(我使用了一个通用的
TestComponent
,而不是
段落
span
,但它是相同的)

应用于组件的示例可能类似于:

it('should call check props value in SelectedValue component', () => {
   const vehicleProps = [ 'car' ]
   const brandProps = [ 'tesla' ]
   const colorProps = [ 'red' ]
   const component = shallow(<SelectedValues curSelectVehicle={vehicleProps} curSelectBrand={brandProps} curSelectColor={colorProps}/>);

   const vehicle = component.findWhere(
      n => n.prop("data-testid") === "select-vehicle"
   );
   const brand = component.findWhere(
      n => n.prop("data-testid") === "select-brand"
   );
   const color = component.findWhere(
      n => n.prop("data-testid") === "select-color"
   );
   expect(vehicle.text()).toBe(vehicleProps[0]);    
   expect(brand.text()).toBe(brandProps[0]);
   expect(color.text()).toBe(colorProps[0]);
});
});
it('should call check props value in SelectedValue component',()=>{
const vehicleProps=['汽车']
const brandProps=['特斯拉']
const colorProps=['红色']
常量分量=浅();
const vehicle=component.findWhere(
n=>n.prop(“数据测试”)=“选择车辆”
);
const brand=component.findWhere(
n=>n.prop(“数据测试”)==“选择品牌”
);
const color=component.findWhere(
n=>n.prop(“数据测试ID”)==“选择颜色”
);
expect(vehicle.text()).toBe(vehicleProps[0]);
expect(brand.text()).toBe(brandProps[0]);
expect(color.text()).toBe(colorProps[0]);
});
});

注意:我使用了
shallow
,而不是
render
,在
component
中调用
findWhere
,一个问题:为什么要将道具作为数组传递?我的意思是为什么
const vehicleProps=['car']
而不是
'car'
?是的,这是一个jest testexpect(component.prop('name'))。toEqual('Moe')和component.setProps({name:'Moe'))这也和你上面做的类似吗