Reactjs 安装时,应对路由器v4参数在单元测试中出现中断

Reactjs 安装时,应对路由器v4参数在单元测试中出现中断,reactjs,enzyme,jestjs,Reactjs,Enzyme,Jestjs,我有一个包含URL参数的React组件。当我运行测试并挂载组件时,参数总是未定义的,因此,中断测试。我试图将它们硬编码为常量和道具,但仍然无法工作。我还有别的想法可以试试吗 import React, { Component } from 'react'; class BarcodePage extends Component { constructor(props) { super(props); } componentDidMount() {

我有一个包含URL参数的React组件。当我运行测试并挂载组件时,参数总是未定义的,因此,中断测试。我试图将它们硬编码为常量和道具,但仍然无法工作。我还有别的想法可以试试吗

import React, { Component } from 'react';

class BarcodePage extends Component {
    constructor(props) {
        super(props);
    }
    componentDidMount() {
        const { SKU, ID } = this.props.match.params
    }
    render() {
        return (
            <h1>Barcode view {SKU} {ID}</h1>
        );
    }
}

export default BarcodePage;


import React from 'react';
import { shallow } from 'enzyme';
import BarcodePage from './BarcodePage';

const component = mount(
  <BarcodePage params={{SKU: '1111', ID: '2121212' }} />
);

describe('<BarcodePage />', () => {
  it('render one header', () => {
    expect(component.find('h1').length).toBe(1);
  });
})
import React,{Component}来自'React';
类BarcodePage扩展组件{
建造师(道具){
超级(道具);
}
componentDidMount(){
const{SKU,ID}=this.props.match.params
}
render(){
返回(
条形码视图{SKU}{ID}
);
}
}
导出默认条形码页;
从“React”导入React;
从“酶”导入{shall};
从“./BarcodePage”导入条形码页;
常量组件=挂载(
);
描述(“”,()=>{
它('呈现一个标题',()=>{
expect(component.find('h1').length).toBe(1);
});
})

React路由器提供并且您的代码使用
this.props.match.params
,而不是
this.props.params
。您将错误的道具传递到单元测试:

<BarcodePage params={{SKU: '1111', ID: '2121212' }} />

非常感谢。最小的小东西让我头疼。
<BarcodePage match={{params: {SKU: '1111', ID: '2121212' }}} />