如何正确模拟其属性中具有组件的React组件类?

发布于 2025-02-13 06:06:23 字数 1028 浏览 0 评论 0原文

我需要在开玩笑上模拟组件,组件结构是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文