bpG*_*sar 5 javascript reactjs jestjs enzyme
我开始学习应用程序测试,我需要测试功能。我有一些组件:
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
);
}
};
}
Run Code Online (Sandbox Code Playgroud)
我想测试名为 whenCDM 的函数,但我不知道如何正确执行。
我终于找到答案了。我只是不明白什么是“模拟”。
这是我的问题的答案:
第一的。需要小的重构功能。
private whenCDM = (countriesList:any): any => {
if (countriesList.current !== null) {
this.whenComponentDidMount(
countriesList.current.getBoundingClientRect().top
);
}
};
Run Code Online (Sandbox Code Playgroud)
然后在cDM中:
public componentDidMount(): void {
setTimeout(this.whenCDM(this.countriesList), 1);
}
Run Code Online (Sandbox Code Playgroud)
然后在测试文件中创建模拟函数: 我想,我可以在 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4412 次 |
| 最近记录: |