我开始学习应用测试,我需要测试该功能。 我有一些组件:
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的函数,但我不知道该怎么做。
答案 0 :(得分:2)
我终于找到答案了。 我只是不明白什么是“模拟”。
以下是我的问题的答案:
首先。需要小的重构功能。
FragmentPagerAdapter viewPagerAdapter = new ExampleAdapter();
mviewPager.setAdapter(viewPagerAdapter);
mtabLayout.setupWithViewPager(mviewPager);
然后在cDM中:
{
"name": "minimap-cursorline",
"version": "0.2.0"
},
{
"name": "minimap-find-and-replace",
"version": "4.5.2"
},
{
"name": "minimap-highlight-selected",
"version": "4.6.1"
},
{
"name": "minimap-linter",
"version": "2.1.1"
},
然后在测试文件中创建模拟函数: 我猜,我只能在 top 选项中设置getBoundingClientRect,但是无论如何...
private whenCDM = (countriesList:any): any => {
if (countriesList.current !== null) {
this.whenComponentDidMount(
countriesList.current.getBoundingClientRect().top
);
}
};