Unit testing 使用Jest&;测试switch语句;酶

Unit testing 使用Jest&;测试switch语句;酶,unit-testing,testing,switch-statement,jestjs,Unit Testing,Testing,Switch Statement,Jestjs,我正在使用switch语句有条件地呈现组件,我已经花了相当长的时间尝试测试,但我只是不知道如何去做,也没有找到任何有用的在线资源。谢谢你看 componentorender=(当前路径)=>{ const{years,tours,歌曲,表演,场地}=this.props; 开关(电流路径){ 案例“/年”: 返回年份。地图(年份=>); 案例“/旅游”: 回程旅游。地图(旅游=>); 案例“/歌曲”: 返回歌曲.map(歌曲=>); 案例“/场馆”: 返回场馆。地图(场馆=>); 案例“/显示”

我正在使用switch语句有条件地呈现组件,我已经花了相当长的时间尝试测试,但我只是不知道如何去做,也没有找到任何有用的在线资源。谢谢你看

componentorender=(当前路径)=>{
const{years,tours,歌曲,表演,场地}=this.props;
开关(电流路径){
案例“/年”:
返回年份。地图(年份=>);
案例“/旅游”:
回程旅游。地图(旅游=>);
案例“/歌曲”:
返回歌曲.map(歌曲=>);
案例“/场馆”:
返回场馆。地图(场馆=>);
案例“/显示”:
返回shows.map(show=>);
案例“/设置列表”:
返回;
案例“/UserStats”:
返回;
违约:
返回;
}
};
这里有一个解决方案:

index.tsx

import React,{Component}来自'React';
常数年=({key,year})=>(
{key},{year}
);
const Tours=({key,tour})=>(
{key},{tour}
);
常量歌曲=({key,song})=>(
{key},{song}
);
康斯特场馆=({key,场馆})=>(
{钥匙},{地点}
);
常量Shows=({key,show})=>(
{key},{show}
);
常量集合列表=()=>集合列表;
constuserstats=()=>UserStats;
const HomePage=()=>主页;
导出接口IXComponentProps{
年份:任何[];
旅游:任何[];
歌曲:任何[];
显示:任何[];
地点:任何[];
currentPath:string;
}
导出类XComponent扩展组件{
建造师(道具){
超级(道具);
}
public componentorender=currentPath=>{
const{years,tours,歌曲,表演,场地}=this.props;
开关(电流路径){
案例“/年”:
返回年份。地图(年份=>);
案例“/Tours”:
回程旅游。地图(旅游=>);
案例“/歌曲”:
返回歌曲.map(歌曲=>);
案例“/场馆”:
返回场馆。地图(场馆=>);
案例“/显示”:
返回shows.map(show=>);
案例“/SetList”:
返回;
案例“/UserStats”:
返回;
违约:
返回;
}
}
公共渲染(){
const{currentPath}=this.props;
返回此.componentorender(currentPath);
}
}
index.spec.tsx

从“React”导入React;
从“酶”中导入{浅,浅Rapper};
从“/”导入{XComponent,IXComponentProps};
描述('XComponent',()=>{
让包装:浅层振打器;
常量mockedProps:IXComponentProps={
年份:[{日期:'2019-01-01'}],
旅游:[{id:'1'}],
歌曲:[{id:'2'}],
显示:[{id:'3'}],
地点:[{id:'4'}],
当前路径:“”
};
在每个之前(()=>{
包装器=浅();
});
每个都是`
currentPath |组件接收器
${'/'}|${'HomePage'}
${'/UserStats'}|${'UserStats'}
${'/SetList'}|${'SetList'}
${'/Shows'}|${'Shows'}
${/VICENTES'}|${'VICENTES'}
${'/Songs'}|${'Songs'}
${'/Tours'}|${'Tours'}
${'/Years'}|${'Years'}
`(
'应按当前路径正确呈现$componentorender组件$currentPath',
({currentPath,componentToRender})=>{
setProps({currentPath});
expect(wrapper.find(componentToRender)).toHaveLength(1);
}
);
});
单元测试结果和覆盖率报告:

通过src/stackoverflow/56453372/index.spec.tsx(7.661s)
XComponent
✓ 应按当前路径/正确地呈现主页组件(19ms)
✓ 应按当前路径/UserStats正确呈现UserStats组件(1ms)
✓ 应按当前路径/设置列表正确呈现设置列表组件(2ms)
✓ 应按当前路径/正确显示渲染显示组件(1ms)
✓ 应按当前路径/场馆正确渲染场馆组件(1ms)
✓ 应按当前路径/歌曲正确渲染歌曲组件(2ms)
✓ 应按当前路径/路线正确渲染路线组件(1ms)
✓ 应按当前路径/年正确呈现年组件(1ms)
-----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
-----------|----------|----------|----------|----------|-------------------|
所有文件| 67.86 | 100 | 52.94 | 100 ||
指数.tsx | 67.86 | 100 | 52.94 | 100 ||
-----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:8项通过,共8项
快照:共0个
时间:9.53秒
HTML覆盖率报告:

以下是完整的演示:

以下是解决方案:

index.tsx

import React,{Component}来自'React';
常数年=({key,year})=>(
{key},{year}
);
const Tours=({key,tour})=>(
{key},{tour}
);
常量歌曲=({key,song})=>(
{key},{song}
);
康斯特场馆=({key,场馆})=>(
{钥匙},{地点}
);
常量Shows=({key,show})=>(
{key},{show}
);
常量集合列表=()=>集合列表;
constuserstats=()=>UserStats;
const HomePage=()=>主页;
导出接口IXComponentProps{
年份:任何[];
旅游:任何[];
歌曲:任何[];
显示:任何[];
地点:任何[];
currentPath:string;
}
导出类XComponent扩展组件{
建造师(道具){
超级(道具);
}
public componentorender=currentPath=>{
const{years,tours,歌曲,表演,场地}=this.props;
开关(电流路径){
案例“/年”:
返回年份。地图(年份=>);
案例“/Tours”:
回程旅游。地图(旅游=>);
案例“/歌曲”:
返回歌曲.map(歌曲=>);
案例“/场馆”:
返回场馆。地图(场馆=>);
案例“/显示”:
返回shows.map(show=>);
案例“/SetList”:
返回;
case'/UserStat