我有一个返回的函数取决于参数HOC componetns。我把这个组件改编成reduxForm HOC。我想用一种使用type()的酶测试它。该函数返回类型为FormValues的HOC组件,但如何在没有HOC的情况下返回测试。如何使用proxyquire进行测试
功能
export const getMiConfiguration = miConfigurationType => {
switch (miConfigurationType) {
case MiConfigurationTypes.WanderingDetection :
case MiConfigurationTypes.MuteWanderingDetection:
return <WanderingDetection />
case MiConfigurationTypes.OpenWanderingControl :
case MiConfigurationTypes.LockedWanderingControl:
return <WanderingControl />
default:
return null
}
}
测试
describe('getMiConfiguration', () => {
;[{id: MiConfigurationTypes.AccessPointOnly, component: null},
{id: MiConfigurationTypes.WanderingDetection, component: <WanderingDetection/>},
{id: MiConfigurationTypes.MuteWanderingDetection, component: <WanderingDetection/>},
{id: MiConfigurationTypes.LockedWanderingControl, component: <WanderingControl/>},
{id: MiConfigurationTypes.OpenWanderingControl, component: <WanderingControl/>},
].forEach(({id, component}) =>
it(`should render correct ${component} component for ${id} type`, () => {
const result = getMiConfiguration(id)
if (component === null)
expect(result).to.be.null
else
result.type.should.be.equal(component.type)
}))
})
组件的示例
export const WanderingDetection = ({miConfigurationType}) =>
<Grid container>
<Grid item xs={12}>
<GeneralMiConfigurations />
</Grid>
{MiConfigurationTypes.MuteWanderingDetection === miConfigurationType &&
[
<Grid item xs={10} style={{margin: '0 auto'}}>
<Divider/>
</Grid>,
<Grid item xs={12} style={{alignSelf: 'center'}}>
<InfoMessage id='passage.label.useInputToMuteLocationField'/>
<InputTypeConfiguration/>
</Grid>,
]
}
</Grid>
WanderingDetection.propTypes = {
miConfigurationType: PropTypes.string,
}
export default formValues('miConfigurationType')(WanderingDetection)
我找到了使用proxyquire的解决方案来覆盖导出
const {getMiConfiguration} = proxyquire('../../../../src/components/devices/miConfiguration', {
'./wanderingDetection': {default: <WanderingDetection/>},
'./wanderingControl': {default: <WanderingControl/>},
})