パラメータHOCコンポーネントに依存して返す関数があります。このコンポーネントを 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)