如何正确模拟其属性中具有组件的React组件类?
我需要在开玩笑上模拟组件,组件结构是这样的:
export class ClassName extends React.Component<ClassNameProps> {
static PropOne;
static PropTwo;
static MethodOne(param: string): void;
static MethodTwo(param: string): void;
}
注意:它上的每个静态属性,返回不同的反应组件。因此,phopone是一个组件
注2 :我对此组件的负责人不可能,我没有权限更改它。
在我的模拟文件上,我通常会做类似的事情:
export default {
ClassName: 'class-name'
}
但是在这种情况下,我也需要模拟这些子组件。我已经尝试了以下想法:
ClassName: {
PropOne: jest.fn(() => Promise.resolve('')),
PropTwo: jest.fn(() => Promise.resolve('')),
MethodOne: jest.fn(),
MethodTwo: jest.fn(),
},
ClassName: class ClassName extends Component<ClassNameProps> {
static PropOne: Component<ClassNameProps>|string = 'class-name.prop-one';
static PropTwo: Component<ClassNameProps>|string = 'class-name.prop.two';
static MethodOne = jest.fn();
static MethodTwo = jest.fn();
},
PS:此组件是附加软件包的一部分。他们不在我的项目上。
I need to mock a component on jest, the component structure is like this:
export class ClassName extends React.Component<ClassNameProps> {
static PropOne;
static PropTwo;
static MethodOne(param: string): void;
static MethodTwo(param: string): void;
}
Note: Each of those static properties on it, return a different react component. So PropOne is a component
Note 2: I'm not the responsable for this component, and I don't have the permission to change it.
On my mock files, I usually do something like:
export default {
ClassName: 'class-name'
}
But in this case, I need to mock those sub components as well. I've already tried the following ideas:
ClassName: {
PropOne: jest.fn(() => Promise.resolve('')),
PropTwo: jest.fn(() => Promise.resolve('')),
MethodOne: jest.fn(),
MethodTwo: jest.fn(),
},
ClassName: class ClassName extends Component<ClassNameProps> {
static PropOne: Component<ClassNameProps>|string = 'class-name.prop-one';
static PropTwo: Component<ClassNameProps>|string = 'class-name.prop.two';
static MethodOne = jest.fn();
static MethodTwo = jest.fn();
},
P.S.: This component is part of a additional package. They are not on my project.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论