reactjs - React redux 表单字段数组单元测试失败
问题描述
我有一个字段数组组件,我在其中有条件地渲染一些组件。该组件如下所示:
export const UttakPeriod = ({
fields,
updatePeriode,
editPeriode,
cancelEditPeriode,
isAnyFormOpen,
isNyPeriodeFormOpen,
readOnly,
periods,
...other
}) => (
<div>
{fields.map((fieldId, index, field) => {
const period = field.get(index);
return (
<div key={fieldId}>
{other.meta.error && index === 0 ? <AlertStripe type="warning">{other.meta.error}</AlertStripe> : null}
<Row>
<Column>
<UttakPeriodType />
<UttakPeriodContent />
</Column>
{periods.length === fields.length &&
renderValidationGraphic(periods, index, index === (fields.length - 1))
}
</Row>
</div>
);
})}
<
/div>
);
我正在尝试测试 renderValidationGraphic 函数呈现的内容,如下所示:
const renderValidationGraphic = (periods, index, isLastIndex) => {
if (!isLastIndex) {
const period= periods[index];
const nextPeriod = periods[index + 1];
const diff = calcDays(period.tom, nextPeriod .fom, 'YYYY-MM-DD');
if (moment(period.tom) >= moment(nextPeriod .fom)) {
return (
<div className={styles.periodIconWrapper}>
<Image src={overlapp} alt="Periods overlap" />
</div>
);
}
}
return null;
};
我正在尝试这样测试:
const getMockedFields = (fieldNames, periods) => {
const field = {
get: idx => periods[idx],
};
return {
map: callback => fieldNames.map((fieldname, idx) => callback(fieldname, idx, field)),
};
};
const fieldNames = ['periods[0]', 'periods[1]'];
const periods = [{
fom: '2017-10-01',
id: '2017-10-01 | 2017-10-10',
openForm: false,
tom: '2017-10-10',
updated: false,
}, {
fom: '2017-10-09',
id: '2017-10-09 | 2017-10-17',
openForm: true,
tom: '2017-10-17',
updated: false,
}];
const updatePeriode = sinon.spy();
const editPeriode = sinon.spy();
const cancelEditPeriode = sinon.spy();
const isAnyFormOpen = sinon.spy();
describe('<UttakPeriod>', () => {
it('should render image for overlapping periods', () => {
const wrapper = shallowWithIntl(<UttakPeriod
fields={getMockedFields(fieldNames, periods)}
updatePeriode={updatePeriode}
editPeriode={editPeriode}
cancelEditPeriode={cancelEditPeriode}
isAnyFormOpen={isAnyFormOpen}
periods={periods}
meta={meta}
readOnly
/>);
const image = wrapper.find(Image);
expect(image).to.have.length(1);
expect(image.prop('alt')).is.equal('Periods overlap');
});
它在现实生活中有效,如果周期重叠,我会得到图像。该功能可以正常renderValidationGraphic
工作,可以在此处看到,但由于某种原因测试未通过。
AssertionError:预期 { 长度:0 } 的长度为 1 但得到 0
那么为什么这个测试会失败,我该如何修复这个测试以使其正常工作?
解决方案
推荐阅读
- amazon-web-services - 更改 AWS 登陆区、控制塔上审计账户的电子邮件地址
- java - 使用速记语法将缺失值传递到列表中的预期方法是什么
- reactjs - 在没有反应应用程序的情况下使用 Draft.js 显示“未定义要求”错误
- python - RuntimeWarning:从未等待协程“MessageMethods.send_message”
- amazon-web-services - AWS 中匿名用户错误的访问策略修复?
- tensorflow - 如何为具有不同时间步长值的 LSTM 模型训练准备训练数据集。我的数据集如下所示
- blazor - Blazor 独立 WASM - 无法获取角色
- docker-image - 为 shell 脚本构建 Docker 镜像
- graphdb - ElasticSearch 连接器 - 属性链中的反向 IRI
- html - 检查所有单词是否以正则表达式的特定字符开头