我正在使用 Strapi CMS 并努力获取嵌套/深层内容的数据。例如:假设我创建了以下内容类型并定义了关系。
人:姓名、年龄
地址:城市,国家
联系方式:代码、号码
一个人有一个地址
地址有很多联系人
现在的问题是,当我访问 '/persons' 时,我只得到 Name、Age 和 Address 对象。但是地址对象没有与地址关联的联系信息。
有人可以帮我解决这个问题或给我指出任何这样的文章吗?
我想使用 Jest 对我的应用程序进行单元测试,Jest 使用 Handlebar 作为模板引擎。
我能够对 Handlebar 助手进行单元测试,但无法测试 Handlebar 部分。
当我尝试导入 spec.js 中的部分内容时,出现错误:
意外字符“#”(1:2)
下面是partial.handlerbars文件
{{#with (resource 'some_selector') }}
<img alt="{{altText}}" src="{{image-url}}" />
{{/with}}
Run Code Online (Sandbox Code Playgroud)
下面是partial.spec.js
import partial from './partial.handlebars';
const Handlebars = require('handlebars');
describe('partial suite', ()=> {
Handlebars.registerPartial('partial', partial);
it('should always be true', ()=> {
expect(true).toBe(true);
})
})
Run Code Online (Sandbox Code Playgroud)
下面是错误:
D:/projects/src/components/partials/partial.handlebars: Unexpected character '#' (1:2)
> 1 | {{#with (resource 'some_selector') }}
| ^
Run Code Online (Sandbox Code Playgroud) 如何使用 Jest 测试 HTML 字符串?
现在,我正在使用如下所示的常规字符串比较来执行此操作。当我运行这个测试用例时,它工作正常。
it('should compile partial as per the context', () => {
const receivedOutput = someFunctionReturningHTMLString();
//*PS: someFunctionReturningHTMLString() is function which returns a html string;*
//In above call, lets assume it returns `<a href="/" class="some_class1 some_class2" >`
const expectedResult = `<a href="/" class="some_class1 some_class2" >`;
expect(receivedOutput .trim()).toEqual(expectedResult.trim())
});
Run Code Online (Sandbox Code Playgroud)
但是,我不认为这种测试纯字符串值的方法是一种好方法。
有人可以帮助我用更好的方法来实际测试 HTML 字符串的内容吗?比如测试字符串中的标签是否有特定的类?类似于我们可以使用 Enzyme for React 组件实现的功能。