You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为AEM React组件编写Jest单元测试用例:覆盖FiveGIntermediateEditConfig

测试FiveGIntermediateEditConfig的Jest用例

首先得把FiveGIntermediateEditConfig单独导出,原代码里它只作为compose的参数传入,没对外暴露,没法直接在测试里引用。修改原代码,给配置加个单独的导出:

// 新增单独导出,不影响原有导出逻辑
export const FiveGIntermediateEditConfig = {
  emptyLabel: "FiveG Intermediate",

  isEmpty() {
    return true;
  },
};

// 其余代码保持不变

然后创建对应的测试文件(比如FiveGIntermediateContainer.test.js),编写测试用例覆盖配置的属性和方法:

import { FiveGIntermediateEditConfig } from './FiveGIntermediateContainer';

describe('FiveGIntermediateEditConfig', () => {
  test('emptyLabel属性值正确', () => {
    expect(FiveGIntermediateEditConfig.emptyLabel).toBe('FiveG Intermediate');
  });

  test('isEmpty方法返回true', () => {
    expect(FiveGIntermediateEditConfig.isEmpty()).toBe(true);
  });
});

额外说明

如果不想改动原代码的导出结构,也可以尝试从最终导出的高阶组件里提取配置,但这种方式依赖compose和MapTo的内部实现,耦合性高,不推荐。最稳妥的方式还是单独导出配置,既不影响业务代码,测试也更直观。

内容的提问来源于stack exchange,提问作者valli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 15:45:37