javascript - 如何用玩笑/酶模拟反应参考?
问题描述
我开始学习应用程序测试,我需要测试功能。我有一些组件:
export class Countries extends React.Component<Props, State> {
private countriesList: React.RefObject<HTMLDivElement> = React.createRef<
HTMLDivElement
>();
public componentDidMount(): void {
setTimeout(this.whenCDM, 1);
}
public render(): React.ReactNode {
return (
<div ref={this.countriesList}>
</div>
);
}
private whenCDM = (): any => {
if (this.countriesList.current) {
this.whenComponentDidMount(
this.countriesList.current.getBoundingClientRect().top
);
}
};
}
我想测试名为 whenCDM 的函数,但我不知道该怎么做。
解决方案
我终于找到了答案。我只是不明白什么是“模拟”。
这是我的问题的答案:
第一的。需要小的重构功能。
private whenCDM = (countriesList:any): any => {
if (countriesList.current !== null) {
this.whenComponentDidMount(
countriesList.current.getBoundingClientRect().top
);
}
};
然后在 cDM 中:
public componentDidMount(): void {
setTimeout(this.whenCDM(this.countriesList), 1);
}
然后在测试文件中创建模拟函数: 我想,我可以在 getBoundingClientRect 中设置只有顶级选项,但无论如何......
// ...code
it("whenCDM", () => {
const getCountriesListRef = () => {
return {
current: {
getBoundingClientRect: () => {
return {
bottom: 624,
height: 54,
left: 15,
right: 360,
top: 570,
width: 345,
x: 15,
y: 570
};
}
}
};
};
const instance = wrapper.instance();
expect(instance.whenCDM(getCountriesListRef()));
});
// ...code
推荐阅读
- snowflake-cloud-data-platform - 带有别名的子查询失败,标识符为“T1.C0”
- css - Bootstrap如何摆脱警报组件中的边距
- mysql - Mysql中无法连接到127.0.0.1:3306
- android - 尝试在 Android Studio 中使用矢量 Drawable 时出现异常
- webpack - browserSync 突然在将其脚本注入自定义域时遇到问题,并且对于 localhost (webpack-mix)
- sas - 尝试在 Linux 上使用 SAS 从(NFS)文件夹中读取时,“文件已锁定,但没有其他信息可用”?
- airflow - 如何实现气流中任务的动态生成
- javascript - 为什么我的 Node.js 网站在公用文件夹中找不到页面?
- bash - Monit 在第一次运行 exec 命令后给出错误
- python - 对索引步数不大于 1 的行求和。 Pandas Python